Copy-and-swap

Source: Internet
Author: User

An assignment operator that overloads an exception safe (exception safe) in its own definition class. Created a idiom of this kind. Also called: Create-temporary-and-swap.


To write robust C + + code. Exception security is important.
To be able to give abnormal safety level three:
1> Basic security: Simple to implement, low cost. Should be used as a regular means.
2> is very safe: it is not possible to achieve this step on any occasion. The assignment operator overload for this example is considered to be one.


3> does not throw an exception: can see non-throwing swap.

The so-called "Create-temporary-and-swap". is to apply for new resources first. And then released after use.
1> Use the RAII principle when applying for new resources.
2> after the successful application. Exchange resources using the non-throwing swap principle.

[CPP]View Plaincopy
  1. class String {
  2. Char * STR;
  3. Public:
  4. string& operator = (String const &s)
  5. {
  6. String temp (s); //Copy-constructor--RAII
  7. Temp.swap (*this); //non-throwing swap
  8. return *this;
  9. }
  10. void Swap (String &s) throw ()
  11. {
  12. Std::swap (this->str, s.str);
  13. }
  14. };

Sometimes it increases the inference of the number of references:

[CPP]View Plaincopy
  1. class String {
  2. Char * STR;
  3. Public:
  4. string& operator = (String const &s)
  5. {
  6. if ( This! = &s)
  7. String (s). Swap (*this); //Copy-constructor and non-throwing swap
  8. return *this;
  9. }
  10. void Swap (String &s) throw ()
  11. {
  12. Std::swap (this->str, s.str);
  13. }
  14. };

A temporary variable inside a function. can actually be omitted. Just use the pass-through method:

[CPP]View Plaincopy
  1. string& operator = (String s) //Pass-by-value
  2. {
  3. S.swap (*this); //non-throwing swap
  4. return *this;
  5. }

Such a practice can be optimized in some cases.
Suppose S binds to lvalue, no optimizations. Temporary objects are created inside the stack, and are assumed to be bound to rvalue. The call to copy ctor is usually omitted.

[CPP]View Plaincopy
    1. String createstring (); //A function that returns a String object.
    2. String s;
    3. s = createstring (); //This is an example of rvalue. An assignment operator that uses a pass-through value. can be optimized

In the c++0x standard, this assignment operator is called the "uniform assignment operator" because it combines "copy assignment" and "Move Assignment". The c++0x compiler will always optimize temporary rvalue once it discovers that a class has a move ctor present.
The old compiler certainly does not have move ctor, but is able to rvalue the assignment operation to optimize, also is a somewhat.

Copy-and-swap

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.