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

iammilind

This Q is an extension of:
Is it possible to write a C++ template to check for a function's existence?

Is there any utility which will help to find:

  • If a member name exists inside a class or not? This member can be a variable or a method.
  • Specifying the type of the member should be optional
iammilind

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Type trait: Check if reference member variable is static or not

分類Dev

How check if type is class?

分類Dev

variable no of arguments for class template's member function

分類Dev

Call member function when class type is not known

分類Dev

How to check if a variable exists in a batch file?

分類Dev

How to check if a variable exists in a batch file?

分類Dev

How to Check if a SYSTEM environment variable exists?

分類Dev

How do I check if a variable exists in an 'if' statement?

分類Dev

How to get/implement "Uninitialized uses warning" message for built-in type member variable of a class?

分類Dev

Using a class variable as a default argument to the class member function

分類Dev

How does this function know the variable exists?

分類Dev

Creation of a functor inside a member function without taking the class as a argument

分類Dev

Deducing the return type of an inline static member function in class definition

分類Dev

Check if database with specified name exists or not

分類Dev

How to check if a certain method/class/API exists in Xamarin iOS?

分類Dev

How would I create a class with a variable name in the class name?

分類Dev

How to access Mainfrm member variable status without using ((CMainFrame*) AfxGetMainWnd ())->...?

分類Dev

How is the move constructor of member variable invoked without using std::forward?

分類Dev

Set A variable from other Variable in a class without function

分類Dev

Automapper - How to get containing class type when mapping a member?

分類Dev

How to register member function to lua without lua bind in c++

分類Dev

Specifying return type of a Public Function is necessary or not?

分類Dev

How to call a builtin function on every column in a table with bigquery without specifying column names?

分類Dev

How to pass a member function which has variable arguments as template argument?

分類Dev

How can I use a member function of a forward declared class?

分類Dev

How to pass a parameter by reference to a member function of template class?

分類Dev

how to declare member function using template? (not template class)

分類Dev

How to get a general function pointer as a private member of a C++ class?

分類Dev

How to console log the name of a variable/function?

Related 関連記事

  1. 1

    Type trait: Check if reference member variable is static or not

  2. 2

    How check if type is class?

  3. 3

    variable no of arguments for class template's member function

  4. 4

    Call member function when class type is not known

  5. 5

    How to check if a variable exists in a batch file?

  6. 6

    How to check if a variable exists in a batch file?

  7. 7

    How to Check if a SYSTEM environment variable exists?

  8. 8

    How do I check if a variable exists in an 'if' statement?

  9. 9

    How to get/implement "Uninitialized uses warning" message for built-in type member variable of a class?

  10. 10

    Using a class variable as a default argument to the class member function

  11. 11

    How does this function know the variable exists?

  12. 12

    Creation of a functor inside a member function without taking the class as a argument

  13. 13

    Deducing the return type of an inline static member function in class definition

  14. 14

    Check if database with specified name exists or not

  15. 15

    How to check if a certain method/class/API exists in Xamarin iOS?

  16. 16

    How would I create a class with a variable name in the class name?

  17. 17

    How to access Mainfrm member variable status without using ((CMainFrame*) AfxGetMainWnd ())->...?

  18. 18

    How is the move constructor of member variable invoked without using std::forward?

  19. 19

    Set A variable from other Variable in a class without function

  20. 20

    Automapper - How to get containing class type when mapping a member?

  21. 21

    How to register member function to lua without lua bind in c++

  22. 22

    Specifying return type of a Public Function is necessary or not?

  23. 23

    How to call a builtin function on every column in a table with bigquery without specifying column names?

  24. 24

    How to pass a member function which has variable arguments as template argument?

  25. 25

    How can I use a member function of a forward declared class?

  26. 26

    How to pass a parameter by reference to a member function of template class?

  27. 27

    how to declare member function using template? (not template class)

  28. 28

    How to get a general function pointer as a private member of a C++ class?

  29. 29

    How to console log the name of a variable/function?

ホットタグ

アーカイブ