void(U ::*)(void)是什么意思?

Aid*_*lly 6 c++ templates

我正在研究is_classBoost中模板的实现,并遇到了一些我无法轻易解读的语法.

    template <class U> static ::boost::type_traits::yes_type is_class_tester(void(U::*)(void));
    template <class U> static ::boost::type_traits::no_type is_class_tester(...);
Run Code Online (Sandbox Code Playgroud)

我如何解释void(U::*)(void)上述?我熟悉C,所以看起来有些类似void(*)(void),但我不明白如何U::修改指针.有人可以帮忙吗?

谢谢

Vic*_*let 13

*表示指针,因为您可以通过写入来访问其内容*p.U::*表示指向类成员的指针U.您可以通过书写u.*ppu->*p(其中u是实例U)访问其内容.

因此,在您的示例中,void (U::*)(void)指向其成员的U指针是一个不带参数且不返回任何值的函数.

例:

class C { void foo() {} };

typedef void (C::*c_func_ptr)(void);

c_func_ptr myPointer = &C::foo;
Run Code Online (Sandbox Code Playgroud)