New features in C ++ 11: rvalue Reference (right value Reference)

Source: Internet
Author: User

New features in C ++ 11: rvalue Reference (right value Reference)

The right value reference allows us to distinguish between the left and right values of an expression.

C ++ 11 introduces the concept of right value reference, so that we can bind the reference to the right value. Use two "Get address symbols ":

int&& rvalue_ref = 99;

Note that only the left value can be paid to the reference, for example:

int& ref = 9;   

We will get this error: "invalid initialization of non-const reference of type int & from an rvalue of type int"

We can only do this:

int nine = 9;int& ref = nine;

You will understand the following example:

# Include
  
   
Void f (int & I) {std: cout <lvalue ref: <I <;} void f (int & I) {std :: cout <rvalue ref: <I <;}int main () {int I = 77; f (I); // lvalue ref called f (99 ); // rvalue ref called f (std: move (I); // return 0 will be introduced later ;}
  

The right value is more obscure. Remember that if the result of an expression is a temporary object, the expression is the right value. also look at the Code:

#include 
  
   int getValue (){    int ii = 10;    return ii;}int main(){    std::cout << getValue();    return 0;}
  

Note that getValue () is a right value.

We can only do this. We must have a const.

const int& val = getValue(); // OKint& val = getValue(); // NOT OK

However, the right value reference in C ++ 11 allows this:

const int&& val = getValue(); // OKint&& val = getValue(); //  OK

Now let's make a comparison:

void printReference (const int& value){        cout << value;}void printReference (int&& value){        cout << value;}

The first printReference () can be set to either the left or the right.
The second printReference () can only accept the reference of the right value as a parameter.

In other words, by using the rvalue references, we can use function overloading to determine whether function parameters are lvalues or rvalues by having one overloaded function taking an lvalue reference and another taking an rvalue reference. in other words, C ++ 11 introduces a new non-const reference type called an rvalue reference, identified by T &&. this refers to temporaries that are permitted to be modified after they are initialized, which is the cornerstone of move semantics.

# Include
  
   
Using namespace std; void printReference (int & value) {cout <lvalue: value = <value <endl;} void printReference (int & value) {cout <rvalue: value = <value <endl;} int getValue () {int temp_ii = 99; return temp_ii;} int main () {int ii = 11; printReference (ii); printReference (getValue (); // printReference (99); return 0;}/* ---------------------- output lvalue: value = 11 rvalue: value = 99 ----------------------*/
  

 

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.