8.3.5/8 函数 [dcl.fct]
说
[...] Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things. [...]
为什么规则如此明确?是否有一些语法甚至允许返回函数而不是函数指针?
我是否误解了这句话?
typedef void (*fp)();
void foo(){}
fp goo()
{
return foo; //automatically converted to function pointer
}
最佳答案
这是一个非常人为的函数尝试返回函数的例子:
void foo() { }
template<typename T>
T f() { return foo; }
int main(){
f<decltype(foo)>();
}
这是我从 Clang 3.2 得到的错误:
Compilation finished with errors:
source.cpp:7:5: error: no matching function for call to 'f'
f<decltype(foo)>();
^~~~~~~~~~~~~~~~
source.cpp:4:3: note: candidate template ignored: substitution failure
[with T = void ()]: function cannot return function type 'void ()'
T f() { return foo; }
~ ^
1 error generated.
关于c++ - 不允许从函数返回函数。我怎么能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14865174/