[Bug libstdc++/60448] swap_ranges does not use ADL correctly
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Mar 6 16:27:00 GMT 2014
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.
More information about the Gcc-bugs
mailing list