结构化绑定中的const引用是否会延长分解对象的寿命? [英] Do const references in structured bindings extend the lifetime of the decomposed object?

查看:87
本文介绍了结构化绑定中的const引用是否会延长分解对象的寿命?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否编写 const auto& [a,b] = f(); 确保延长从 f()返回的对象或至少对象 a b 是否绑定到?阅读该提案,我看不到任何明显的内容除非另有说明,否则请确保使用该语言。但是,以下内容不会延长临时文件的生存期,因此我看不到它将如何涵盖:

Does writing const auto& [a, b] = f(); guarantee extending the lifetime of the object returned from f(), or at least the objects a and b are bound to? Reading through the proposal I don't see anything obvious in the language to make me sure that it does unless it's just covered by something else. However, the following doesn't extend the lifetime of the temporary, so I don't see how it would be covered:

const auto& a = std::get<0>(f());

在本文的顶部,似乎表明已将其覆盖

At the top of the paper it seems to suggest that it is covered


分解声明的cv-qualifier和ref-qualifier适用于为初始化程序引入的引用,而不是单个成员别名

the cv-qualifiers and ref-qualifier of the decomposition declaration are applied to the reference introduced for the initializer, not for the individual member aliases

但是在实际标准的拟议措词中,我看到的最接近的提法如下,尽管我不确定如何阅读以获得保证。 m寻找:

But in the proposed wording for the actual standard, the closest mention I see is below, though I'm not sure how to read it to get the guarantee I'm looking for:


如果e是未括号的id表达式,其命名为左值或引用是从标识符列表中引入的
分解声明,
decltype(e)是
分解声明中指定的引用类型

if e is an unparenthesized id-expression naming an lvalue or reference introduced from the identifier-list of a decomposition declaration, decltype(e) is the referenced type as given in the specification of the decomposition declaration

似乎gcc和clang都可以根据 wandbox实验更丑陋的实现了我自己的类型的所有细节,似乎可以延长外部对象及其其他数据的寿命

It seems that gcc and clang both extend the lifetime of the object returned until the end of the scope based on a wandbox experiment. An uglier one implementing all the bells and whistles for my own type seems to extend the lifetime of the outer object and its other data members.

尽管几乎可以肯定作者的意图,但我还是想知道这种语言可以保证这是安全的。

Though almost certainly the authors' intent(s), I'd like to know for sure that the language guarantees this is safe.

推荐答案

是。诀窍是要认识到,尽管看起来很漂亮,但 [之前的结构化绑定声明的部分仍不适用于 identifier-list 中的名称。 em>。相反,它们适用于声明隐式引入的变量。 [dcl.struct.bind] / 1

Yes. The trick is to realize that despite the appearance, the portion of a structured binding declaration before the [ doesn't apply to the names in the identifier-list. They apply instead to the variable introduced implicitly by the declaration. [dcl.struct.bind]/1:


首先,引入具有唯一名称 e 的变量。如果 initializer 中的
assignment-expression 具有数组类型 A 并且没有 ref-限定符存在, e 具有类型 cv A ,并且每个元素都已复制初始化或直接初始化来自 assignment-expression 的相应元素
,由
initializer 的形式指定。否则, e 的定义如下:

First, a variable with a unique name e is introduced. If the assignment-expression in the initializer has array type A and no ref-qualifier is present, e has type cv A and each element is copy-initialized or direct-initialized from the corresponding element of the assignment-expression as specified by the form of the initializer. Otherwise, e is defined as-if by

attribute-specifier-seq < sub> opt decl-specifier-seq ref-qualifier opt e initializer ;

其中,声明永远不会解释为函数声明
以及声明中除 declarator-id 是从相应的结构化绑定声明中提取的

where the declaration is never interpreted as a function declaration and the parts of the declaration other than the declarator-id are taken from the corresponding structured binding declaration.

然后将名称定义为可以是 e 元素的别名,也可以是绑定到对调用 get 的结果的引用e

The names are then defined to either be aliases for the elements of e or references bound to the result of calling get on e.

在您的示例中,就好像通过(假定 f 返回两元素 std :: tuple ):

In your example, it's as if by (assuming that f returns a two-element std::tuple):

const auto& e = f(); // 1
using E = remove_reference_t<decltype((e))>;
std::tuple_element<0, E>::type& a = get<0>(e);
std::tuple_element<1, E>::type& b = get<1>(e);

(除了 decltype(a) decltype(b)得到特殊的处理来隐藏它们的引用。)

(Except that decltype(a) and decltype(b) gets the special treatment to hide their referenceness.)

第#行应该很明显1确实延长了 f 返回值的寿命。

It should be pretty obvious that line #1 does extend the lifetime of f's return value.

这篇关于结构化绑定中的const引用是否会延长分解对象的寿命?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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