C ++函数返回函数 [英] C++ function returning function

查看:170
本文介绍了C ++函数返回函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准中的函数返回函数不允许?我理解他们在概念上是可笑的,但在我看来,语法将允许他们。根据此网页, noptr-declarator [是]任何有效的声明者,其中将包括声明符的函数:

  int f() 






>



在我看来,[dcl.decl]中的语法允许

  int f(char)(double)

作为 char f >并返回与 int g(double)相同签名的函数。

  1 declarator:
2 ptr-declarator
3 noptr-declarator参数和限定符trailing-return-type
4 ptr-declarator:
5 noptr -declarator
6 ptr-operator ptr-declarator
7 noptr-declarator:
8 declarator-id attribute-specifier-seq opt
9 noptr-declarator parameters-and-qualifiers
10 noptr-declarator [constant-expression opt] attribute-specifier-seq opt
11(ptr-declarator)
12参数和限定符:
13(parameter-子句)cv-qualifier-seqAfter

大致来说,在
1-> 2,2 = 4,4-> 6,4-> 6
你应该有
ptr-operator ptr-operator ptr-operator
然后使用4-> 5,5 = > 8为第一个声明符;对于第二和第三声明符使用4-> 5,5 = 7,7-> 9。

解决方案

来自[dcl.fct]:


函数不应该具有数组或函数类型的返回类型,尽管它们可能有返回类型
类型指针或引用这样的事情。没有函数数组,虽然可以有数组
的函数指针。


使用C ++ 11 ,你可能只是想要:

  std :: function< int()> F(); 
std :: function< int(double)> f(char);






有关C ++语法的一些混淆。可以根据语法解析语句 int f(char)(double); 这是一个解析树:



此外,这样的解析基于[dcl.fct] / 1甚至有意义:


TD 中声明 D 的格式为

      D1 parameter-declaration-clause cv-qualifier-seq

          ref-qualifier opt > opt attribute-specifier-seq opt

以及所包含的 declarator-id <在 T D1 的声明
D 中的 declarator-id
类型是 derived-declarator-type-list function of( parameter-declaration-clause cv-qualifier-seq opt
ref-qualifier opt 返回 T


这个例子 T == int D == f(char)(double) D1 == f(char) T D1 int f(char))中的 declarator-id 类型是(char)返回int的函数。因此,derived-declarator-type-list 是(char)返回的函数。因此, f 的类型将被读为函数的(char)返回函数的(double)返回int。



这是最后很多东西,什么也没有,因为这是一个明确禁止的声明形式。但不是由语法。


Where in the standard are functions returning functions disallowed? I understand they are conceptually ridiculous, but it seems to me that the grammar would allow them. According to this webpage, a "noptr-declarator [is] any valid declarator" which would include the declarator of a function:

int f()();


Regarding the syntax.

It seems to me that the syntax, as spelled out in [dcl.decl], allows

int f(char)(double)

which could be interpreted as the function f that takes a char and returns a function with same signature as int g(double).

1    declarator:
2       ptr-declarator
3       noptr-declarator parameters-and-qualifiers trailing-return-type
4    ptr-declarator:
5        noptr-declarator
6        ptr-operator ptr-declarator
7    noptr-declarator:
8        declarator-id attribute-specifier-seq opt
9        noptr-declarator parameters-and-qualifiers
10       noptr-declarator [ constant-expression opt ] attribute-specifier-seq opt
11       ( ptr-declarator )
12    parameters-and-qualifiers:
13       ( parameter-declaration-clause ) cv-qualifier-seqAfter

Roughly speaking, after 1->2, 2=4, 4->6, 4->6 you should have ptr-operator ptr-operator ptr-operator Then, use 4->5, 5=7, 7->8 for the first declarator; use 4->5, 5=7, 7->9 for the second and third declarators.

解决方案

From [dcl.fct], pretty explicitly:

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. There shall be no arrays of functions, although there can be arrays of pointers to functions.

With C++11, you probably just want:

std::function<int()> f();
std::function<int(double)> f(char);


There is some confusion regarding the C++ grammar. The statement int f(char)(double); can be parsed according to the grammar. Here is a parse tree:

Furthermore such a parse is even meaningful based on [dcl.fct]/1:

In a declaration T D where D has the form
    D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
        ref-qualifieropt exception-specificationopt attribute-specifier-seqopt
and the type of the contained declarator-id in the declaration T D1 is "derived-declarator-type-list T", the type of the declarator-id in D is "derived-declarator-type-list function of (parameter-declaration-clause ) cv-qualifier-seqopt ref-qualifieropt returning T".

In this example T == int, D == f(char)(double), D1 == f(char). The type of the declarator-id in T D1 (int f(char)) is "function of (char) returning int". So derived-declarator-type-list is "function of (char) returning". Thus, the type of f would be read as "function of (char) returning function of (double) returning int."

It's ultimately much ado about nothing, as this is an explicitly disallowed declarator form. But not by the grammar.

这篇关于C ++函数返回函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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