c++ - 不允许从函数返回函数。我怎么能?

标签 c++ function function-pointers language-lawyer

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/

相关文章:

python - 函数参数中空列表的值,示例here

c - 正值输入 C

c++ - 成员函数指针不太对

c++ - 将 C 函数指针分配给成员函数

C++:传递具有任意数量参数的函数作为参数

c++ - Herb Sutters 如何监控类?

c++ - Libssh2:防止后台任务被杀死

c++ - 为什么要删除非成员函数?

c++ - 使用 std::wstring 或 wchar_t 进行 Poco 日志记录

c++ - Eclipse like annotations bar for VStudio C++