N
Niels Dekker (no reply address)
When calling swap as follows (as recommanded in Effective C++, 3rd
Edition, by Scott Meyers), what swap is chosen to be called?
using std::swap;
swap(a, b);
Suppose there is a global ::swap function provided, whose parameter type
matches closer to the type of a and b than any of the std::swap
overloads does. Will this ::swap be called, or is std::swap still
preferred? I ask this because the compilers I tried disagree! So will
any of the ::swap functions I defined down below be called in the
following main()?
#include <algorithm>
struct Foo {};
void swap(int &, int &) {}
void swap(Foo &, Foo &) {}
template<typename T> void swap(T*&, T*&) {}
int main()
{
using std::swap;
int i1, i2;
swap(i1, i2);
int *ptr1, *ptr2;
swap(ptr1, ptr2);
Foo foo1, foo2;
swap(foo1, foo2);
Foo *foo_ptr1, *foo_ptr2;
swap(foo_ptr1, foo_ptr2);
}
To my surprise, MSVC++ 8.0 prefers to call std::swap for int and int*.
It only calls ::swap for Foo and Foo*. But GNU g++ 3.4.4 surprises me
even more, as it never calls any of my ::swap overloads at all, and
always prefers calling std::swap instead! So what's the Standard
compliant way?
Kind regards
Niels Dekker
xs4all.nl/~nd/dekkerware
Edition, by Scott Meyers), what swap is chosen to be called?
using std::swap;
swap(a, b);
Suppose there is a global ::swap function provided, whose parameter type
matches closer to the type of a and b than any of the std::swap
overloads does. Will this ::swap be called, or is std::swap still
preferred? I ask this because the compilers I tried disagree! So will
any of the ::swap functions I defined down below be called in the
following main()?
#include <algorithm>
struct Foo {};
void swap(int &, int &) {}
void swap(Foo &, Foo &) {}
template<typename T> void swap(T*&, T*&) {}
int main()
{
using std::swap;
int i1, i2;
swap(i1, i2);
int *ptr1, *ptr2;
swap(ptr1, ptr2);
Foo foo1, foo2;
swap(foo1, foo2);
Foo *foo_ptr1, *foo_ptr2;
swap(foo_ptr1, foo_ptr2);
}
To my surprise, MSVC++ 8.0 prefers to call std::swap for int and int*.
It only calls ::swap for Foo and Foo*. But GNU g++ 3.4.4 surprises me
even more, as it never calls any of my ::swap overloads at all, and
always prefers calling std::swap instead! So what's the Standard
compliant way?
Kind regards
Niels Dekker
xs4all.nl/~nd/dekkerware