本文介绍了尾随返回类型的占位符是否会覆盖初始占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

g ++似乎接受autodecltype(auto)的任何组合作为初始和尾随返回类型:

g++ appears to accept any combination of auto and decltype(auto) as initial and trailing return types:

int a;
auto f() { return (a); }                             // int
auto g() -> auto { return (a); }                     // int
auto h() -> decltype(auto) { return (a); }           // int&
decltype(auto) i() { return (a); }                   // int&
decltype(auto) j() -> auto { return (a); }           // int
decltype(auto) k() -> decltype(auto) { return (a); } // int&

但是,clang拒绝jk,说:错误:尾随返回类型的函数必须指定返回类型'auto',而不是'decltype(auto)'(演示).

However, clang rejects j and k, saying: error: function with trailing return type must specify return type 'auto', not 'decltype(auto)' (demonstration).

哪个编译器正确?在每种情况下应使用哪个规则(autodecltype(auto))?在 trailing-return-type 中使用占位符类型有意义吗?

Which compiler is correct? Which rule (auto or decltype(auto)) should be used in each case? And does it make any sense to use a placeholder type in a trailing-return-type?

推荐答案

auto.

§8.3.5[dcl.fct]/2

§8.3.5 [dcl.fct] /2

,声明T D1中包含的 declarator-id 的类型为"derived- declarator-type-list T",

and the type of the contained declarator-id in the declaration T D1 is "derived-declarator-type-list T",

T应该是单个 type-specifier 自动. [...]

T shall be the single type-specifier auto. [...]

另请参阅核心问题1852 与[dcl.spec.auto]/1矛盾.

See also Core Issue 1852 for the apparent contradiction with [dcl.spec.auto]/1.

这篇关于尾随返回类型的占位符是否会覆盖初始占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:01