Mobile construction and movement assignment and Std::move

Source: Internet
Author: User

---------------------------------------Mobile Construction--------------------------------------------

Traditional deep-Copy depth assignment

For a class that contains pointers, its copy construction and copy assignment should be implemented from the implementation. The so-called deep copy and deep assignment. I think it has become a consensus.

For example, the following classes:



Hasptrmem (const hasptrmem& Another): _d (new int (*another._d))
{cout<< "Hasptrmem (const hasptrmem& another) <<this<<" <<&another<< Endl;}





Hasptrmem a;//Hasptrmem B (a);//cout<<*a._d<<endl;//cout<<*b._d<<endl;  hasptrmem&& ret = gettemp (); return 0;}

The above procedure, we have known,ret as an rvalue reference, refers to the temporary object, because the temporary object is to return a copy of the object, so the surface appears to be the scope of the object to return extended, the life cycle is also extended.

From Rvalue to moving construct


before we set up a concept , which is the rvalue reference. Use the idea of rvalue reference, and then implement the copy. This way, the problem of the temporary object is also solved.  

#include <iostream>using namespace Std;class hasptrmem{public:    hasptrmem (): _d (new int (0)) {    cout< < "Hasptrmem ()" <<THIS<<ENDL;} Hasptrmem (const hasptrmem& Another): _d (new int (*another._d)) {    cout<< "Hasptrmem (const hasptrmem& Another) "<<this<<" <<   
Hasptrmem (Hasptrmem &&another) { cout<<this<< "Move resourse from" <<&another< < << another._d <<endl; _d = another._d; Another._d = nullptr;} ~hasptrmem () { delete _d; cout<< "~hasptrmem ()" <<this<<endl; } int * _d;}; Hasptrmem gettemp () { return Hasptrmem ();} int main (int argc, char *argv[]) { Hasptrmem a = Gettemp (); return 0;}

Mobile Construction

The following is the move constructor. We borrowed the temporary variable to "steal" the contents of the object to be returned.

The mobile construction fully embodies the design idea of rvalue reference, and we also see the essence of rvalue reference at the object level by moving construction. Thus, it is not difficult to understand how the normal type Rvalue reference is manipulated internally.

Mobile Construction
Hasptrmem (Hasptrmem &&another) { cout<<this<< "Move resourse from" <<&another< < "<< another._d<<endl; " _d = another._d; Another._d = nullptr;}

And look at the copy constructor, let's compare the differences:

Hasptrmem (const hasptrmem& Another): _d (new int (*another._d)) {    cout<< "Hasptrmem (const HASPTRMEM & another) "<<this<<" <<   &another<<Endl;}

  Moving constructs are compared to the copy construction, and the moving constructs pass the assignment of pointers and take over the space address of the temporary object on the heap in time before the temporary object is reconstructed.

About the default move constructor

  For objects that do not contain resources, the self-implementing copy and the move semantics are meaningless, and for such a type the movement is the copy, and the copy is the move.

Copy construction / assignment and move construction / assignment must be provided at the same time or not at the same time. To ensure that both copy and move semantics are available. A class can only implement one semantics if one is declared.

  A class that only copies semantics, that is, the class in c++98. Only the classes that move the semantics indicate that the resources owned by the class's variables can only be moved, not copied. Then such a resource must be unique. Only the type of the move semantic construct is often the type of "resource type." such as smart pointers, file streams, and so on.

Efficiency issues

#include <iostream>

Using NAMESAPCE std;


Class copyable{public:copyable (int i): _i (new int (i)) {cout<< "copyable (int i):" <<this&lt ;<endl; } copyable (const copyable & Another): _i (new int (*another._i)) {cout<< "copyable (const COPYA Ble & another): "<<this<<endl; } copyable (Copyable && another) {cout<< "copyable (copyable && another):" <<this&lt ;<endl; _i = another._i; } copyable & operator= (const copyable &another) {cout<< "Copyable & operator= (const COPYABL E &another): "<<this<<endl; if (this = = & another) return *this; *_i=*another._i; return *this; } copyable & Operator= (copyable && another) {cout<< "Moveable & operator= (moveable &amp ;& another): "<<this<<endl; if (this = &another) {*_i = *another._i; Another._i = NULL; } return * this; } ~copyable () {cout<< "~copyable ():" <<this<<endl; if (_i) Delete _i; } void Dis () {cout<< "class copyable is called" <<endl; } void Dis () const {cout<< "Const class copyable is called" <<endl; }private:int * _i;}; void Putrrvalue (copyable && a) {cout<< "Putrrvalue (copyable && a)" <<endl; A.dis ();} void Putclvalue (const copyable & a) {cout<< "Putcrvalue (Copyable & A)" <<endl; A.dis ();//error!} The const t& and t&& overloads exist at the same time who is called first? void Whichcall (const copyable & A) {A.dis ();} void Whichcall (copyable && a) {A.dis ();} int main (int argc, char *argv[]) {//Copyable RRC = getcopyable (); cout<< "invoke move Construct" <<endl; Copyable a =copyable (2);//Anonymous object/temporary object first call Rvalue reference construct-rvalue construct cout<< "copy construct" <<endl; Copyable CA (a); cout<< "directly constructs the right value" <<endl; Copyable && Rra =copyable (2); cout<< "==================================" <<endl; An rvalue reference is associated with a const reference. is efficiency the same? cout<< "rvalue reference pass" <<endl; Putrrvalue (copyable (2)); cout<< "Const Reference pass" <<endl; Putclvalue (copyable (2)); cout<< "----------------------" <<endl; What type of overload is called first? t&& or the const t&? Whichcall (copyable (2)); This is nothing to tangle with! t&& 's appearance is the problem of resolving the non-cosnt function after the const T & Accept anonymous/temporary object. return 0;}

-----------------------------------------template function Std::move----------------------------------------

Although you cannot bind an rvalue reference directly to an lvalue, we can explicitly convert an lvalue to the corresponding rvalue reference type. We can also get an rvalue reference that is bound to an lvalue by calling a new standard library function called move, which is defined in untility. The move function uses the * * mechanism to return an rvalue reference for a given object.

int &&rra = RR1; Error:  //! error! Rvalue reference cannot be directly bound to an lvalue int &&rr = Std::move (RR1); Ok!

  The move call tells the compiler that we have an lvalue, but we want to deal with him as if it were a right-hand value.

Calling move means committing: we will no longer be able to use it except to assign a value to RR1 or destroy it. After calling move, we cannot make any assumptions about the value of the moved source object.

Mobile construction and movement assignment and Std::move

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.