gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:46:32 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com