C++11 left, right, and right value references

Source: Internet
Author: User

C++11 left value, right value, rvalue reference detailed left value, right value

All values in c++11 must be one of the left and right values, and the right value can be subdivided into a pure right value and a dead value. In the c++11 can take the address, has the name is the left value, conversely, cannot take the address, does not have the name is the right value (will the dead value or the pure right value). For example, int a = B+c, A is an lvalue, it has a variable named a, &a can get the address of the variable, the return value of the expression b+c, function int func () is an rvalue, and before it is assigned to a variable, we cannot find it by the variable name, and (B+c) Such operations are not compiled.

The right value, the dead value

Before you understand the right value of c++11, take a look at the concept of the right value in c++98: The right value in c++98 is the pure right value, and the pure right value refers to the value of the temporary variable, not the literal value associated with the object. The temporary variable refers to the return value of the function returned by the non-reference, expression, etc., such as the return value of the function int func (), the expression a+b, the literal value not associated with the object, such as true,2, "C", and so on.

C++11 extends the right value in the c++98. In C++11, the right value is divided into a pure right value (Prvalue,pure Rvalue) and a dead value (xvalue,expiring value). The concept of the pure right value is equivalent to our concept of the right value in the C++98 standard, which refers to the temporary variable and the literal value not associated with the object, and the dead value is the expression associated with the c++11 new rvalue reference, so that the expression is usually the object that will be moved (for him), such as returning an rvalue reference t& The function return value of &, the return value of Std::move, or the return value of the type conversion function converted to t&&.

The dead value can be understood as the value obtained by "stealing" other variable memory space. In order to ensure that other variables are no longer being used, or are about to be destroyed, the release and allocation of memory space can be avoided by "stealing", extending the lifetime of the variable values.
Lvalue Reference, rvalue reference

An lvalue reference is a type that references a left value. An rvalue reference is a type that refers to an rvalue, in fact, since the right value usually does not have a name, we can only find it by reference.

Both rvalue references and lvalue references are reference types. Whether you declare an lvalue reference or an rvalue reference, you must initialize it immediately. The reason can be understood to be that the reference type itself does not own the memory of the bound object, just an alias for that object. An lvalue reference is an alias for a named variable value, and an rvalue reference is an alias for an unnamed (anonymous) variable.

An lvalue reference is usually also not bound to an rvalue, but a constant lvalue reference is a "universal" reference type. It can accept a very important lvalue, a constant lvalue, and a right value to initialize it. However, the rvalue referenced by the constant Lvalue can only be read-only in its "rest". In contrast, a very-left value can only be initialized with a very left-hand value.

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

An Rvalue value reference typically cannot be bound to any lvalue, and to bind an lvalue to an rvalue reference, it is often necessary to std::move () to cast the lvalue to the right value, for example:

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

C++11 left, right, and right value references

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.