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

问题描述

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

  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甚至有意义:

这个例子 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, after1->2, 2=4, 4->6, 4->6you should have ptr-operator ptr-operator ptr-operatorThen, 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:

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 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 ++函数返回函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 19:46
查看更多