我怎样才能“不使用”一个命名空间?

我的开发系统(Codegear C ++ Builder)的一个变幻莫测的是,一些自动生成的头坚持有…

using namespace xyzzy 

…在他们的声明,这至less会影响我的代码。

有没有办法,我可以取消/重写以前的“使用”语句,以避免这种情况。

也许…

 unusing namespace xyzzy; 

不。 但有一个潜在的解决scheme:如果你把你的include指令放在它自己的命名空间中,像这样…

 namespace codegear { #include "codegear_header.h" } // namespace codegear 

…那么这个头里的任何使用指令的效果都被中和了。

这在某些情况下可能会有问题。 这就是为什么每个C ++风格指南都强烈build议不要在头文件中使用“using namespace”指令。

不,你不能不使用命名空间。 你唯一能做的就是把using namespace语句放在一个块中来限制它的作用域。

例:

 { using namespace xyzzy; } // stop using namespace xyzzy here 

也许你可以改变使用自动生成头的模板。

您可能会在冲突时使用显式名称空间:

 string x; // Doesn't work due to conflicting declarations ::string y; // use the class from the global namespace std::string z; // use the string class from the std namespace 

为了将来的参考:因为XE版本有一个新的值,您可以#define避免using namespace System;可怕using namespace System; int包含:DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE

如何使用sed,perl或其他命令行工具作为生成过程的一部分来修改生成的标题生成后,但在它们被使用之前?

使用Visual Studio 2005进行快速实验表明,您可以将这些头文件放在自己命名的命名空间中,然后use此名称空间中的所需内容(但不要use整个名称空间,因为它将引入要隐藏的名称空间。

 #include<iostream> #include<stdio.h> namespace namespace1 { int t = 10; } namespace namespace2 { int t = 20; } int main() { using namespace namespace1; printf("%d" , t); printf("%d" , namespace2::t); }