gpt4 book ai didi

c++ - 右值引用的生命周期

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:39:35 24 4
gpt4 key购买 nike

我想我在理解右值引用方面有问题。这种结构的真正使用生命周期和用途是什么。

int&& value = 5;

如果我理解正确,5 是右值对象(我不能获取它的地址)并且它是临时的 - 生命周期是当前表达式的结尾。将其分配给右值引用是否会以某种方式延长生命周期?如果是,对象的新生命周期是多少?

最佳答案

Does assigning that to rvalue reference somehow prolong lifetime?

是的。 Rvalue references可用于延长临时对象的生命周期(注意,lvalue referencesconst 也可以延长临时对象的生命周期,但不能通过它们修改)。因此:

// both will extend the lifetime of the temporary
int&& value = 5; // modifiable
const int& value = 5; // non-modifiable

If yes, what is the new lifetime of object?

临时对象的生命周期被延长以匹配引用的生命周期。参见 lifetime of a temporary.

关于c++ - 右值引用的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54047228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com