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
andk
说:错误:具有尾随返回类型的函数必须指定返回类型“auto”,而不是“decltype(auto)”(演示)。
哪个编译器是正确的?在每种情况下应使用哪个规则(auto
或)?在trailing-return-typedecltype(auto)
中使用占位符类型是否有意义?