我知道如何编写一个类,在编译时可以检测到给定的类T是否具有给定类型的成员,并给定类型Type,例如,如何检测给定名称的成员变量的存在和类型?
#include <type_traits>
template <typename T, typename Type, bool = std::is_class<T>::value>
struct has_member_foo
{
private:
template <Type T::*> struct helper;
template <typename U>
static std::false_type test(...);
template <typename U>
static std::true_type test(helper<&U::foo> *);
typedef decltype(test<T>(nullptr)) testresult;
public:
static const bool value = testresult::value;
};
template <typename T, typename Type>
struct has_member_foo<T, Type, false> : std::false_type { };
struct Has_Foo
{
int foo;
};
struct Has_No_Foo
{
int bar;
};
void test()
{
static_assert(has_member_foo<Has_Foo, int>::value == true, ":(");
static_assert(has_member_foo<Has_No_Foo, int>::value == false, ":(");
static_assert(has_member_foo<int, int>::value == false, ":(");
}
我不喜欢你有状态的成员变量的确切类型,因为如果我想使用这些特性的大部分我在乎这个成员是否可以转换为某种类型的时候,是一个整数类型等,而不是确切的类型。我希望能够检测具有给定名称的成员变量的存在和类型。我希望能够写这样的事:
static_assert(has_member_foo<T>::value && std::is_integral<typename has_member_foo<T>::type>::value,
"The type has to have an integral member with name foo");
如果我知道,构建& T :: foo是合法的,则可以通过一些事来报复的成员的类型一样
template <typename T, typename U>
T get_member_type(T U::*);
typedef decltype(get_member_type(&T::foo)) the_member_type;
但我不能产生两种方法的组合,即SFINAEs到正确的结果,主要是由于helper-struct必须知道指向成员的指针的签名。最终的代码将是一个名为参数的预处理器宏,因此任何解决方案都可以使用预处理器。
啊,关键因素是decltype(STD :: declval包含.foo) - 我傻,我用它来检测函数签名的所有时间;)非常感谢! –
MadScientist
2013-02-19 08:01:41