【发布时间】:2016-04-19 06:12:26
【问题描述】:
我想检查一个类的成员变量是否是静态的。使用 std::is_member_pointer 对除引用成员之外的所有类型都有效。
#include <type_traits>
struct A {
int foo;
};
struct B : A {};
struct C {
static int foo;
};
struct D : C {
};
struct E {
int &foo;
};
struct F {
static int &foo;
};
static_assert(std::is_member_pointer<decltype(&A::foo)>::value, "No");
static_assert(std::is_member_pointer<decltype(&B::foo)>::value, "No");
static_assert(!std::is_member_pointer<decltype(&C::foo)>::value, "No");
static_assert(!std::is_member_pointer<decltype(&D::foo)>::value, "No");
// Fail to compile:
static_assert(std::is_member_pointer<decltype(&E::foo)>::value, "No");
static_assert(!std::is_member_pointer<decltype(&F::foo)>::value, "No");
我理解错误,指针不能指向引用成员。但是如何避免它并且仍然区分它是静态变量还是非静态变量?有什么想法吗?
【问题讨论】:
标签: c++ c++11 reference typetraits