The moving semantics of rvalue references

Source: Internet
Author: User

This article is translated from the right value reference interpretation of the classic article, if English is also possible, go directly to the original English. Thbecker.net/articles/rvalue_references/section_01.html

Rvalue references are an attribute in C + + and have settled into the C++11 standard, which may seem a bit difficult to understand when you first touch, but he is a very useful gadget.

An rvalue reference resolves two issues:

1.move semantics 2. Perfect forwarding.

Let's take a quick look at the move semantics, but before we introduce the move semantics, we need to introduce the meaning of Lvalue and rvalue first.

In C, we give the definition of an lvalue as: An lvalue is an expression E, so he can appear either to the left of the assignment operator or to the right. The right value is: Only the expression that appears to the right of the assignment operator is allowed.

int a = 42;int B = 43;//A B are both lvalue a = B; OKB = A; Oka = a * b; ok//A * b is the right value int c = A * b; OK, the right value appears at = right A * b = 42; Error, right value is not allowed to appear on = left

So in C + +, the definition of left and right values is similar, but there is a little change. In C + +, an lvalue is an expression that you can take an address (&) action on, and an expression that has a non-lvalue value.

 //Left value://  inti = the; I= +;//OK, I is the left value  int* p = &i;//OK, I is the left value  int&foo (); Foo ()= the;//OK, foo () is the left value  int* P1 = &foo ();//OK, foo () is the left value//Right Value://  intFoobar (); intj =0; J= Foobar ();//OK, Foobar () is the right value  int* P2 = &foobar ();//error, cannot take the address of the right-value expressionj = the;//OK, 42 is the right value

Let's say we have a class X, and he has a member function as a pointer, which may point to some resource. So the corresponding construction, destruction, copying and other operations are required to some special processing work.

such as our common std::vector, then the copy operation of X is similar to the following:

x& X::const & RHS) {  // [...]  //////// [...]}

Or you might encounter the following code:

X foo (); X x;x = foo ();

So the third sentence, the last sentence, is to perform three operations:

1. Release the original X resource.

2. Clone Foo Returns the resource in the value of the resource to X.

3. Destroy the resource in the release return value.

Obviously, this is a very inefficient practice, and it is wiser to swap operations, swap x and return value x ' Resources, and then the original X's resources will be freed automatically.

In other words, what we want to do is:

...
Swap (M_presource,rhs.m_presource);
...

This is called move semantics, and in c++11, this feature can be implemented by overloading:

x& X::operator= (<??? > rhs) {  // [...]  //// [...] }

So this??? What kind of a? Of course first it should be a reference type, reduce unnecessary copy of the value, and then we want it to be separate from the traditional reference type, because we want to call??? when the right operand is the right value. This overload, and when the right operand is the left value, call the original reference assignment construct, then, well, then we'll put??? It's called "rvalue reference," okay.

The moving semantics of rvalue references

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.