为什么不是 std::swap 在全局命名空间中

Why is not std::swap in global namespace?

本文关键字:全局 命名空间 swap std 为什么不      更新时间:2023-10-16

有效 c++ 第三版中的第 25 项,Scott Meyers 建议在与类相同的命名空间中实现交换,然后在交换时使用 使用std::swap,作者在那里说:

例如,如果您要以这种方式编写调用交换:

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

您将强制编译器仅考虑 std 中的交换,因此 消除了获得更合适的 T 特异性的可能性 在其他地方定义的版本。唉,一些被误导的程序员确实有资格 以这种方式调用交换,这就是为什么完全 专门化标准::交换为您的班级。

作者建议始终以这种方式交换对象:

#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);
}

为什么全局命名空间中不std::swap?这样,添加自定义交换函数会更简单。

可能是因为标准是这样说的,17.6.1.1/2:

除宏、运算符 new 和运算符删除之外的所有库实体都在命名空间 std 或嵌套在命名空间 std 中的命名空间中定义。

而且您有时仍然需要放置using ::swap,因此它会引入更多特殊情况。在这里我使用 func 而不是 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 {}

失败与

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);