标准6.8节中的这个例子如何工作?

LxL*_*LxL 3 c++ function-pointers function

在标准的章节§6.8(N3690草案)中,我看到了这段奇怪的代码:

struct T2 { T2(int){ } };
int a, (*(*b)(T2))(int), c, d;
Run Code Online (Sandbox Code Playgroud)

什么是 int(*(*b)(T2))(int)?!

b指向T2构造函数的指针吗?或者是指向函数指针的指针?

波纹管代码也编译得很好奇怪!:

struct T2 { T2(int){ } };
int((*b)(T2));
Run Code Online (Sandbox Code Playgroud)

Naw*_*waz 9

int (*(*b)(T2))(int) 
Run Code Online (Sandbox Code Playgroud)

它声明b指向函数的指针:

  • 需要T2作为参数
  • 并返回指向函数的 指针
    • 采取一个int参数
    • 并返回一个int.

应该使用typedef简化此声明:

typedef int (*return_type) (int);
typedef return_type(*function_type) (T2);
Run Code Online (Sandbox Code Playgroud)

或者更好地使用C++ 11样式类型别名:

using return_type   = int(*)(int);
using function_type = return_type(*)(T2);
Run Code Online (Sandbox Code Playgroud)

然后宣言成为:

function_type b; //so simple!
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • @omid:是的.额外的括号是多余的.所以你甚至可以写出`int(((((*(b)(T2)))))```````````````````````````````` (2认同)