What constitutes of RValues?

不问归期 提交于 2019-12-12 03:04:02

问题


RValues are things which are not maniputable regions of memory, so literals like integers are considered RValues.

  1. Do constants constitute RValues? const int x = 0; is maniputable at least one time.
  2. 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?


回答1:


Scalar rvalues are expressions that evaluate to scalar values like 42 or i + k (whereas scalar lvalues are expressions that evaluate to scalar objects like i or *int_ptr or numbers[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:

std::string foo()
{
    return "hello world";
}

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 expression foo() whose evaluation yields that result.




回答2:


  1. Do constants constitute RValues? const int x = 0; is maniputable at least one time.

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.

2.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?

Rvalue of a class type can either be modifiable or non-modifiable but rvalues of built-in types are always cv-unqualified.

§ 3.10/9 Class rvalues can have cv-qualified types; non-class rvalues always have cv-unqualified types.

Consider this example:

struct S {
     int x;
     void f(int s) { x = s; }
};

// ill-formed: Cannot assign to an rvalue of built-in type.
S().x = 42;

// Valid: A member function can be used to modify the referent of rvalue.
S().f(42);

The sub-expression S() creates an rvalue of class type whose lifetime is the end of ; full-expression.




回答3:


An lvalue refers to a memory location and we can take the address of that memory location using the & operator. An rvalue is an expression which is not an lvalue.

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




回答4:


  1. All variables, including nonmodifiable (const) variables, are lvalues. So x is an lvalue whereas 0 is an rvalue.

  2. Temporary objects are made rvalues since they do not exist beyond that particular statement.

You can read more here




回答5:


RValues are things which are not maniputable regions of memory

rvalues are expressions, not "things".

rvalues can refer to objects ("maniputable regions of memory").

so literals like integers are considered RValues.

Literals are rvalue (not "considered" rvalues) because C++ is defined that way.

Do constants constitute RValues?

const int x = 0;

Actually, x is a variable of type const int.

Given the above definition of x, the expression x is a lvalue.

Now, the temporary objects created by the compiler are also RValues

No, rvalues are expressions, not objects.

even when they have maniputable memory regions. Why is that so?

It isn't so.

Because they cannot be modified by "users"? Is this the reason?

rvalues are rvalues because the language is defined this way.

So, a memory region which is NOT maniputable by the "users" is called RValue?

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.

memory region which is NOT maniputable by the "users"

It isn't clear what you mean here; do you mean read-only memory, or something else?




回答6:


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




回答7:


RValues are things which are not maniputable regions of memory

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.



来源:https://stackoverflow.com/questions/8585924/what-constitutes-of-rvalues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!