我应该在实现文件中使用未命名的名称空间吗?
我在外部 *.cpp 文件中定义了一些函数(这里不涉及类),当然还有一个合适的 *.h 文件。
*.cpp 文件中的某些函数仅在该 *.cpp 文件中使用,其他地方不使用。它们甚至没有在 *.h 文件中提及。
我应该将这些函数放入未命名的命名空间中,还是可以将它们放在其他函数旁边?如果是这样,为什么我需要为它们提供一个未命名的名称空间?我看不出有什么问题,因为无论如何都无法从外部访问这些功能。
I have defined some functions (no classes involved here) in an external *.cpp file, and of course there is an appropriate *.h file.
Some of the functions in the *.cpp file are used only in that *.cpp file nowhere else. They are not even mentioned in the *.h file.
Should I put those functions into an unnamed namespace or may they live just next to the other functions? And if so, why should I need an unnamed namespace for them? I cannot see a problem, since those functions are not accessible from outside anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您希望它们对于该编译单元来说是真正私有的,请将它们放在匿名名称空间中。如果您不这样做,那么有人可以在其他地方声明这些函数并显式使用它们。
采取以下示例:
library.cpp
:client.cpp
:If you want them to be truly private to that compilation unit, put them in an anonymous namespace. If you don't then someone could declare those functions elsewhere and use them explicitly.
Take the following example:
library.cpp
:client.cpp
:您的问题可以分为两部分:
1。 “如何隐藏全局函数?”
一种简单的方法是不将函数的标头放入头文件中:
完整代码文件:
编译时,你会得到一个“mylibrary.o”或“mylibrary.obj”文件。您可以将其提供给其他开发人员:“mylibrary.hpp”加“mylibrary.obj”,但不包含“mylibrary.cpp”文件。大多数“plain c”/“c++”编译器都可以这种方式工作。
还有其他方法,请阅读下一节。
2. “匿名命名空间是隐藏全局函数的好技术吗?”
“匿名命名空间”技术是隐藏全局函数的另一种方法。
有一个类似的问题:
未命名/匿名命名空间与静态函数
但是就我个人而言,我不推荐这种技术,因为它是“最喜欢的”答案。
命名空间就是其中之一,我希望它从“纯c”或“c++”开始就存在。但是,“匿名名称空间”或“未命名名称空间”使用起来似乎很奇怪。
这就像试图隐藏某些东西,然后忘记将其存储在哪里。
3 其他建议
(a) 我建议每个文件使用一个主要的必需的,而不是可选的、非匿名的命名空间。它可能具有嵌套的附加内部名称空间。每个主命名空间应该具有相同的 id。作为文件名,但不带文件扩展名或文件后缀。
(b) 避免匿名名称空间。这就像将东西存储在仓库中,没有索引。
(c) 在头文件中使用文件扩展名或文件前缀,可能是“.h”或“.hpp”,即使它是一个 C++ 文件。标准规定,c++ 不应在“c++”文件上使用文件扩展名或文件后缀,但这些文件扩展名或文件后缀在文件系统上很难识别或查找。
祝你好运。
Your question can be split in two:
1. "How can I hide global functions ?"
One simple way to do that, is NOT to put the header of the function into the header file:
Full code file:
When this is compiled, you get a "mylibrary.o" or "mylibrary.obj" file. You may provide it, to other developers as: "mylibrary.hpp" plus "mylibrary.obj", but, without the "mylibrary.cpp" file. Most "plain c" / "c++" compilers can work this way.
There are other ways, read the next section.
2. "Are anonymous namespaces, a good technique to hide global functions ?"
The "Anonymous Namespaces" technique, is another way to hide global functions.
There is a similar question on:
Unnamed/anonymous namespaces vs. static functions
But, personally, I don't recommend this technique, as the "favorite" answer.
Namespaces are one of those things, that I wish existed since the start of "pure c" or "c++". But, "anonymous namespaces" or "unnamed namespaces", seems weird to use.
Its like trying to hide something, and, later, forget, where do you store it.
3 Additional suggestions
(a) I suggest to use a single main REQUIRED, not optional, non-anonymous namespace per file. It may have nested, additional inner namespaces. Each main namespace, should have the same id. as the filename, but, without the file extension or file suffix.
(b) Avoid anonymous namespaces. Its like storing things in a warehouse, without an index.
(c) Use a file extension, or file prefix, in your header files, maybe ".h", or ".hpp", even if if its a c++ file. The standard says c++ shouldn't use an file extension or file suffix on "c++" files, but are difficult to identify or find on the filesystem.
Good Luck.
我想如果您不希望从外部看到这些函数,请将它们声明为
static
。I guess if you don't want these functions to be seen from outside declare them as
static
.