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

查看:141
本文介绍了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.

decltype(auto)上的cppreference:

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 能够进行挖掘,所以这是

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

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

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