g++ 似乎接受任意组合auto
and decltype(auto)
作为初始和尾随返回类型:
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拒绝了j
and k
,说:错误:具有尾随返回类型的函数必须指定返回类型“auto”,而不是“decltype(auto)” (示范 http://goo.gl/uw5h0b).
哪个编译器是正确的?哪个规则(auto
or decltype(auto)
) 应该在每种情况下使用?在中使用占位符类型是否有意义?尾随返回类型?
auto
引入时需要尾随返回类型.
§8.3.5 [dcl.fct] /2
在一份声明中T D
where D
有形式
D1 (
parameter-declaration-clause
) cv-qualifier-seq
opt
ref-qualifier
opt exception-specification
opt attribute-specifier-seq
opt trailing-return-type
以及所包含的类型声明符 ID在声明中T D1
是“派生-声明符类型列表 T
”,
T
应该是单一的类型说明符 auto. [...]
也可以看看核心问题 1852 http://open-std.org/jtc1/sc22/wg21/docs/cwg_toc.html#1852与 [dcl.spec.auto]/1 明显矛盾。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)