C++中未命名命名空间的使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5312213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:53:10  来源:igfitidea点击:

Uses of unnamed namespace in C++

c++namespaces

提问by Asha

When would one use unnamed namespace in C++ ? Is it better in any sense than a free standing function? Also, should it be used only in source file and not in header file?

什么时候会在 C++ 中使用未命名的命名空间?它在任何意义上都比独立功能更好吗?另外,它应该只在源文件中使用而不是在头文件中使用吗?

采纳答案by T.E.D.

According to Stroustrup, you should use it in places where in old C you would have made staticglobals. The idea is that the items in question can be "global" to the source file they are in, but not pollute the namespace of any other source files in your compilation.

根据 Stroustrup 的说法,您应该在旧 C 中创建static全局变量的地方使用它。这个想法是,有问题的项目可以是它们所在的源文件的“全局”,但不会污染编译中任何其他源文件的命名空间。

In other words, you shouldn't be creating staticglobals in C++. You should be using unnamed namespaces instead.

换句话说,您不应该static在 C++ 中创建全局变量。您应该使用未命名的命名空间。

I have found some situations where they are useful in header files, but that should be rare. Mostly I think for declaring throwable exceptions. In that case the definitions in question will be global for everything that #includes that header, but not for things that don't.

我发现在某些情况下它们在头文件中很有用,但这应该很少见。大多数情况下,我认为是为了声明可抛出的异常。在这种情况下,所讨论的定义对于包含#include该标题的所有内容都是全局的,但不适用于不包含该标题的内容。

回答by sharptooth

Unnamed namespace is private to the translation unit and this can be used to shield global variables and functions with same names occurring in different translation units so that no link conflicts arise.

未命名命名空间是翻译单元私有的,这可用于屏蔽在不同翻译单元中出现的具有相同名称的全局变量和函数,从而不会出现链接冲突。

For example, you need a class that will only be defined in a .cpp file and used only within that file. You want to call it CModuleLock. If it is not in an unnamed namespace and some other .cpp file accidentially has another class CModuleLocknot in an unnamed namespace you won't be able to link your program.

例如,您需要一个仅在 .cpp 文件中定义并仅在该文件中使用的类。你想调用它CModuleLock。如果它不在未命名的命名空间中,并且某些其他 .cpp 文件意外地有另一个CModuleLock不在未命名的命名空间中的类,您将无法链接您的程序。

回答by Jeff Foster

It's used for name hiding. Each unnamed namespace is unique. The link hereexplains in more detail. It is typically used in a source file to hide functions that should only have internal linkage (e.g. not exposed to the outside world).

它用于名称隐藏。每个未命名的命名空间都是唯一的。这里的链接更详细地解释了。它通常用于源文件中以隐藏仅应具有内部链接(例如不暴露于外部世界)的函数。

回答by fbafelipe

Unnamed namespaces are the "C++ version" of global static variables and functions. Note that you can also use a unnamed namespace for classes.

未命名的命名空间是全局静态变量和函数的“C++ 版本”。请注意,您还可以为类使用未命名的命名空间。

回答by Nishant Sharma

Basically, namespace solve the conflicts between the same name classes, identifiers, and function.for more information click on the link given below https://simplifiedtutorial4u.blogspot.in/2017/08/what-is-namespace-in-c.html

基本上,命名空间解决了同名类、标识符和函数之间的冲突。有关更多信息,请单击下面给出的链接 https://simplifiedtutorial4u.blogspot.in/2017/08/what-is-namespace-in-c。 html