RValues 由什么构成?
RValue 是不可操作的内存区域,因此像整数这样的文字被视为 RValue。
- 常数构成 RValue 吗?
const int x = 0;
至少可操作一次。 - 现在,编译器创建的临时对象也是 RValue,即使它们具有可操作的内存区域。为什么会这样?
因为它们不能被“用户”修改?是这个原因吗?
那么,“用户”不可操作的内存区域称为 RValue 吗?
RValues are things which are not maniputable regions of memory, so literals like integers are considered RValues.
- Do constants constitute RValues?
const int x = 0;
is maniputable at least one time. - Now, the temporary objects created by the compiler are also RValues even when they have maniputable memory regions. Why is that so?
Because they cannot be modified by "users"? Is this the reason?
So, a memory region which is NOT maniputable by the "users" is called RValue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先 - lval 和 rval 是表达式的属性 - 表达式可以是 rval 或 lval。
要回答您的另一个问题,可以通过 rval 引用来修改由 rvals 创建的临时 - 此功能是在 c++11 中添加的此功能
对于 1) 应用移动语义 2) 完美转发非常有用。
有关详细信息,请访问 http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx
first of all - lval and rval are properties of expressions - an expression is either an rval or an lval.
To answer your other question, temporaries created by rvals can be modified by having a rval reference - this feature is added in c++11
This feature is very useful for 1) applying move semantics 2 ) perfect forwarding.
Read more about it at http://blogs.msdn.com/b/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx
。 错误。右值绝对是可变的。在 C++03 中这样做是一件令人头疼的事情,但可以合法地完成 - 在 C++11 中这是非常常见的。
右值是产生即将消亡的东西的表达式。这是他们的定义属性。因此,您可以使用 C++11 的右值引用来窃取他们的资源而无需担心,或者如果您绝望的话,可以在 C++03 中“交换”。这使得它们在许多情况下更加正确,并且在很多很多情况下更快。
Wrong. Rvalues absolutely are mutable. In C++03 doing this is a headache, but can be done legally- in C++11 it's extremely common.
Rvalues are expressions that yield things which are about to die. That's their defining property. Therefore, you can steal their resources without worrying about it, using C++11's rvalue references, or if you're desperate, "swaptimizing" in C++03. This makes them more correct in many situations, and much faster in many, many circumstances.
标量右值是计算为标量值的表达式,如
42
或i + k
(而标量左值是计算为标量对象的表达式,如i
或*int_ptr
或numbers[0]
)。类类型的右值是计算结果为临时对象的表达式。最突出的例子是调用按值返回类对象的函数:
给定上述函数定义,表达式 foo() 是一个右值。请注意,我不是在谈论结果(这是一个临时对象),而是在谈论表达式
foo()
,其求值会产生该结果。Scalar rvalues are expressions that evaluate to scalar values like
42
ori + k
(whereas scalar lvalues are expressions that evaluate to scalar objects likei
or*int_ptr
ornumbers[0]
).rvalues of class type are expressions that evaluate to temporary objects. The most prominent example is the call of a function that returns a class object by value:
Given the above function definition, the expression
foo()
is an rvalue. Note that I'm not talking about the result (which is a temporary object) but the expressionfoo()
whose evaluation yields that result.在您的声明中,
x
既不是右值也不是左值,它称为declarator-id。 (参见 C++03 中的语法 8/4)当它在(子)表达式中使用时,它是一个不可修改的左值,可以初始化为任何常量表达式。类类型的右值可以是可修改的,也可以是不可修改的,但内置类型的右值始终是 cv 未限定的。
考虑这个例子:
子表达式
S()
创建一个类类型的右值,其生命周期是 ; 的末尾。 完整表达。In your declaration,
x
is neither an rvalue or lvalue, it is called a declarator-id. (See grammar of 8/4 in C++03) When it is used in a (sub-)expression it is a non-modifiable lvalue which can be initialized to any constant expression.Rvalue of a class type can either be modifiable or non-modifiable but rvalues of built-in types are always cv-unqualified.
Consider this example:
The sub-expression
S()
creates an rvalue of class type whose lifetime is the end of ; full-expression.lvalue
指的是内存位置,我们可以使用&
运算符获取该内存位置的地址。右值
是一个不是左值
的表达式。<代码>1。常数构成 RValue 吗?常量 int x = 0;至少可操作一次。
不,因为你这样做 -
const int *p = &x
An
lvalue
refers to a memory location and we can take the address of that memory location using the&
operator. Anrvalue
is an expression which is not anlvalue
.1. Do constants constitute RValues? const int x = 0; is maniputable at least one time.
No, because you an do this -
const int *p = &x
所有变量,包括不可修改(const)变量,都是左值。因此 x 是左值,而 0 是右值。
临时对象被设为右值,因为它们在特定语句之外不存在。
您可以在此处阅读更多信息
All variables, including nonmodifiable (const) variables, are lvalues. So x is an lvalue whereas 0 is an rvalue.
Temporary objects are made rvalues since they do not exist beyond that particular statement.
You can read more here
右值是表达式,而不是“事物”。
右值可以引用对象(“可操作的内存区域”)。
文字是右值(不是“被视为”右值)因为 C++ 就是这样定义的。
实际上,
x
是一个const int
类型的变量。给定上述 x 的定义,表达式 x 是左值。
不,右值是表达式,不是对象。
事实并非如此。
右值是右值,因为语言是这样定义的。
不,右值仅指定表达式,而不是像内存这样运行时存在的东西。
右值有时可以引用一个对象,它是用户可以操作的“内存区域”。
不清楚您在这里的意思;你的意思是只读存储器,还是其他什么?
rvalues are expressions, not "things".
rvalues can refer to objects ("maniputable regions of memory").
Literals are rvalue (not "considered" rvalues) because C++ is defined that way.
Actually,
x
is a variable of typeconst int
.Given the above definition of
x
, the expressionx
is a lvalue.No, rvalues are expressions, not objects.
It isn't so.
rvalues are rvalues because the language is defined this way.
No, rvalue only designate expressions, not things that exist at runtime like memory.
A rvalue can sometimes refer to an object, which is a "memory region" that the user can manipulate.
It isn't clear what you mean here; do you mean read-only memory, or something else?