指向成员的指针如何绑定到 T U::*

how is pointer to member able to bind to T U::*

我正在经历 std::is_member_pointer in cpprefernce

template< class T >
struct is_member_pointer_helper         : std::false_type {};
 
template< class T, class U >
struct is_member_pointer_helper<T U::*> : std::true_type {};
 
template< class T >
struct is_member_pointer : 
    is_member_pointer_helper<typename std::remove_cv<T>::type> {};

int main() {
    class cls {};
    std::cout << (std::is_member_pointer<int cls::*>::value
                     ? "T is member pointer"
                     : "T is not a member pointer") << '\n';
    std::cout << (std::is_member_pointer<int(cls::*)(int, int)>::value
                     ? "T is member pointer"
                     : "T is not a member pointer") << '\n';
}

输出:

T is member pointer
T is member pointer

int cls::*int(cls::*)(int, int) 如何绑定到 struct is_member_pointer_helper<T U::*> 中的 T U::*

对于 int cls::* Tint 并且 Ucls 对于 int(cls::*)(int) 什么是 TU?我浏览了这个 ,其中包含以下代码:

template<class MemberT, class ClassT>
using make_member_ptr = MemberT ClassT::*;

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

int main()
{   
    make_member_ptr<int, A> ptr;
    make_member_ptr<void(), A> fptr;
    static_assert(std::is_same_v<int A::*, decltype(ptr)>);
    static_assert(std::is_same_v<decltype(fptr), void(A::*)()>);
    return 0;
}

就像评论中的 werner pointer make_member_ptr<void(), A> 是如何扩展到 void (A::*)()

link to the code

类型用单词拼写更容易理解

T U::* 是“指向 class U 成员的指针,类型为 T”。

int(cls::*)(int, int) 是“指向 class cls 成员的指针,类型为 'a function with parameters (int, int), returning int'”。

因此 Ucls,而 T 是一个“带有参数 (int, int) 的函数,返回 int”,又名 int(int, int) .