gpt4 book ai didi

c++ - 这个尾随返回类型在 C++11 中合法吗?

转载 作者:可可西里 更新时间:2023-11-01 17:57:28 32 4
gpt4 key购买 nike

以下内容:

auto (*f())() -> int;

在 C++11 模式下给出错误,Clang 说:

error: 'auto' return without trailing return type; deduced return types are a C++14 extension

但在 C++14 模式下编译。 GCC 在使用 -Wall -Wextra -pedantic 的两种模式下编译时都不会报错。

n3337 7.1.6.4/2 说:

The auto type-specifier may appear with a function declarator with a trailing-return-type (8.3.5) in any context where such a declarator is valid.

8.3.5p2 讨论了函数声明符,但我太缺乏经验无法理解。谁能解释一下它在 C++11 中是否合法?

最佳答案

这部分包含在 CWG 1725 中:

The treatment of a declaration like the following is not clear:

auto (*f())() -> int; // #1

8.3.5 [dcl.fct] paragraph 2 appears to require determining the type of the nested declarator

auto (*f()); // #2

which, because it does not have a trailing-return-type, would be ill-formed by (C++11) 7.1.6.4 [dcl.spec.auto]. (In C++14, an auto return type without a trailing-return-type is, of course, permitted.)

Rationale (September, 2013): The intent of the C++11 wording is that the requirement for a trailing return type applies only at the top level of the declarator to which auto applies, not to each possible recursive stage in the declarator processing. [..]

此外,根据[dcl.fct]/2 ,

In a declaration T D where D has the form

     D1 ( parameter-declaration-clause ) [...] trailing-return-type

and the type of the contained declarator-id in the declaration T D1 is “derived-declarator-type-list T, T shall be the single type-specifier auto.

如 DR 中所述,T D1auto (*f()) ,其类型为“() 的函数返回指向 auto 的指针”(即符合要求)。因此,您的代码在 C++11 和 14 以及 f 中均有效的类型是“() 的函数返回指向() 的函数返回int 的指针”。

关于c++ - 这个尾随返回类型在 C++11 中合法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37493769/

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