std中的模板类型是is_member_function_pointer

Raj*_*jat 3 c++ c++11 c++14

我指的是std::is_member_function_pointer 这里可能的实现.

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."); 
}
Run Code Online (Sandbox Code Playgroud)

我知道

"dectype(A ::构件)"

将会

"void(A ::*)()"

但我不明白什么类型T和类型U映射到"void(A ::*)()"?

mpa*_*ark 6

图案void (A::*)()分解成T U::*其中UATvoid ().您可以反向查看构造,如下所示:

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::*)()>'
}
Run Code Online (Sandbox Code Playgroud)

因此传递给Tas ,确定给定类型是否是函数类型.void ()std::is_function

在指向数据成员的情况下,这会失败,因为推导出T的是非函数类型.