
I'm sure I'm being a bit dense here, but apart from handling native arrays what does boost::swap give you that std::swap doesn't? Thanks, - Rob.

On Fri, Mar 5, 2010 at 1:29 PM, Robert Jones
I'm sure I'm being a bit dense here, but apart from handling native arrays what does boost::swap give you that std::swap doesn't?
Extensibility. You call the qualified boost::swap but you can have implementations of swap in a namespace along with the swapee (is that a word?!). This facility is hugely useful when creating your own types and libraries for which you desire swap support. Users that call boost::swap will automatically leverage your customized swap behaviour.
Thanks,
- Rob.
Regards, Neil Groves

On Fri, Mar 5, 2010 at 1:37 PM, Neil Groves
On Fri, Mar 5, 2010 at 1:29 PM, Robert Jones
wrote: I'm sure I'm being a bit dense here, but apart from handling native arrays what does boost::swap give you that std::swap doesn't?
Extensibility. You call the qualified boost::swap but you can have implementations of swap in a namespace along with the swapee (is that a word?!). This facility is hugely useful when creating your own types and libraries for which you desire swap support. Users that call boost::swap will automatically leverage your customized swap behaviour.
Ahh, so it's a universal solution? "swap(a,b)" would use ADL but not find std::swap, and "std::swap(a,b)" would not find via ADL, whereas "boost::swap(a,b)" does both? Ok, cool! Any many boost libs define a swap(), but not a std::swap()! - Rob

It's really a replacement for the common idiom: using std::swap; swap(a,b); Which enables the use of ADL with a fallback to std::swap. Unfortunately, there are a number of compilers that choke on that idiom. boost::swap abstracts the idiom in a more portable way (and adds support for arrays).

On Fri, Mar 5, 2010 at 10:30 AM, Joseph Gauterin < joseph.gauterin@googlemail.com> wrote:
It's really a replacement for the common idiom:
using std::swap; swap(a,b);
IIRC one should write 'using namespace std' to avoid artificially preferring std::swap over swap from other namespaces.
Which enables the use of ADL with a fallback to std::swap. Unfortunately, there are a number of compilers that choke on that idiom. boost::swap abstracts the idiom in a more portable way (and adds support for arrays).
While your point is completely correct, I think that it is an important point that boost::swap allows the use of qualified calls to swap while maintaining extensibility. This allows one to avoid using XXX::swap in header files etc. This is extremely useful when writing header only libraries for example. Neil Groves
participants (4)
-
Joel Falcou
-
Joseph Gauterin
-
Neil Groves
-
Robert Jones