C ++ 11 learning notes -- right value reference and mobile construction Semantics

Source: Internet
Author: User

C ++ 11 learning notes -- right value reference and mobile construction Semantics
Today, I decided to try another way to express it. Any new syntax is generated to solve a problem, so let's look at the problem first today. Copy the code class myStr {protected: char * str _; public: myStr (void) // default constructor and do nothing: str _ (nullptr) {} myStr (const char * rhs) // normal assignment constructor: str _ (nullptr) {if (! Rhs) return; str _ = new char [1024]; strcpy (str _, rhs); // cout <"Str constructor" <str _ <std :: endl;} myStr (const myStr & rhs) // copy constructor: str _ (nullptr) {if (! Rhs) return; str _ = new char [1024]; strcpy (str _, rhs. str _); // cout <"Str copy constructor" <str _ <std: endl;} myStr (myStr & rhs): str _ (nullptr) {swap (rhs); // std: cout <"Str move constructor" <str _ <std: endl ;}~ MyStr () // destructor {if (! Str _) return; // std: cout <"Str destructor" <str _ <std: endl; delete [] str _;} const myStr & operator = (myStr rhs) // value assignment operator overload {rhs. swap (* this); // obtain the data return (* this) using copy-and-swap usage; // avoid repeated operator =} void swap (myStr & rhs) // exchange algorithm {std: swap (str _, rhs. str _);} operator char * (void) const {return str _;} myStr & operator + = (const char * rhs) {if (rhs) strcat (str _, rhs); return (* this );} // Friend myStr operator + (const myStr & x, const myStr & y) // {// return myStr (x) + = y; //} friend myStr operator + (const myStr & x, const myStr & y) {return std: move (myStr (x) + = y );} copy the code and execute the following code to copy the code myStr ss ("000"); myStr s1 ("11111"), s2 ("22222"), s3 ("3333333 "), s4 ("4444444"); cout <std: endl; time_t timestamp1; time_t timestamp2; time_t timestamp3; const long max = 30000000; time (& timestamp 1); for (long I = 0; I <max; I ++) {ss = s1 + s2 + s3 + s4;} time (& timestamp2 ); timestamp3 = timestamp2-timestamp1; the Code below the copy code is the only different implementation, but it brings a performance gap of 30-40%. Copy the code/friend myStr operator + (const myStr & x, const myStr & y) // {// return myStr (x) + = y; //} friend myStr operator + (const myStr & x, const myStr & y) {return std: move (myStr (x) + = y );} copy the code and find another example. Copy the code class MemoryBlock {public: // Constructor (initialization Resource) explicit MemoryBlock (size_t length): _ length (length ), _ data (new int [length]) {std: cout <"MemoryBlock constructor" <std: endl ;}// destructor (release resources )~ MemoryBlock () {if (_ data! = Nullptr) {delete [] _ data;} std: cout <"MemoryBlock destructor" <std: endl;} // copy constrtor (implement copy semantics: copy that) MemoryBlock (const MemoryBlock & that) // copy the resources of that object: _ length (that. _ length), _ data (new int [that. _ length]) {std: copy (that. _ data, that. _ data + _ length, _ data); std: cout <"copy constructor" <std: endl;} // copy the value assignment operator (meaning: release this + copy that) MemoryBlock & operator = (const MemoryBlock & t Hat) {if (this! = & That) {// release its own resource delete [] _ data; // copy that object's resource _ length = that. _ length; _ data = new int [_ length]; std: copy (that. _ data, that. _ data + _ length, _ data);} return * this;} // mobile Constructor (implement Mobile semantics: Move that) MemoryBlock (MemoryBlock & that) // point its resource pointer to the resource owned by that object: _ length (that. _ length), _ data (that. _ data) {// set the pointer that the object originally points to the resource to a null value that. _ data = nullptr; that. _ length = 0;} // The Moving Value assignment operator (implementing the moving semantics: releasing this + moving t Hat) MemoryBlock & operator = (MemoryBlock & that) {if (this! = & That) {// release its own resource delete [] _ data; // point its resource pointer to the resource _ data = that. _ data; _ length = that. _ length; // set the pointer that the object originally points to the resource to a null value that. _ data = nullptr; that. _ length = 0;} return * this;} private: size_t _ length; // resource length int * _ data; // pointer to the resource, representing the resource itself }; memoryBlock f () {return MemoryBlock (50);} run the following code to copy the code const long max = 100000; time_t timestamp1; time_t timestamp2; time_t timestamp3; Time (& timestamp1); for (long I = 0; I <max; I ++) {MemoryBlock a = MemoryBlock (50); MemoryBlock c = std :: move (a) ;}time (& timestamp2); timestamp3 = timestamp2-timestamp1; copy the code to replace MemoryBlock c = std: move (a) with MemoryBlock c =; there is also a 30% performance gap. This is the benefit of right value reference and moving construction semantics. I understand that in the past, only the left value can be referenced, but the right value cannot be referenced, the addition of the new syntax realizes the reference of the right value, which reduces the generation and destruction of zero-point objects, but also brings more weird syntaxes, significantly increasing the learning cost of c ++, in terms of syntax design, will it be more elegant to add reference counters like oc to manage objects? At least it will not make the upper-layer coders so tired, in fact, smart pointers can achieve the same effect. After so many years, c ++ has been doing addition, so that learning and usage costs are too high. When will the uncle of the Standards Committee consider doing vertex subtraction, do not make c ++ so academic or full of places a trap, or let 10 methods to implement a technology, but five of them are traps. Depend!

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.