How can I determine the presence and type of a member variable named?

I know how to write a class that can be detected at compile time if a given class T has a named member with a given Type, for example.

#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 specify the exact type of a member variable, because if I want to use those traits most of the time, what matters to me is whether the member is convertible to a certain type, is an integral type, etc. not the exact type. I would like to be able to detect the existence and type of a member variable with a given name. I would like 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 the & T :: foo construct is legal, it is possible to get the element type 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 create a combination of the two methods that SFINAEs need to know the signature of the member pointer for correct results, mainly due to the helper structure. The final code will be a preprocessor macro with a name as an argument, so any solutions can use a preprocessor as well.

+3


source to share


2 answers


This is an easy way to do 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 this is how you 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 easier 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

      

Then you can use them like this:

DEFINE_MEMBER_CHECKER(id)

int main()
{
    static_assert(
        HAS_MEMBER(X, id) && is_integral<MEMBER_TYPE(X, id)>::value,
        "Error!"
        );
}

      

+5


source


#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";
}

      



must work. If your compiler doesn't already support template aliases, replace TestCode< decltype( std::declval<T>().foo ) >

with typename test_code< decltype( std::declval<T>().foo ) >::type

.

+1


source







All Articles