我需要检查并查看给定的类型是否有成员变量。但是,如果给出的类型不是类或结构,则会出现编译器错误。使用模板来检查不工作的成员变量int
struct Vector {
int x;
};
template <typename Type>
class has_member_x
{
class yes { char m;};
class no { yes m[2];};
struct BaseMixin
{
int x;
};
struct Base : public Type, public BaseMixin {};
template <typename T, T t> class Helper{};
template <typename U>
static no deduce(U*, Helper<void (BaseMixin::*)(), &U::x>* = 0);
static yes deduce(...);
public:
static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0)));
};
int main() {
BOOST_STATIC_ASSERT(has_member_x<int>::result);
BOOST_STATIC_ASSERT(has_member_x<Vector>::result);
}
当我尝试编译它失败,并出现以下错误。
error: base type ‘int’ fails to be a struct or class type
有没有一种方法可以在C++或C++ 0x中完成?
关闭我的头顶,我的第一个想法是专门为非类类型的'has_member_x'。 –
为什么不检查std :: numeric_limits <>是否专门针对您的类型T?如果是,假设它没有成员'x'。或者为内建函数编写你自己的专业化版本,比如'template <> class has_member_x {public:static const bool result = false; }'。 –
Sjoerd