是否应该将结构化绑定的decltype(auto)用作引用? [英] Is decltype(auto) for a structured binding supposed to be a reference?

查看:96
本文介绍了是否应该将结构化绑定的decltype(auto)用作引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个例子:

#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.

ab都是int&&.

但是decltype(auto)的实际行为方式来自

But the way decltype(auto) actually behaves comes from [dcl.type.auto.deduct]:

如果占位符是decltype(auto)类型说明符,则T应仅是占位符.像[dcl.type.simple]中所述确定为T推导的类型,就好像edecltype的操作数一样.

If the placeholder is the decltype(auto) type-specifier, T shall be the placeholder alone. The type deduced for T is determined as described in [dcl.type.simple], as though e had been the operand of the decltype.

这种措辞确实很尴尬,但最终:

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屋!

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