C ++: On the right reference value

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/tonglin12138/article/details/91479048

1. What is the left and right values?

       C / C ++ language may be placed on the left of the variable assignment symbol, i.e. having a corresponding memory cell can be accessed by the user, and can amount to change its value by the user. Left value represents an object stored in the computer memory, rather than the result of a constant or calculated. Or left value is representative of a memory address value, and can be read by the memory address of the memory and write (mainly write) operation; this is why the left can be assigned a value. There are values corresponding to the right: When a symbol or constants in the right side of the operator, the computer will read their "right value", that is the true value of their representatives.       
       In simple terms is that the left is equivalent to the value of the address value, the right value corresponds to a data value . The right value refers to a reference to the data stored in a memory in the address.

Left value rvalue Translation:
       The L-value L refers Location, showing addressable. AValue (OF S. Computer) that has AN address.
       R-value of R refers Read, represents read. in computer science, a value that does not have an address in a computer language.
       Left and right values are relative to the terms of the assignment expression. Left value is to appear on the left side of an assignment expression. Left value expression can be divided into a left value and a value of the read-only and writable left. The right value can be found in the expression assignment expression on the right, he can not occupy temporary or literal amount of memory space, the space may be an entity that does not have the right to write. Such as

int a=3;
const int b=5;
a=b+2; //a是左值,b+2是右值
b=a+2; //错!b是只读的左值但无写入权,不能出现在赋值符号左边
(a=4+=28; //a=4是左值表达式,28是右值,+=为赋值操作符
34=a+2; //错!34是字面量不能做左值

2. rvalue references

       In order to support the moving operation, c ++ new standard introduces a new reference type - rvalue references. The so-called rvalue reference is to the right must be bound to a reference value. Let's get the right value referenced by && instead of &. As we shall see, there is an important reference value of the right of property - only bind to a target to be destroyed. Therefore, we are free to be a reference to the value of the right resources, "move" to another object.

       In general, a left expression value represents the identity of an object, but an rvalue expression represents the value of the object.

for example:

int i=42;
int &r=i;   //正确,r引用i
int &&rr=i   //错误,不能将一个右值引用绑定到一个左值上
int &r2=i*42;  //错误,i*42是一个右值
const int &r3=i*42;  //正确,我们可以将一个const的引用绑定到一个右值上
int &&r2=i*42; //正确,将rr2绑定到乘法结果上

1. Left lasting value, the value of the right short
       left lasting value of the state, and the right value either literal constants, either temporary object expression evaluation process created.
Since rvalue references can be bound to a temporary object, we learned that
the object referenced by 1 to be destroyed
2 ,. the object no other user

These two features mean: Code using the right values ​​quoted are free to take over the resources referenced object.

2. The value of the variable is left
       variable can be seen as only one operand operator without expression, though we rarely treated as such variables. Any other similar expressions, variable expressions have left value / right value property. Variable expression values are left, the result brought about is that we can not bind an rvalue reference to a reference value on the right type of variable .

int &&rr1 =42;  //正确,字面值常量是右值
int &&r2 =rr1;   //错误,表达式rr1是左值!

Note: The value of the variable is left, so we can not directly bind an rvalue reference to a variable, even if the value of this variable is the right reference types can not.

3. move the standard library function
       , although not a direct reference rvalue left bound to a value, but we can explicitly convert a value corresponding to the left and right values of reference type. We can get bound to the right on the left a new reference value of the standard library functions called the move by calling this function is defined in the header file utility.

int &&rr3 =std::move(rr1);  //OK

       move calls to tell the compiler: We have a value left, but we hope the same value as the right to deal with it. We must recognize, call the move would mean commitment: In addition to rr1 assignment or destroyed beyond, we will no longer use it. After a call to move, we can not make any assumptions about the value of the source object backward .

Note:
1. We can destroy a shift after the source object, you can also give it a new value, but you can not use a value of the source object shift.
2. For move should be std: move and not move. To do so to avoid potential name conflicts.

Guess you like

Origin blog.csdn.net/tonglin12138/article/details/91479048