C++11 rvalue Reference

Source: Internet
Author: User

http://blog.csdn.net/cpd92/article/details/50651700

Http://www.tuicool.com/articles/i2qyyyB

Move is an rvalue reference that reduces the copy and release process and improves program performance.

#include <iostream>#include<string>#include<vector>using namespacestd;classMyString {Private:    Char*_data;    size_t _len; void_init_data (Const Char*s) {_data=New Char[_len +1];        memcpy (_data, S, _len); _data[_len]=' /'; } Public: MyString () {_data=NULL; _len=0; } MyString (Const Char*p) {_len=strlen (P);    _init_data (P); } MyString (Constmystring&str) {_len=Str._len;        _init_data (Str._data); Std::cout<<"Copy Constructor is called! Source:"<< Str._data <<Std::endl; } MyString&operator=(Constmystring&str) {        if( This! = &str) {_len=Str._len;        _init_data (Str._data); } std::cout<<"Copy Assignment is called! Source:"<< Str._data <<Std::endl; return* This; }    Virtual~MyString () {if(_data) Free(_data); }};intMain () {MyString A; A= MyString ("Hello"); Std::vector<MyString>VEC; Vec.push_back (MyString (" World"));}

In the main function, the operation of the call copy constructor and the copy assignment operator are implemented. MyString ("Hello") and MyString ("World") are temporary objects, which are right values. Although they are temporary, the program still invokes copy construction and copy assignment, resulting in meaningless resource requests and actions released. If you can directly use the resources that the temporary object has already requested, you can save resources and save time on resource requests and releases. This is precisely the purpose of defining the transfer semantics.

Add transfer construct, transfer assignment operator overload

MyString (mystring&&str) {Std::cout<<"Move Constructor is called! Source:"<< Str._data <<Std::endl; _len=Str._len; _data=Str._data; Str._len=0; Str._data=NULL; } MyString&operator= (mystring&&str) {Std::cout<<"Move Assignment is called! Source:"<< Str._data <<Std::endl; if( This! = &str) {_len=Str._len; _data=Str._data; Str._len=0; Str._data=NULL; }        return* This; }

With the addition of the transfer constructor and the transfer copy operator, our program runs with the following results:

Move Assignment is called! Source:hello  Move Constructor is called! source:world

Left-value right-value judgment

#include <iostream>#include<string>#include<vector>using namespacestd;template<typename t>voidPrintt (t&t) {cout<<"Lvaue"<<Endl;} Template<typename t>voidPrintt (T &&t) {cout<<"rvalue"<<Endl;} Template<typename t>voidTestforward (T &&v)    {Printt (v); Printt (Std::forward<T>(v)); Printt (Std::move (v));}intMain () {Testforward (1); intx =1;    Testforward (x); Testforward (Std::forward<int>(x));}

Std::forward will be forwarded according to the original type of the parameter

Std::move to the right value

Universal Packaging Device

#include <iostream>#include<string>#include<vector>using namespacestd;template<classFunction,class... Args>Inline Auto Funcwrapper (Function&& F, args && ... args)Decltype (f (Std::forward<Args>(args) ...) {    returnF (std::forward<args>(args) ...);}voidTest0 () {cout <<"void"<<Endl;}intTest1 () {return 1; } intTest2 (intx) {returnx;}stringTest3 (stringS1,stringS2) {returnS1 +S2;}voidTest () {funcwrapper (test0);//no return value, print 1Cout<<funcwrapper (test1) <<endl;//returns 1Cout<<funcwrapper (Test2,1) <<endl;//returns 1Cout<<funcwrapper (Test3,"AA","BB") <<endl;//back to "AABB"}intMain () {test ();}

Members of the Emplace_back

Most containers in c++11 add a Emplace_back member function, which is defined in the vector:

Class ... args >void Emplace_back (args&& .... args);

The args&& here is an undetermined reference type, so it can receive lvalue references and rvalue references, and its interior is also called the Std::forward implementation for perfect forwarding. So if we need to add an rvalue, temporary variable to the container, using Emplace_back can improve performance.

C++11 rvalue Reference

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.