“使用命名空间"在 C++ 头文件中 [英] "using namespace" in c++ headers

查看:76
本文介绍了“使用命名空间"在 C++ 头文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们所有的 c++ 课程中,所有的老师总是将 using namespace std; 放在他们的 .h 文件中的 #include 之后.在我看来,这似乎很危险,因为通过在另一个程序中包含该头文件,我会将命名空间导入到我的程序中,也许没有意识到、有意或想要它(头文件包含可以非常深入地嵌套).

In all our c++ courses, all the teachers always put using namespace std; right after the #includes in their .h files. This seems to me to be dangerous since then by including that header in another program I will get the namespace imported into my program, maybe without realizing, intending or wanting it (header inclusion can be very deeply nested).

所以我的问题是双重的:using namespace 不应该在头文件中使用,和/或有什么方法可以撤消它,例如:

So my question is double: Am I right that using namespace should not be used in header files, and/or is there some way to undo it, something like:

//header.h
using namespace std {
.
.
.
}

还有一个问题:头文件 #include 是否应该包含它对应的 .cpp 文件需要的所有头文件,只有头文件需要的那些头文件定义并让 .cpp 文件 #include 剩下的,或者没有并声明它需要的一切为 extern?
问题背后的原因与上述相同:我不希望在包含 .h 文件时出现意外.

One more question along the same lines: Should a header file #include all the headers that it's corresponding .cpp file needs, only those that are needed for the header definitions and let the .cpp file #include the rest, or none and declare everything it needs as extern?
The reasoning behind the question is the same as above: I don't want surprises when including .h files.

另外,如果我是对的,这是一个常见的错误吗?我的意思是在现实世界的编程和那里的真实"项目中.

Also, if I am right, is this a common mistake? I mean in real-world programming and in "real" projects out there.

谢谢.

推荐答案

你绝对不应该在标题中使用 using namespace 正是因为你说的原因,它会意外地改变代码的含义包含该标题的任何其他文件.没有办法撤销 using namespace 这是它如此危险的另一个原因.我通常只使用 grep 或类似的东西来确保 using namespace 不会在标题中被调用,而不是尝试任何更复杂的东西.可能静态代码检查器也标记了这一点.

You should definitely NOT use using namespace in headers for precisely the reason you say, that it can unexpectedly change the meaning of code in any other files that include that header. There's no way to undo a using namespace which is another reason it's so dangerous. I typically just use grep or the like to make sure that using namespace isn't being called out in headers rather than trying anything more complicated. Probably static code checkers flag this too.

头文件应该只包含它需要编译的头文件.强制执行此操作的一种简单方法是始终将每个源文件自己的标头作为第一件事,在任何其他标头之前.如果标头不是自包含的,那么源文件将无法编译.在某些情况下,例如引用库中的实现细节类,您可以使用前向声明而不是 #include,因为您可以完全控制此类前向声明​​类的定义.

The header should include just the headers that it needs to compile. An easy way to enforce this is to always include each source file's own header as the first thing, before any other headers. Then the source file will fail to compile if the header isn't self-contained. In some cases, for example referring to implementation-detail classes within a library, you can use forward declarations instead of #include because you have full control over the definition of such forward declared class.

我不确定我是否会称它为普通,但它肯定会偶尔出现,通常是由不知道负面后果的新程序员编写的.通常只需对风险进行一点教育即可解决所有问题,因为解决起来相对简单.

I'm not sure I would call it common, but it definitely shows up once in a while, usually written by new programmers that aren't aware of the negative consequences. Typically just a little education about the risks takes care of any issues since it's relatively simple to fix.

这篇关于“使用命名空间"在 C++ 头文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆