8.3.5/8 Functions [dcl.fct]



为什么这么明确的规则?是否有一些语法甚至允许返回函数而不是函数指针?

我会误解报价吗?

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/

10-11 23:10
查看更多