如果通过 const 引用延长了它的生命周期,那么这个右值在哪里?
Where is this rvalue residing if its lifetime is prolonged by const reference?
我已经通读了这个问题:
Where is rvalue stored in C
并根据答案:
int main()
{
int a = 7; // 7 rvalue is stored in the program binary and assigned to "a"
int b = 7 * rand(); // 7 is pulled from the program binary, multiplied by result of rand and assigned to "b"
// But...
const int& c = 3; // 3 is an rvalue?
const_cast<int&>(c) = 1; // rvalues don't have storage space? But I'm assigning to it.
std::cout << c; // Prints 1
}
我想了解,通常右值临时(等号右侧)在内存中没有存储 space 并且其生命周期在行尾结束。如果我们将一个 const 引用绑定到它,那么它会将右值的生命周期延长到它所绑定的引用的生命周期。在分配给数字文字“3”的情况下,这是否意味着它确实获得了一些存储空间 space 而不是右值?还是右值?
另外我想知道我所做的是否是未定义的行为,无论 const_cast 是不可取的还是其他什么。
normally a rvalue temporary (right side of the equals sign) doesn't have storage space in memory
正常情况下是有存储的space,你应该是理解错了一些概念吧
In the case to assigning to "3", a number literal, does this mean it does get some storage space and isn't an rvalue? Or is an rvalue?
两者兼而有之:它是一个右值并且它有一个存储space。栈上有a space分配给store和整数,值为3
,然后c
绑定到这个变量。
关于您的代码:
const int& c = 3; // 3 is an rvalue?
是的,是右值。
const_cast<int&>(c) = 1; // rvalues don't have storage space? But I'm assigning to it.
std::cout << c; // Prints 1
来自const_cast:
const_cast makes it possible to form a reference or pointer to
non-const type that is actually referring to a const object or a
reference or pointer to non-volatile type that is actually referring
to a volatile object. Modifying a const object through a non-const
access path and referring to a volatile object through a non-volatile
glvalue results in undefined behavior.
我已经通读了这个问题:
Where is rvalue stored in C
并根据答案:
int main()
{
int a = 7; // 7 rvalue is stored in the program binary and assigned to "a"
int b = 7 * rand(); // 7 is pulled from the program binary, multiplied by result of rand and assigned to "b"
// But...
const int& c = 3; // 3 is an rvalue?
const_cast<int&>(c) = 1; // rvalues don't have storage space? But I'm assigning to it.
std::cout << c; // Prints 1
}
我想了解,通常右值临时(等号右侧)在内存中没有存储 space 并且其生命周期在行尾结束。如果我们将一个 const 引用绑定到它,那么它会将右值的生命周期延长到它所绑定的引用的生命周期。在分配给数字文字“3”的情况下,这是否意味着它确实获得了一些存储空间 space 而不是右值?还是右值?
另外我想知道我所做的是否是未定义的行为,无论 const_cast 是不可取的还是其他什么。
normally a rvalue temporary (right side of the equals sign) doesn't have storage space in memory
正常情况下是有存储的space,你应该是理解错了一些概念吧
In the case to assigning to "3", a number literal, does this mean it does get some storage space and isn't an rvalue? Or is an rvalue?
两者兼而有之:它是一个右值并且它有一个存储space。栈上有a space分配给store和整数,值为3
,然后c
绑定到这个变量。
关于您的代码:
const int& c = 3; // 3 is an rvalue?
是的,是右值。
const_cast<int&>(c) = 1; // rvalues don't have storage space? But I'm assigning to it.
std::cout << c; // Prints 1
来自const_cast:
const_cast makes it possible to form a reference or pointer to non-const type that is actually referring to a const object or a reference or pointer to non-volatile type that is actually referring to a volatile object. Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior.