Boost logo

Boost :

Subject: Re: [boost] boost::shared_ptr, std::swap
From: Ben Pope (benpope81_at_[hidden])
Date: 2012-05-26 05:13:13


On Saturday, May 26, 2012 04:54 PM, Ingo Loehken wrote:
> Hello,
>
> i just recognized, that std::swap is not overload for boost::shared_ptr,
> meaning that using swap with std namespace ends up with unexpected
> results in respect to performance.
>
> Is this by intent ?

If you look at the synopsis:
http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#Synopsis

You'll see a member swap and a free function swap in the boost
namespace, this free function is intended to be found with ADL.

There is a note on that page:
"[swap is defined in the same namespace as shared_ptr as this is
currently the only legal way to supply a swap function that has a chance
to be used by the standard library.]"

If you look at the implementation of boost::swap you'll see something like:

namespace boost {
    void swap(T& a, T& b)
    {
       using std::swap;
       swap(a, b);
    }
}

Which allows you to swap with ADL or std::swap fallback. I don't think
you want to explicitly call ::std::swap from user code.

Details: http://www.boost.org/doc/libs/1_49_0/libs/utility/swap.html

Ben


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk