This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/60448] swap_ranges does not use ADL correctly


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60448

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Slightly different version of that last test:

namespace tagged
{

template <typename T>
struct Swappable {};

#ifndef NO_CUSTOM_SWAP
template <typename T>
void swap(Swappable<T> & a, Swappable<T> & b) {
   static_cast<T &>(a).swap(static_cast<T &>(b));
}
#endif

} // namespace tagged

namespace std
{
#ifdef SWAP_FIRST
  template<typename T>
    void swap(T& a, T& b);
#endif

  template<typename T>
    void swapper(T t)
    {
      swap(*t, *t);
    }

  template<typename T>
    void swap(T& a, T& b)
    {
      T tmp = static_cast<T&&>(a);
      a = static_cast<T&&>(b);
      b = static_cast<T&&>(tmp);
    }
}

int main()
{
  struct local : tagged::Swappable<local> {
    local(int x) : data(x) {}

    local(local const &) = delete;
    local(local      &&) = delete;
    local & operator=(local const &) = delete;
    local & operator=(local      &&) = delete;

    void swap(local & other) {
       auto x = other.data;
       other.data = this->data;
       this->data = x;
    }
  private:
    int data;
  };

  local l{1};
  std::swapper(&l);
}


I must be guessing something wrong about libc++'s std::swap_ranges(), because
if it was declared before std::swap() then it would never work with any type
that doesn't provide its own swap() overload, as demonstrated by compiling the
above with NO_CUSTOM_SWAP defined.

So I don't know exactly what's going on, but I'm not convinced libstdc++ is
doing anything wrong.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]