【发布时间】:2014-08-07 05:42:18
【问题描述】:
g++ 似乎接受 auto 和 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 和 k,说:错误:具有尾随返回类型的函数必须指定返回类型 'auto',而不是 'decltype(auto)' (demonstration )。
哪个编译器是正确的?在每种情况下应使用哪条规则(auto 或 decltype(auto))?在 trailing-return-type 中使用占位符类型是否有意义?
【问题讨论】:
标签: c++ decltype c++14 trailing-return-type return-type-deduction