How to detect the presence and type of a member variable given its name?

c++, c++11, template-meta-programming

Solution

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!"
        );
}

Problem

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.

Original source