我需要检查并查看给定类型是否具有成员变量。但是,如果给出的类型不是类或结构,则会出现编译错误。
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);
}
当我尝试编译它时失败并出现以下错误。
错误:基类型'int'无法是结构或类类型
有没有办法可以在c ++或c ++ 0x中完成?
答案 0 :(得分:1)
你可以使用boost :: enable_if和boost :: type_traits,特别是boost :: is_integral或boost :: is_class。
试试这个(我没有测试过,但这应该会给你一个想法。):
struct Vector {
int x;
};
template <typename Type, typename Enabled = void>
class has_member_x
{
public:
static const bool result = false;
};
template <typename Type, typename Enabled = boost::enable_if<boost::is_class<Type> >::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);
}
答案 1 :(得分:1)
修复原始has_member_x
类中的错误,并按照其他人的建议为class
类型添加专门化后,最终的工作版本如下所示:
#include <type_traits>
template <typename Type, bool Enabled = std::is_class<Type>::value>
class has_member_x
{
public:
static const bool result = false;
};
template <typename Type>
class has_member_x<Type, true>
{
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<int BaseMixin::*, &U::x>* = 0);
static yes deduce(...);
public:
static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0)));
};