将文字常量分配给右值引用时会发生什么? [英] What happens when you assign a literal constant to an rvalue reference?
问题描述
这当然是一个挑剔的问题,主要是出于好奇心.假设我们有以下内容:
This is admittedly a nit-picky question that is mainly driven by curiosity. Suppose we have the following:
int x = 5;
int&& xref = std::move(x);
std::cout << "Before assignment x: " << x << std::endl;
std::cout << "Before assignment xref: " << xref << std::endl;
xref = 10;
std::cout << "After assignment x: " << x << std::endl;
std::cout << "After assignment xref: " << xref << std::endl;
预期的输出是:
// Before assignment x: 5
// Before assignment xref: 5
// After assignment x: 10
// After assignment xref: 10
这很有道理. std::move
将x
转换为xvalue,并允许我们将其内存位置绑定到xref
并相应地修改其内容.现在说我们有以下内容:
This makes sense. std::move
converts x
to an xvalue and allows us to bind its memory location to xref
and modify its contents accordingly. Now lets say we have the following:
int&& xref = 5;
std::cout << "Before assignment xref: " << xref << std::endl;
xref = 10;
std::cout << "After assignment xref: " << xref << std::endl;
int x = 5;
std::cout << "After assignment x: " << x << std::endl;
输出直观:
// Before assignment xref: 5
// After assignment xref: 10
// After assignment x: 5
这具有总体意义.我们希望能够将常量文字5
绑定到xref
,因为5
是prvalue.我们还希望xref
是可变的.我们进一步期望常量文字5
的值是不可修改的(如上述代码片段的最后两行中有些书呆子所示).
This make overall sense. We expect to be able to bind the constant literal 5
to xref
because 5
is a prvalue. We also expect that xref
be mutable. We further expect that the value of constant literal 5
isn't modifiable (as shown somewhat pedantically in the last two lines of the above snippet).
所以我的问题是,这到底是怎么回事? C ++如何知道不修改常量文字5
的值,又如何为xref
保持足够的身份才能知道赋值已将其更改为10
.当绑定到常量文字时,是否在分配给xref
后创建新变量?在C ++ 03中,这个问题从未出现过,因为只有const引用可以绑定到右值.
So my question is, what exactly is going on here? How does C++ know not to modify the value of the constant literal 5
yet maintain sufficient identity for xref
to know that it's been changed to 10
by the assignment. Is a new variable being created upon assignment to xref
when its bound to a constant literal? This question never came up in C++03 since only const references could be bound to rvalues.
推荐答案
构造一个临时变量,该临时变量根据文字的值进行初始化,并且其持续时间与引用的时间一样长.您可以使用此对象执行自己喜欢的操作.
A temporary is constructed, initialised from the value of the literal, and it lasts as long as the reference. You can do what you like with this object.
就生命周期而言,这与您编写const int& x = 5
一样;只是,在那里,您正在使用自动创建的临时对象这一事实被掩盖了,因为const
阻止您通过突变对其进行证明.
In terms of lifetime, this is just the same as if you'd written const int& x = 5
; only, there, the fact that you're working with an automatically-created temporary object is masked because the const
prevents you from proving it with a mutation.
[C++14: 8.5.3/5]:
[..] 如果T1
是非类类型,则会创建类型为" cv1T1
"的临时文件,并复制-从初始值设定项表达式初始化(8.5).然后将引用绑定到临时目录. [..]
[C++14: 8.5.3/5]:
[..] IfT1
is a non-class type, a temporary of type "cv1T1
" is created and copy-initialized (8.5) from the initializer expression. The reference is then bound to the temporary. [..]
这篇关于将文字常量分配给右值引用时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!