C++ 如何“不使用”命名空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/167862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I "unuse" a namespace?
提问by Roddy
One of the vagaries of my development system (Codegear C++Builder) is that some of the auto-generated headers insist on having...
我的开发系统 (Codegear C++Builder) 的变幻莫测之一是一些自动生成的头文件坚持...
using namespace xyzzy
...statements in them, which impact on my code when I least want or expect it.
...其中的语句,当我最不想要或不期望它时会影响我的代码。
Is there a way I can somehow cancel/override a previous "using" statement to avoid this.
有没有办法我可以以某种方式取消/覆盖以前的“使用”语句来避免这种情况。
Maybe...
也许...
unusing namespace xyzzy;
采纳答案by Head Geek
Nope. But there's a potential solution: if you enclose your include directive in a namespace of its own, like this...
不。但是有一个潜在的解决方案:如果您将 include 指令包含在它自己的命名空间中,就像这样......
namespace codegear {
#include "codegear_header.h"
} // namespace codegear
...then the effects of any using directives within that header are neutralized.
...然后该标头中的任何 using 指令的效果都将被抵消。
That might be problematic in some cases. That's why every C++ style guide strongly recommends notputting a "using namespace" directive in a header file.
在某些情况下这可能是有问题的。这就是为什么每个 C++ 风格指南都强烈建议不要在头文件中放置“使用命名空间”指令。
回答by jk.
No you can't unusea namespace. The only thing you can do is putting the using namespace
-statement a block to limit it's scope.
不,你不能UNUSE命名空间。您唯一能做的就是将using namespace
-statement 放在一个块中以限制它的范围。
Example:
例子:
{
using namespace xyzzy;
} // stop using namespace xyzzy here
Maybe you can change the template which is used of your auto-generated headers.
也许您可以更改用于自动生成的标题的模板。
回答by Eclipse
You may be stuck using explicit namespaces on conflicts:
您可能会在冲突中使用显式命名空间:
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
回答by cdelacroix
For future reference : since the XE version there is a new value that you can #define to avoid the dreaded using namespace System;
int the include : DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE
供将来参考:由于 XE 版本有一个新值,您可以 #define 以避免可怕的using namespace System;
int 包括:DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE
回答by cdelacroix
How about using sed, perl or some other command-line tool as part of your build process to modify the generated headers after they are generated but before they are used?
如何在构建过程中使用 sed、perl 或其他一些命令行工具来修改生成的头文件,然后再使用它们?
回答by Kasprzol
Quick experiment with Visual Studio 2005 shows that you can enclose those headers in your own named namespace and then use
what you need from this namespace (but don't use
the whole namespace, as it will introduces the namespace you want to hide.
Visual Studio 2005 的快速实验表明,您可以将这些标头包含在您自己命名的命名空间中,然后use
将您需要的内容包含在该命名空间中(但不要use
包含整个命名空间,因为它会引入您想要隐藏的命名空间。
回答by Narendra kumawat
#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);
}