Bit*_*map 17 c++ namespaces using-declaration
这两种using关键字用法有什么区别:
using boost::shared_ptr;
Run Code Online (Sandbox Code Playgroud)
和
using namespace boost;
Run Code Online (Sandbox Code Playgroud)
Alo*_*ave 35
using boost::shared_ptr;
Run Code Online (Sandbox Code Playgroud)
仅包括shared_ptr来自boost于当前的命名空间的命名空间.这意味着您可以使用shared_ptr不使用命名空间限定它boost.
它被称为使用声明.
using namespace boost;
Run Code Online (Sandbox Code Playgroud)
包括boost当前范围中命名空间中的所有符号.这意味着您可以使用boost命名空间中的所有符号,而无需使用命名空间限定它们boost.
它被称为using指令.
你为什么总是喜欢using declaration过using directive?
最好使用first(using declaration)并避免使用second(using directive),因为第二个会导致命名空间污染,因为可能会将大量名称引入当前命名空间,其中许多都是不必要的.不必要的名称的存在极大地增加了意外名称冲突的可能性.
引用Herb Sutter用法using directive:
我觉得有必要把一个using directive疯狂的野蛮人的军队想象成一个疯狂的野蛮人,无论它经过什么地方都会不分青红皂白地发生破坏 - unintended conflicts,即使你认为你与它结盟也会导致它的存在.
| 归档时间: |
|
| 查看次数: |
8013 次 |
| 最近记录: |