C++11 one of the moving semantics (basic concept)

Source: Internet
Author: User

Summary

Moving semantics is one of the new features of c++11, and the moving semantics can be used to move objects rather than copies. In some cases, performance can be significantly improved. This article introduces some of the basic concepts in c++11 mobility semantics.

An expression

An expression is made up of one or more operands, and the expression evaluates to a result, the literal value and the variable are the simplest expressions, and the result is the values of literals and variables. Combining an operator with one or more operands can produce more complex expressions.

Left and right values

Lvalue: An expression that is able to get an address is a left-hand value. For example, a common variable is a left-hand value. Also, for example, the assignment expression in general is an lvalue (so it is generally required that the overloaded assignment operator in the class returns *this).

Right value: An expression that cannot be addressed is an rvalue. For example, a function's non-reference return value or literal constant. (It is easy to understand that the right value is a temporary variable that has no name but is actually created).

class a{}; // A is the left value A; // Geta function returns the right value a Geta () {    return  A ();} void SetA (a A) {} // argument A () is Rvalue SetA (A ())
Rvalue reference

Reference: The alias of the variable. The following points: 1) The operation of the reference is exactly the same as for the variable; 2) The reference must be initialized when it is defined and cannot be bound to other objects.

Lvalue Reference: A regular reference that can only be bound to an lvalue (some compilers can bind to an rvalue, see example below).

Rvalue Reference: A reference that must be bound to a right value. Since the right value is typically an object that is temporarily created and is about to be destroyed, an rvalue reference can extend the life cycle of the rvalue until the variable of the Rvalue reference type goes out of scope, and the Rvalue reference type's variable is destroyed while the referenced right value is destroyed.

Note here: The type of the variable and whether the variable itself is an lvalue or an rvalue is not related (as long as the variable is an lvalue, because it has a name, you can take the address.) For example, it is strange that you cannot bind an rvalue reference to a variable of an rvalue reference type, but this is actually true:

// A is a variable of the Rvalue reference type a&& a = geta (); // error, A is an lvalue, you cannot bind an lvalue to an rvalue reference a&& B = A;

As mentioned above, some compilers can bind rvalue to Lvalue reference above, in vs2015,

//vs no error in 2015, bind rvalue to Lvalue reference above a& a = Geta ();

In g++5.3.1, the above code will error, and you cannot bind an rvalue to an lvalue reference.

It can be seen that for rvalue binding to lvalue references, the requirements for different compilers are not the same, so you don't have to pay too much attention to these details. The most important usage scenario for rvalue is the movement of objects, which is described in the next article.

Reference

C + + Primer (5th edition)

C++11 one of the moving semantics (basic concept)

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.