为什么不是 std::swap 在全局命名空间中
Why is not std::swap in global namespace?
有效 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);
相关文章:
- 在命名空间中使用全局命名空间中的函数
- 将好友从模板导出到全局命名空间
- Catalina C++:使用<cmath>标头产生错误:全局命名空间中没有名为 'signbit' 的成员
- 将以"_[a-z0-9]"开头和"using"的标识符导入全局命名空间是否定义良
- 为什么不允许在全局命名空间处阻止范围?
- 运算符<<依赖于参数的查找不在全局命名空间中查找
- 我可以停止 GCC 将标准库名称包含在全局命名空间中吗?
- "uintmax_t":不是"全局命名空间"的成员
- C++项目中的全局命名空间
- 使用 "::member" 引用全局命名空间有什么用吗?
- 在全局命名空间中重载不依赖于用户定义类型的标准定义类型的运算符是否格式正确?
- 使用全局命名空间限定符和指向数据成员的指针
- InitializeCriticalSectionEx 不是 atlwinverapi.h 中全局命名空间的成员
- C++在命名空间中声明与全局命名空间中的函数同名的函数
- 在vs2010中使用iostream时,出现错误C2039:'exit':不是"全局命名空间"的成员
- 是否可以恢复到"default"全局命名空间?
- C++错误代码全局命名空间
- equal()是否默认包含在全局命名空间中
- 前向声明和全局命名空间声明
- C++在全局命名空间中找不到非标准 C 函数