Boost logo

Boost :

Subject: Re: [boost] [swap] please update documentation with the header Ineed to include
From: Niels Dekker - address until 2010-10-10 (niels_address_until_2010-10-10_at_[hidden])
Date: 2009-06-29 06:38:54


Daniel James wrote:
> Try it. std::swap can't swap arrays, boost::swap can. boost::swap will
> also call swap via. ADL when appropriate and supports compilers that
> don't support ADL. Which is why it's recommended that 'boost::swap' is
> used.

Thanks, Daniel. In fact, proper performance of boost::swap /does/
depend on compilers having implemented ADL. But we've found out that
for some compilers (including MSVC 7.1, Borland 5.9.3, and Intel 8.1),
ADL only works fine when a using-directive ("using namespace std") is
used, instead of a using-declaration ("using std::swap"). Luckily this
using-directive is hidden within an internal helper function of
boost::swap.

Peter Simons wrote:
>> $ cat >test.cpp <<EOF
>> #include <string>
>> #include <boost/swap.hpp>
>>
>> int main(int, char * [])
>> {
>> std::string a1[42];
>> std::string a2[42];
>>
>> boost::swap(a1, a2); // Okay.
>>
>> using boost::swap;
>> swap(a1, a2); // Does not compile!
>>
>> return 0;
>> }
>> EOF
>> $ g++ -pedantic test.cpp && ./a.out && echo okay
>> okay

Interesting! Can you tell us whether it picks std::swap or boost::swap?

BTW, support for built-in arrays is added to std::swap very recently;
it's in the Working Draft. Basically it's my very first (very) little
contribution to the Standard :-)

25.4.3 Swap [alg.swap]
(www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2914.pdf) says:

  template<ValueType T, size_t N>
  requires Swappable<T>

  void swap(T (&a)[N], T (&b)[N]);
  2 Effects: swap_ranges(a, a + N, b)

I wonder if GCC has already implemented this new feature.

--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center

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