Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

template types in std is_member_function_pointer

Tags:

c++

c++11

c++14

I am referring to a possible implementation of std::is_member_function_pointer here.

template< class T >
struct is_member_function_pointer_helper : std::false_type {};

template< class T, class U>
struct is_member_function_pointer_helper<T U::*> : std::is_function<T> {};

template< class T >
struct is_member_function_pointer : is_member_function_pointer_helper< std::remove_cv_t<T> >     


#include <type_traits>

class A {
public:
    void member() { }
};



int main()
{
    // fails at compile time if A::member is a data member and not a function
    static_assert(std::is_member_function_pointer<decltype(&A::member)>::value,
                  "A::member is not a member function."); 
}

I know that

"dectype(&A::member)"

will be

"void (A::*)()"

But I dont understand what type T and type U map to in "void (A::*)()"?

like image 519
Rajat Avatar asked Dec 07 '25 17:12

Rajat


1 Answers

The pattern void (A::*)() decomposes into T U::* where U is A and T is void (). You can see the construction in inverse, like this:

struct A {};

using T = void ();
using U = A;

template <typename> struct print;

int main() {
    print<T U::*>{};
    // error: implicit instantiation of undefined template 'print<void (A::*)()>'
}

So the T as void () is passed to std::is_function which determines whether the given type is a function type.

In the case of pointer to data members, this fails because the deduced T is going to be a non-function type.

like image 88
mpark Avatar answered Dec 09 '25 19:12

mpark