简体   繁体   中英

Why is not std::swap in global namespace?

Item 25 in Effective c++ third edition , Scott Meyers suggests to implement swap in the same namespace as the class, and then when swapping to employ the using std::swap, and there the author says :

For example, if you were to write the call to swap this way:

 std::swap(obj1,obj2); // the wrong way to call swap 

you'd force the compiler to consider only the swap in std, thus eliminating the possibility of getting a more appropriate T-specific version defined elsewhere. Alas, some misguided programmers do qualify calls to swap in this way, and that is why it's important to totally specialize std::swap for your classes.

The author recommends to always swap objects this way :

#include <iostream>
#include <utility>

#define CUSTOM_SWAP

namespace aaa{

struct A
{
};
#ifdef CUSTOM_SWAP
void swap( A&, A& )
{
    std::cout<<"not std::swap"<<std::endl;
}
#endif

}

int main() 
{
    using std::swap;   // add std::swap to a list of possible resolutions

    aaa::A a1;
    aaa::A a2;

    swap(a1,a2);
}

Why isn't std::swap in global namespace? That way, it would be simpler to add custom swap functions.

Probably because the standard says so, 17.6.1.1/2:

All library entities except macros, operator new and operator delete are defined within the namespace std or namespaces nested within namespace std.

And you would still need to put using ::swap sometimes, so it would introduce even more special cases. Here I use func instead of swap - http://ideone.com/WAWBfZ :

#include <iostream>
using namespace std;

template <class T>
auto func(T) -> void
{
cout << "::f" << endl;
}

namespace my_ns {
struct my_struct {};

auto func(my_struct) -> void
{
cout << "my_ns::func" << endl;
}

auto another_func() -> void
{
// won't compile without `using ::func;`
func(123);
}
}

auto main() -> int {}

fails with

prog.cpp: In function ‘void my_ns::another_func()’:
prog.cpp:21:17: error: could not convert ‘123’ from ‘int’ to ‘my_ns::my_struct’
         func(123);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM