是否应该将结构化绑定的decltype(auto)用作引用? [英] Is decltype(auto) for a structured binding supposed to be a reference?
问题描述
考虑一个例子:
#include <iostream>
#include <type_traits>
#include <tuple>
int main() {
auto tup = std::make_tuple(1, 2);
auto [ a, b ] = tup;
decltype(auto) e = a;
std::cout << std::boolalpha << std::is_reference_v<decltype(e)> << std::endl;
}
c (输出:false
)和此问题与解答 e
应该作为参考还是gcc错误?还是代码格式错误?
clang (output: false
) and gcc (output: true
) are disagreeing in this simple case. Having in mind e.g. this Q&As should the e
be a reference or is it a gcc bug? Or maybe the code is ill-formed?
推荐答案
标识符自己是参考.来自 [dcl.struct.bind]/3 :
鉴于由
std::tuple_element<i, E>::type
指定的T i 类型,每个v i 是初始化为对T i 的引用"类型的变量对于初始化程序,如果初始化程序是左值,则引用是左值引用;否则,右值引用;引用的类型为T i .
Given the type Ti designated by
std::tuple_element<i, E>::type
, each vi is a variable of type "reference to Ti" initialized with the initializer, where the reference is an lvalue reference if the initializer is an lvalue and an rvalue reference otherwise; the referenced type is Ti.
即a
和b
都是int&&
.
But the way decltype(auto)
actually behaves comes from [dcl.type.auto.deduct]:
如果占位符是
decltype(auto)
类型说明符,则T
应仅是占位符.像[dcl.type.simple]中所述确定为T
推导的类型,就好像e
是decltype
的操作数一样.
If the placeholder is the
decltype(auto)
type-specifier,T
shall be the placeholder alone. The type deduced forT
is determined as described in [dcl.type.simple], as thoughe
had been the operand of thedecltype
.
这种措辞确实很尴尬,但最终:
This wording is really awkward, but ultimately:
decltype(auto) e = a;
~~~~~~~~~~~~~~
表示:
decltype( a ) e = a;
~~~~
和decltype(a)
的意思是,来自 [dcl.type.simple]/4.1 :
如果
e
是命名结构化绑定([dcl.struct.bind])的未括号化的 id-expression ,则decltype(e)
是所引用类型在结构化绑定声明的规范中;
if
e
is an unparenthesized id-expression naming a structured binding ([dcl.struct.bind]),decltype(e)
is the referenced type as given in the specification of the structured binding declaration;
a
的引用类型为int
,因此e
必须为int
.这意味着它不是参考,并且clang是正确的.提起 81176 .
The referenced type of a
is int
, so e
must be an int
. Which means it's not a reference, and clang is correct. Filed 81176.
这篇关于是否应该将结构化绑定的decltype(auto)用作引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!