GCC 的 decltype(auto) 不符合标准? [英] GCC's decltype(auto) doesn't conform to the standard?

查看:30
本文介绍了GCC 的 decltype(auto) 不符合标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 GCC 8.2 下用不同的选项编译这个 C++ 代码,它总是成功,不产生警告并输出 true:

I tried compiling this C++ code under GCC 8.2 with different options and it always succeeds, produces no warnings and outputs true:

int && a = 123;
decltype(auto) b = a;

std::cout << std::boolalpha << std::is_same<decltype(b), int&>::value;

同时,相同的代码不会在 Clang 中编译,如果我对标准的理解是正确的,那就是符合标准的行为.

Meanwhile, the same code will not compile in Clang, and if my understanding of the standard is correct, that's the standard-conforming behavior.

decltype 上的 cppreference:

cppreference on decltype:

如果参数是一个无括号的 id 表达式或一个无括号的类成员访问表达式,则 decltype 产生由此表达式命名的实体的类型.

If the argument is an unparenthesized id-expression or an unparenthesized class member access expression, then decltype yields the type of the entity named by this expression.

cppreference on decltype(auto):

cppreference on decltype(auto):

如果变量的声明类型是decltype(auto),关键字auto被其初始化器的表达式(或表达式列表)替换,实际类型根据decltype的规则推导出来.

If the declared type of the variable is decltype(auto), the keyword auto is replaced with the expression (or expression list) of its initializer, and the actual type is deduced using the rules for decltype.

因此,decltype(auto) 应该产生 int&&.并且由于a 是一个左值,它不应该绑定到b,从而导致编译错误.

Therefore, decltype(auto) should yield int&&. And since a is an lvalue, it should not bind to b, resulting in a compilation error.

那么 GCC 是不符合标准还是我遗漏了什么?

So does GCC fail to conform to the standard or is there something I'm missing?

推荐答案

你的推理是合理的.而且我想我看到 GCC 跳闸的地方了.

Your reasoning is sound. And I think I see where GCC is tripping.

decltype(auto) 的措辞表示 auto 在初始化程序中被替换为 表达式.根据 GCC,这意味着您的代码不等同于

The wording for decltype(auto) says that auto is replaced with the expression in the initializer. Which according to GCC would imply your code is not equivalent to

decltype(a) b = a;

而是等价于

decltype((a)) b = a;

但那是错误的.初始值设定项 一个无括号的id表达式",所以[dcl.type.simple]中的规则对于无括号的id表达式应该正常适用.b 的类型需要推导为int&&.

But that is wrong. The initializer is "an unparenthesized id-expression", so the rules in [dcl.type.simple] for unparenthesized id-expressions should apply normally. The type of b needs to be deduced as int&&.

由于 @Aconcagua 能够挖掘,这是一个 已知的 GCC 错误.

And as @Aconcagua was able to dig up, this is a known GCC bug.

这篇关于GCC 的 decltype(auto) 不符合标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆