首页 > 解决方案 > Syntax of an un-named function pointer in C++

问题描述

I was looking into most vexing parse, and I stumbled upon something like this:

Foo bar(Baz()); // bar is a function that takes a pointer to a function that returns a Baz and returns a Foo

This is quite different from the typical syntax of return-type(*name)(parameters). Are the parenthesis present the parenthesis for the parameter list, or are they for the name?

标签: c++

解决方案


完全显式形式:

Foo bar(Baz f());

bar是一个带单个参数f的函数,它是一个返回的函数(不带参数)Baz

不命名参数:

Foo bar(Baz ());

最终采用指向函数的指针的原因bar是函数不能按值传递,因此将参数声明为函数会自动将其衰减为指针。上面的声明等价于:

Foo bar(Baz (*)());

// or:
Foo bar(Baz (*f)());  // with a named parameter

这类似于参数列表中的void foo(int [10])whereint [10]也意味着int *


推荐阅读