问题
I know how to write a class that can detect at compile time if a given class T has a member with a given name with given type Type, e.g.
#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, ":(");
}
I don't like that you have to state the exact type of the member variable because if I want to use these traits most of the time I care whether this member is convertible to a certain type, is an integral type, etc. and not about the exact type. I'd like to have the ability to detect the presence and the type of a member variable with a given name. I would like to be able to write something like this:
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");
If I know that the construct &T::foo is legal, it is possible to get the type of the member via something like
template <typename T, typename U>
T get_member_type(T U::*);
typedef decltype(get_member_type(&T::foo)) the_member_type;
but I cannot produce a combination of the two methods that SFINAEs to the correct results, mainly due to the helper-struct has to know the signature of the pointer to the member. The final code will be a preprocessor macro with the name as argument so any solutions are allowed to use the preprocessor, too.
回答1:
This is a simple way of doing it for a variable named id
:
#include <type_traits>
using namespace std;
template<typename T, typename V = bool>
struct has_id : false_type { };
template<typename T>
struct has_id<T,
typename enable_if<
!is_same<decltype(declval<T>().id), void>::value,
bool
>::type
> : true_type
{
typedef decltype(declval<T>().id) type;
};
And here is how you would use it:
#include <iostream>
using namespace std;
struct X { int id; };
int main()
{
static_assert(
has_id<X>::value && is_integral<has_id<X>::type>::value,
"Error!"
);
}
You can make things even simpler if you can tolerate macros:
#define DEFINE_MEMBER_CHECKER(member) \
template<typename T, typename V = bool> \
struct has_ ## member : false_type { }; \
template<typename T> \
struct has_ ## member<T, \
typename enable_if< \
!is_same<decltype(declval<T>().member), void>::value, \
bool \
>::type \
> : true_type \
{ \
typedef decltype(declval<T>().member) type; \
};
#define HAS_MEMBER(C, member) \
has_ ## member<C>::value
#define MEMBER_TYPE(C, member) \
has_ ## member<C>::type
You could then use them this way:
DEFINE_MEMBER_CHECKER(id)
int main()
{
static_assert(
HAS_MEMBER(X, id) && is_integral<MEMBER_TYPE(X, id)>::value,
"Error!"
);
}
回答2:
#include <type_traits>
template<typename T>
struct test_code { typedef void type; };
template<typename T>
using TestCode = typename test_code<T>::type;
template<typename T, typename=void>
struct has_member_foo:std::false_type {};
template<typename T>
struct has_member_foo<T,
TestCode< decltype( std::declval<T>().foo ) >
>:std::true_type
{
typedef decltype( std::declval<T>().foo ) type;
};
template<typename T>
using FooMemberType = typename has_member_foo<T>::type;
struct test_nofoo {};
struct test_foo { int foo; };
#include <iostream>
int main() {
std::cout << has_member_foo<test_nofoo>::value << has_member_foo<test_foo>::value << std::is_same<int, FooMemberType<test_foo>>::value << "\n";
}
should work. If your compiler doesn't support template aliases yet, replace TestCode< decltype( std::declval<T>().foo ) >
with typename test_code< decltype( std::declval<T>().foo ) >::type
.
来源:https://stackoverflow.com/questions/14940464/how-to-detect-the-presence-and-type-of-a-member-variable-given-its-name