【发布时间】:2018-11-18 07:26:55
【问题描述】:
我尝试在 GCC 8.2 下使用不同的选项编译此 C++ 代码,它始终成功,不产生警告并输出 true:
int && a = 123;
decltype(auto) b = a;
std::cout << std::boolalpha << std::is_same<decltype(b), int&>::value;
同时,相同的代码在 Clang 中不会编译,如果我对标准的理解是正确的,那就是符合标准的行为。
decltype 上的 cppreference:
如果参数是不带括号的 id 表达式或不带括号的类成员访问表达式,则 decltype 产生由此表达式命名的实体的类型。
decltype(auto) 上的 cppreference:
如果变量的声明类型为decltype(auto),则关键字auto被替换为其初始化器的表达式(或表达式列表),并使用decltype的规则推导出实际类型。
因此,decltype(auto) 应该产生 int&&。并且由于a是一个左值,它不应该绑定到b,导致编译错误。
那么 GCC 不符合标准还是我缺少什么?
【问题讨论】:
-
VS 也失败了,所以你可能是对的。
-
我相信我最近在 CppCon 幻灯片中看到了这一点。
-
这被报告为bug #78209
标签: c++ gcc clang auto decltype