Boost logo

Boost :

From: Niels Dekker - mail address until 2008-12-31 (nd_mail_address_valid_until_2008-12-31_at_[hidden])
Date: 2007-09-29 11:07:44


Joseph Gauterin wrote:
> As discussed previously on the mailing list with David Abrahams and
> Steve Watanabe, I've written a swap function for boost which uses
> argument dependent lookup (aka koenig lookup) [...]

Looks nice!

Please note that there's a bug in your test, "specialized_in_std.cpp",
as it says:
  namespace std
  {
    void swap(swap_test_class& left, swap_test_class& right)
    {
      left.swap(right);
    }
  }

The C++ Standard does not allow you to add your own overload of swap to
the std namespace. Quoting the latest Draft (N2315, [reserved.names]):
"It is undefined for a C++ program to add declarations or definitions to
namespace std ..." But I guess you know about this already, and you
intended to do a template specialization of std::swap, right?

Also note that your boost::swap may not work properly on ConceptGCC. I
tried the following test program, using the BoostCon Edition of
ConceptGCC, from
http://www.generic-programming.org/software/ConceptGCC/download.php

  ////////////////////////////////////////////////////////////
  #include <boost/utility/swap.hpp>
  #include <boost/utility.hpp>

  // Foo is non-copyable, but still it is swappable!
  class Foo: boost::noncopyable
  {
    int m_data;
  public:
    Foo() : m_data(0) {}

    void swap(Foo & arg)
    {
      std::swap(this->m_data, arg.m_data);
    }
  };

  void swap(Foo & arg1, Foo & arg2)
  {
    arg1.swap(arg2);
  }

  int main(int, char*[])
  {
    Foo object1;
    Foo object2;
    boost::swap(object1,object2);
    return 0;
  }
  ////////////////////////////////////////////////////////////

In this case, boost::swap should call the swap(Foo &, Foo &) function
that I put into the global namespace, right? Instead, ConceptGCC
(BoostCon) rejects the program, saying:

noncopyable.hpp: In member function 'Foo& Foo::operator=(const Foo&)':
noncopyable.hpp:28: error: 'const boost::noncopyable_::noncopyable&
boost::noncopyable_::noncopyable::operator=(const
boost::noncopyable_::noncopyable&)' is private
libs/utility/swap/test/niels_test.cpp:5: error: within this context
utility/swap.hpp: At global scope:
utility/swap.hpp:20: note: synthesized method 'Foo& Foo::operator=(const
Foo&)' first required here

Actually Douglas Gregor already has a plan on how to fix this issue:
http://conceptgcc.wordpress.com/2007/01/02/revisiting-name-lookup-with-the-swap-example/

So I guess in the future your boost::swap will work fine with ConceptGCC
as well. :-)

Kind regards,

  Niels

-- 
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