Translation "c++ Rvalue References explained"c++ rvalue Reference detailed Part1: overview

Source: Internet
Author: User

This article is about "C + + Rvalue References explained" The translation of the text, the original Thomas Becker.

This article explained in detail the role of c++11 rvalue reference and the significance of the occurrence, but also by Scott Meyers Recommendation, the full text is divided into 11 parts, I will use spare time, respectively translated.

Limited by the author's level, may be described a few problems, but also hope to correct.

Some nouns are not translated into Chinese in order to maintain the meaning and convenience of understanding, and some are given common Chinese translation in parentheses.

Directory
    1. Overview
    2. Move semantics
    3. Rvalue reference
    4. Force move semantics
    5. Is the rvalue reference the right value?
    6. Move semantics and compiler optimizations
    7. Perfect Forwarding (Perfect forwarding): problem
    8. Perfect Forwarding (Perfect Forwarding): the solution
    9. Rvalue References and exceptions
    10. Implicit move situation
    11. Acknowledgements and in-depth reading
Overview

Rvalue references are a C + + feature that is introduced with the C++11 standard. What makes rvalue references harder to understand is that when you first touch it, it's harder to figure out what it's for, or what kind of problem it's going to solve. Therefore, I will not directly explain what an rvalue reference is. Instead, I'll start with the problem that's going to be solved, and then show how to solve these problems by using rvalue references. In this way, the definition of the Rvalue reference will be presented to you fairly and naturally.

An rvalue reference will address at least the following two issues:

    1. Implements the move semantics (aka. Move semantics)
    2. Perfect Forwarding (Perfect forwarding)

If you are not familiar with these issues, don't worry. They will be explained in more detail below. Let's start with the move semantics. But before I start, I need to remind you of what is Lvalue (lvalues) and Rvalue (rvalues) in C + +. It is very difficult to give a rigorous definition, but the following explanations are sufficient for the purpose.

The original definition of lvalue and rvalue is like this in earlier C: an lvalue is an expression e that may appear on the left or right hand side of an assignment. The right value is an expression that can only appear on the right-hand side of an assignment. Example:

 int  a = 42   int  b = 43   //  A and B are lvalue s:  a = b; //  OK  B = A; //  OK  A = a * b; //  OK  //  A * b is the right value:  int  C = A * b; //  OK, right value on the right-hand side of the assignment  A * b = 42 ; //  

In C + +, this is still available initially, and is an intuitive way to identify Lvalue and rvalue. However, as C + + and its user-defined types introduce subtle changes in variability (modifiability) and transferability (assignability), this definition is no longer correct. We do not need to delve into this issue any further. Next is another definition of this, though it may still not stand up to scrutiny, but it allows you to handle rvalue references: An lvalue is an expression that points to a memory address, and allows us to get that memory address through the & operator. An rvalue is an expression that is not a left-hand value . Examples are as follows:

//Left value://inti = the; I= +;//OK, I is the left valueint* p = &i;//OK, I is the left valueint&foo (); foo ()= the;//OK, foo () is the left valueint* P1 = &foo ();//OK, foo () is the left value//Right Value://intfoobar ();intj =0; J= Foobar ();//OK, Foobar () is the right valueint* P2 = &foobar ();//error, cannot be taken from the right valuej = the;//OK, 42 is the right value

If you are interested in rigorous rvalue and lvalue definitions, Mikael Kilpeläinen's ACCU article is a relatively good start on this subject.

Translation "c++ Rvalue References explained"c++ rvalue Reference detailed Part1: overview

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.