Determine if a class has a function

Using a trick (described by Olivier Langlois ) I can tell if a class is of a specific type:

template<typename T> struct hasType
{
    template<typename C> static char test( typename C::Type );
    template<typename C> static char* test(...);
    enum{ Value= sizeof(test<T>(0))==1 };
};

      

I can also tell if a class has a variable:

template<typename T> struct hasType
{
    template<typename C> static char test( decltype(C::var) );
    template<typename C> static char* test(...);
    enum{ Value= sizeof(test<T>(0))==1 };
};

      

However, decltype(c::func)

nur decltype(c::func())

(which depends on parameters) does not work for member functions . Is there a way to do this, or do I need to create a functor in each class and detect it using typename C::functor

?

Edit : You are both correct, but since I also have to test for the type, I will use decltype(&C::func)

(which obviously needs to be a pointer).

+2
c ++ templates sfinae member-functions


source to share


No one has answered this question yet

See similar questions:

115
Check if a class has a member function of a specific signature

or similar:

1138
Why do we need virtual functions in C ++?
674
Is the 'const' value last in the class function declaration?
563
Use 'class' or 'typename' for template parameters?
519
How to call a parent class function from a derived class function?
8
Determining if a type is a class type?
6
C ++. Do you know if the type / class is nested?
five
SFINAE - Attempt to determine if a template type has a member function with a variable return type
3
How does SFINAE C ++ syntax work?
1
SFINAE concepts / error with typename
1
Determine class type in C ++ without reflection / introspection



All Articles
Loading...
X
Show
Funny
Dev
Pics