Detailed explanation of C++ lvalue, rvalue, and rvalue reference

Lvalue, rvalue
The simplest understanding: you can take the address of an expression to be an lvalue, otherwise it is an rvalue.

In C++11, all values ​​must belong to one of lvalues ​​and rvalues, and rvalues ​​can be subdivided into prvalues ​​and xvalues. In C++11, an lvalue that can take an address and has a name is an lvalue, and conversely, an rvalue (xvalue or prvalue) that cannot take an address and has no name. For example, int a = b+c, a is an lvalue, and its variable name is a, and the address of the variable can be obtained through &a; the return value of the expression b+c and the function int func() is an rvalue, We cannot find it by the variable name until it is assigned to a variable, and operations like &(b+c) will not compile.

Before understanding the rvalue
in C++11, let's take a look at the concept of rvalue in C++98: rvalue in C++98 is a prvalue, and a prvalue refers to a temporary variable value, a literal value not associated with an object. Temporary variables refer to function return values, expressions, etc. returned by non-reference, such as the return value of function int func(), expression a+b; literal values ​​not associated with objects, such as true, 2, "C", etc. .
C++11 extends rvalues ​​in C++98. In C++11, rvalues ​​are further divided into pure rvalues ​​(prvalue, Pure Rvalue) and xvalues ​​(eXpiring Value). The concept of pure rvalue is equivalent to our concept of rvalue in the C++98 standard, which refers to temporary variables and literal values ​​that are not associated with objects; the xvalue is a new addition to C++11. The value refers to the associated expression, such that the expression is usually the object to be moved (move for other use), such as the return value of a function returning an rvalue reference T&&, the return value of std::move, or a type conversion to T&& The return value of the function.
The xvalue can be understood as the value obtained by "stealing" the memory space of other variables. When ensuring that other variables are no longer used or are about to be destroyed, the release and allocation of memory space can be avoided by "stealing", which can prolong the life of variable values.

lvalue reference, rvalue reference
An lvalue reference is a type that refers to an lvalue. An rvalue reference is a type of reference to an rvalue. In fact, since an rvalue usually does not have a name, we can only find its existence by reference.
Both rvalue references and lvalue references are reference types. Whether an lvalue reference or an rvalue reference is declared, it must be initialized immediately. The reason for this can be understood as the reference type itself does not own the memory of the bound object, but an alias for the object. An lvalue reference is an alias for the value of a named variable, while an rvalue reference is an alias for an unnamed (anonymous) variable.
An lvalue reference cannot normally be bound to an rvalue either, but a const lvalue reference is a "catch-all" reference type. It can accept non-const lvalue, const lvalue, rvalue to initialize it. But an rvalue referenced by a const lvalue can only be read-only for the rest of its life. Conversely, non-const lvalues ​​can only accept non-const lvalues ​​to initialize them.

int &a = 2;       # 左值引用绑定到右值,编译失败
int b = 2;        # 非常量左值
const int &c = b; # 常量左值引用绑定到非常量左值,编译通过
const int d = 2;  # 常量左值
const int &e = c; # 常量左值引用绑定到常量左值,编译通过
const int &b =2;  # 常量左值引用绑定到右值,编程通过

An rvalue reference cannot usually be bound to any lvalue. To bind an lvalue to an rvalue reference, std::move() is usually required to coerce the lvalue to an rvalue, for example: +

int a;
int &&r1 = c;             # 编译失败
int &&r2 = std::move(a);  # 编译通过

The following table lists the types of values ​​that can be referenced by various reference types in C++11. It is worth noting that as long as the reference type of the rvalue can be bound, the lifetime of the rvalue can be extended.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324500692&siteId=291194637