How to check if a member name (variable or function) exists in a class, with or without specifying type?

时光怂恿深爱的人放手 提交于 2019-11-28 14:03:06

With std::experimental::is_detected and std::experimental::disjunction you could do this:

//check for a type member named foo
template <typename T>
using foo_type_t = typename T::foo;

//check for a non-type member named foo
template <typename T>
using foo_non_type_t = decltype(&T::foo);

template <typename T>
using has_foo = disjunction<is_detected<foo_type_t, T>,
                            is_detected<foo_non_type_t, T>>;

Then you would use has_foo<my_class>::value in whatever you want.

The above will work for more than just types and member functions, but you could easily constrain it by using traits like std::is_member_function_pointer and std::is_member_object_pointer if you like.

To supply your optional argument, you could use the std::experimental::is_detected_exact helper.

Live Demo


Note that if you take the implementations of the above traits from the pages I linked, you can use this with C++14. A minor change to the disjunction code will let you use it in C++11.

C++03

#define HasMember(NAME) \
  template<class Class, typename Type = void> \
  struct HasMember_##NAME \
  { \
    typedef char (&yes)[2]; \
    template<unsigned long> struct exists; \
    template<typename V> static yes Check (exists<sizeof(static_cast<Type>(&V::NAME))>*); \
    template<typename> static char Check (...); \
    static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes)); \
  }; \
  template<class Class> \
  struct HasMember_##NAME<Class, void> \
  { \
    typedef char (&yes)[2]; \
    template<unsigned long> struct exists; \
    template<typename V> static yes Check (exists<sizeof(&V::NAME)>*); \
    template<typename> static char Check (...); \
    static const bool value = (sizeof(Check<Class>(0)) == sizeof(yes)); \
  }

Usage: Simply invoke the macro with whatever member you want to find:

HasMember(Foo);  // Creates a SFINAE `class HasMember_Foo`
HasMember(i);    // Creates a SFINAE `class HasMember_i`

Now we can utilize HasMember_X to check X in ANY class as below:

#include<iostream>
struct S
{
  void Foo () const {}
//  void Foo () {}  // If uncommented then type should be mentioned in `HasMember_Foo`    
  int i;
};
int main ()
{
  std::cout << HasMember_Foo<S, void (S::*) () const>::value << "\n";
  std::cout << HasMember_Foo<S>::value << "\n";
  std::cout << HasMember_i<S, int (S::*)>::value << "\n";
  std::cout << HasMember_i<S>::value << "\n";
}

Catches:

  1. In case of methods, if we don't mention the type then the class must not have overloaded methods. If it has then this trick fails. i.e. even though the named member is present more than once, the result will be false.
  2. If the member is part of base class, then this trick fails; e.g. if B is base of S & void B::Bar () is present, then HasMember_Bar<S, void (B::*)()>::value or HasMember_Bar<S, void (S::*)()>::value or HasMember_Bar<S>::value will give false

For non-C++17:

Boost Type Traits library has metafunctions for both checking if member with given name present or more fine grained control by giving signature.

Following is a program using slightly modified HAS_MEMBER macro:

#include <iostream>
#include <type_traits>

#define DECLARE_HAS_MEMBER(__trait_name__, __member_name__)                          \
                                                                                     \
    template <typename __boost_has_member_T__>                                       \
    class __trait_name__                                                             \
    {                                                                                \
        using check_type = ::std::remove_const_t<__boost_has_member_T__>;            \
        struct no_type {char x[2];};                                                 \
        using  yes_type = char;                                                      \
                                                                                     \
        struct  base { void __member_name__() {}};                                   \
        struct mixin : public base, public check_type {};                            \
                                                                                     \
        template <void (base::*)()> struct aux {};                                   \
                                                                                     \
        template <typename U> static no_type  test(aux<&U::__member_name__>*);       \
        template <typename U> static yes_type test(...);                             \
                                                                                     \
        public:                                                                      \
                                                                                     \
        static constexpr bool value = (sizeof(yes_type) == sizeof(test<mixin>(0)));  \
    }

struct foo
{
    int bar(){}
};

struct baz
{};

DECLARE_HAS_MEMBER(has_bar, bar);

int main()
{
    std::cout << has_bar<foo>::value << '\n' << has_bar<baz>::value;
}

As noted in linked page it uses the fact that if you inherit from two classes that have members with same name, attempts to use this name will become ambiguous.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!