[Kde-pim] Re: use of std::swap in gpgme++

Frank Osterfeld frank.osterfeld at gmx.de
Fri May 27 17:10:39 BST 2011


On May 26, 2011, at 2:56 PM, Aaron J. Seigo wrote:

> hi...
> 
> looking at gpgme++ today i noticed code like this:
> 
>    const VerificationResult & operator=( VerificationResult other ) {
> ·       swap( other );
> ·       return *this;
>    }
> 
>    void swap( VerificationResult & other ) {
> ·       Result::swap( other );
> ·       using std::swap;
> ·       swap( this->d, other.d );
>    }
> 
> is swapping d pointers on assignment intentional? it seems a bit odd that 
> assigning an object to another results in them swapping their data. (as 
> opposed to assigning the rhs to the lhs)


That's the copy-and-swap idiom, see e.g. [1] (I know it from [2]).
It makes the assignment  exception-safe (std::swap must not throw).

Frank

[1] http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap
[2] Herb Sutter and Andrei Alexandrescu: C++ Coding Standards

_______________________________________________
KDE PIM mailing list kde-pim at kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/



More information about the kde-pim mailing list