using swap to make assignment operator exception safe

This is a discussion on using swap to make assignment operator exception safe within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    using swap to make assignment operator exception safe

    Hello everyone,


    The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant).

    It used a temporary object "temp" in this sample, and assignment is made on a to temp ar first. Even if there is exception, the current this object's state is not corrupted.

    My question is, the pattern works only if there is no exception thrown by swap function. If there are exception in swap function, the state of current object instance may still be corrupted (swap may invoke the assignment operator of member variables). Is my understanding correct?

    Code:
    class A;
    A& A::operator= (const A& a)
    {
        A temp;
        temp = a; // exception may be thrown
        swap (*this, temp);
    }

    thanks in advance,
    George

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's right.

    However, isn't that implementation using assignment recursively (the commented line)? Rather the copy constructor is used (which may throw).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks anon,


    How to avoid the recursive issue in the swap-to-make-exception-safe pattern? :-)

    Quote Originally Posted by anon View Post
    That's right.

    However, isn't that implementation using assignment recursively (the commented line)? Rather the copy constructor is used (which may throw).

    Thanks cpjust,


    But the link does not contain information about how to design a no-exception throw swap function. :-)


    Quote Originally Posted by cpjust View Post


    regards,
    George

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    A& A::operator= (const A& a)
    {
        A temp(a); //copy constructor
        swap (*this, temp);
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    I would write a member swap and then write either:
    Code:
    A& A::operator=(const A& a)
    {
        A temp(a);
        swap(temp);
        return *this;
    }
    or:
    Code:
    A& A::operator=(const A& a)
    {
        if (this != &a)
        {
            A temp(a);
            swap(temp);
        }
        return *this;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The second one would rarely be needed, since hopefully the member swap doesn't fail on swapping with your self and hopefully the member swap is relatively fast. If it wasn't fast, then using the swap technique would be a bit of a pessimization I would imagine, since you would be using the presumably slow copy constructor as well as the slow swap, rather than just implementing the copy assignment operator directly.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Member swap can be fast if you are swapping resources such as pointers between instances.

    If you don't have such resources and shallow copying is all you need you don't need to implement operator= at all. That should take care of a majority of cases where swap-technique would be a pessimization?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It should always be obvious how to write a nothrow swap. If it is hard, then perhaps the class doesn't need a swap specialization (it has only shallow data), or there is a design error in the class.

    But all resource-managing classes, which are the prime candidates for fast nothrow swaps, should contain some kind of resource handle. This handle should be easily swappable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks for all of you guys' help!


    My question is answered.

    Quote Originally Posted by CornedBee View Post
    It should always be obvious how to write a nothrow swap. If it is hard, then perhaps the class doesn't need a swap specialization (it has only shallow data), or there is a design error in the class.

    But all resource-managing classes, which are the prime candidates for fast nothrow swaps, should contain some kind of resource handle. This handle should be easily swappable.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  3. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 10:53 AM
  4. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  5. Multiple OS's
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 04-10-2003, 01:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21