Rvalue Reference and Transfer semantics (C++11)

Source: Internet
Author: User

Resources:

Http://www.cnblogs.com/lebronjames/p/3614773.html

left and right values are defined:

All expressions and variables in C + + (including c) are either Lvalue or rvalue. The definition of a popular lvalue is a non-temporary object (which can take an address, have a name), and those objects that can be used in more than one statement. All variables satisfy this definition and can be used in multiple codes, all of which are lvalue values. The right values refer to temporary objects , which are valid only in the current statement. Take a look at the following examples:

1. Simple Assignment Statements

such as: int i = 0;

In this statement, I is an lvalue, 0 is a temporary value, or the right value. In the following code, I can be referenced, 0 is not possible. The immediate number is the right value.

2. The right value can also appear to the left of an assignment expression, but it cannot be an assigned object because the right value is only valid in the current statement and the assignment is meaningless.

such as: ((i>0)? i:j) = 1;

In this example, 0 appears on the left side of the "=" as the right value. But the assignment object is I or J, both are lvalue values.

Before c++11, the right value cannot be referenced, and the maximum is to bind a right value with a constant reference, such as:

const int &a = 1;

Syntax symbols for left and right values:

The declaration symbol for the lvalue is "&", and for the left value, the declaration symbol for the right value is "&&". However, if a temporary object is passed to another function through a function that accepts an rvalue, it becomes an lvalue, because the temporary object becomes a named object during the transfer process.

#include <iostream>using namespacestd;voidValueint&v) {cout<<" Left"; cout<<__func__<":"<<v<<Endl;}voidValueint&&v) {cout<<" Right"; cout<<__func__<":"<<v<<Endl;}voidFvalue (int&&v) {value (v);}intMain () {intA= A;    Value (a); Value (2); Fvalue (1); Value (Std::move (a));

Transfer semantics:

Rvalue references are used to support the transfer semantics. Transfer semantics can move resources (heaps, system objects, and so on) from one object to another, reducing the creation, copying, and destruction of unnecessary temporary objects, which can significantly improve the performance of C + + applications. Maintenance (creation and destruction) of temporary objects has a serious impact on performance.

By transferring semantics, the resources in the temporary object can be transferred to other objects.

In the existing C + + mechanism, we can define copy constructors and assignment functions. To implement transfer semantics, you need to define a transfer constructor, and you can also define a transfer assignment operator. The transfer constructor and transfer assignment operators are called for copy and assignment of the right value. If the transfer constructor and the transfer copy operator are not defined, then the existing mechanism is followed, and the copy constructor and assignment operator are called.

Example:

#include <cassert>#include<iostream>#include<cstring>using namespacestd;classstring{Private :    Char*_data; int_len; voidInitConst Char*s) {_data=New Char[_len+1];        memcpy (_data,s,_len); _data[_len]=' /'; }     Public: String (): _data (nullptr), _len (0) {} String (Const Char*str) {Assert (str!=nullptr); _len=strlen (str);    init (str); } String (Conststring&str) {cout<<"Call cctor"<<Endl; _len=Str._len;    Init (str._data); } String&operator=(Conststring&str) {cout<<"Call Copy Assignment"<<Endl; if( This!=&str) {_len=Str._len;        Init (str._data); }        return* This; }    //Transfer Constructor (C++11)String (string&&str) {cout<<"Call move ctor"<<Endl; _len=Str._len; _data=Str._data; Str._data=nullptr; Str._len=0; }    //Transfer assignment operator (C++11)string&operator= (string&&str) {cout<<"Call Move Assign"<<Endl; if( This!=&str) {_len=Str._len; _data=Str._data; }        return* This; }    voidprint () {cout<<"Data:"<Endl; }    ~String () {cout<<"Dctor"<<Endl; if(_data) {delete[] _data; }    }};intMain () {Const Char* s="Hello World";    String Str (s);    Str.print (); String tmp (String ("Hello"));    Tmp.print (); String&& mstr=std::move (str); TMP=MSTR; Tmp.print ();}

Compilation options: g++ move.cpp-std=c++11-fno-elide-constructors

Where-fno-elide-constructors means: forcing g++ always calls the copy constructor, even when initializing another object of the same type with a temporary object.

Standard library function Std::move

Since the compiler only calls the transfer constructor and the transfer assignment function on rvalue references, all named objects can only be lvalue references, and if it is known that a named object is no longer in use and wants to call the transfer constructor and the transfer assignment function on it, that is, to use an lvalue reference as an rvalue reference, how do you do it? The standard library provides the function std::move, which converts an lvalue reference to an rvalue reference in a very simple way.

Example:

#include <iostream>using namespacestd;template<classT>voidM_swap (t& a,t&b)    {T tmp (Std::move (a)); A=Std::move (b); b=Std::move (TMP);}intMain () {intA=0; intb=1; cout<<"before swap:"<<a<<" "<<b<<Endl;    M_swap (A, b); cout<<"After swap:"<<a<<" "<<b<<Endl;}

Rvalue Reference and Transfer semantics (C++11)

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.