[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-pim
Subject:    [Kde-pim] Re: use of std::swap in gpgme++
From:       Frank Osterfeld <frank.osterfeld () gmx ! de>
Date:       2011-05-27 16:10:39
Message-ID: 8C037760-F05F-4441-93F2-58DA0BE6543C () gmx ! de
[Download RAW message or body]


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@kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic