Topic: Class with member swap and std::swap specialization still not


Author: "Niels Dekker (no reply address)" <unknown@this.is.invalid>
Date: Wed, 13 Sep 2006 11:26:00 CST
Raw View
> Wouldn't it better to include any T as being Swappable for which the
> following lines are valid, and have the right semantics?
>   using std::swap;
>   swap(t, u);  // t and u are values of type T

Greg Herlihy replied:
>
> No, the second line all by itself should be valid:
>
>      swap( t, u );
>
> which is not the case in the above program. Because swap() has been
> defined in the std namespace, it will not be found when passed
> arguments of a class (Uncopyable) that has been declared in the global
> namespace.

Thank you.  So will std functions that have those Swappable requirements
(e.g., std::iter_swap) give compile errors when I try to use them for
objects of my Uncopyable class, even if it has a specialization for
std::swap?

In other words, will the following program be rejected by C++0x?
------------------------------------------------------------------------
  #include <algorithm>

  class Uncopyable
  {
  public:
    Uncopyable() {}
    void swap(Uncopyable &) {}
  private:
    Uncopyable(const Uncopyable &);
    Uncopyable& operator=(const Uncopyable&);
  };

  namespace std
  {
    template <>
    void swap<Uncopyable>(Uncopyable& lhs, Uncopyable & rhs)
    {
      lhs.swap(rhs);
    }
  }

  int main()
  {
    Uncopyable t, u;

    std::iter_swap(&t, &u);  // Rejected by C++0x?
  }
------------------------------------------------------------------------

Kind regards,

  Niels Dekker

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]