不允许从函数返回函数。我怎么能? [英] Not allowed to return a function from a function. How could I?

查看:2338
本文介绍了不允许从函数返回函数。我怎么能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ b


8.3.5 / 8函数[dcl.fct] [...] 函数不应该有返回类型的
类型
数组或函数,尽管它们可能有返回类型的类型指针或对此类型的引用的东西。 [b]

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



我是否错过了解释报价?

  typedef void(* fp)(); 

void foo(){}
fp goo()
{
return foo; //自动转换为函数指针
}


解决方案

这是一个试图返回函数的函数的一个人为的例子:

  void foo(){} 

模板< typename T>
T f(){return foo; }
$ b $ int main(){
f< decltype(foo)>();
}

这是我从Clang 3.2获得的错误:

 编译完成后出现错误:
source.cpp:7:5:错误:没有匹配函数调用'f'
F< decltype(FOO)GT;();
^ ~~~~~~~~~~~~~~~
source.cpp:4:3:note:candidate template ignored:substitution failure
[with T = void( )]:函数不能返回函数类型'void()'
T f(){return foo; }
〜^
1产生的错误。


8.3.5/8 Functions [dcl.fct] says

[...] 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. [...]

Why so explicit of a rule? Is there some syntax that would even allow returning a function as opposed to a function pointer?

Am I miss-interpreting the quote?

typedef void (*fp)();

void foo(){}
fp goo()
{
    return foo; //automatically converted to function pointer
}

解决方案

This is quite a contrived example of a function trying to return a function:

void foo() { }

template<typename T>
T f() { return foo; }

int main(){
    f<decltype(foo)>();
}

This is the error I get from 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.

这篇关于不允许从函数返回函数。我怎么能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆