C++ 指向成员函数语法的奇怪指针

C++ 指向成员函数语法的奇怪指针,c++,function-pointers,member-function-pointers,C++,Function Pointers,Member Function Pointers,我了解如何声明函数的类型: typedef void (typedef_void_f)(); // typedef_void_f is void() using alias_void_f = void(); // alias_void_f is void() 它可以用来声明函数指针: void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; } typedef_void_f *a = functio

我了解如何声明函数的类型:

typedef void (typedef_void_f)(); // typedef_void_f is void()
using alias_void_f     = void(); // alias_void_f is void()
它可以用来声明函数指针:

void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; }

typedef_void_f *a = function; // pointer to void()
alias_void_f   *b = function; // pointer to void()
我遇到了一系列语法错误:

很明显,我不理解p0172r0的
test
函数语法


有人能解释一下
模板constepr bool测试(类型host::*)
语法的细节吗?

类型host::*
是指向类数据成员的指针
TYPE
是类成员的类型,
host::*
表示指向
host
的成员的指针。因此,
键入host::*
接受指向
主机的任何成员的指针

请尝试以下操作:

constexpr void my_test(void (S::*)()) {}
这是声明参数类型指针的正确方法,该指针指向返回无参数的
void
S
成员函数


不,我不是在问
T::*
在模板参数中的含义。很抱歉。没有很快重新打开。这不限于成员指针。如果您使用了
T=int()
,那么
T*p
是有效的,而
int()*p
是语法规则所禁止的。@cppleener是的,正如我刚才所说的那样。那么为什么
void my_test(void()S:*)
不接受指向任何
void()的指针呢
S的成员?@PaperBirdMaster据我们所知,指向类数据成员的指针和指向类成员函数的指针指向不同的东西。我从来没有见过你在任何代码中使用的一种语法。我一直在测试,我不认为C++允许这个。请参阅:我已经用GCC6.0.0测试了in的代码。@PaperBirdMaster这很有趣。因此,如果
my_test(void(S:*)()
是声明指向
void()
成员
S
的指针的正确方法,那么不应该
template constepr bool test(TYPE host::*)
be
template constepr bool test(TYPE(host:*)())
?我不理解@PaperBirdMaster上的语法,我认为可以像您所说的那样“拆分”类型,因为模板推断规则。
struct host {
   int function() const;
};

template <typename TYPE>
constexpr bool test(TYPE host::*) { // <---- What is this??
    return is_same_v<TYPE, int() const>;
}

constexpr auto member = &host::function;

test(member);
void my_test(void() S::*) {}

my_test(&S::function);
error: variable or field 'my_test' declared void
 void my_test(void() S::*) {}
                   ^
error: expected ')' before 'S'
 void my_test(void() S::*) {}
                     ^
error: 'my_test' was not declared in this scope
     my_test(&S::function);
constexpr void my_test(void (S::*)()) {}