“using namespace”在c ++标题 [英] "using namespace" in c++ headers

查看:142
本文介绍了“using namespace”在c ++标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在所有的c ++课程中,所有的老师总是使用 #include 后面的命名空间std; 在 .h 文件中。这似乎在我看来是危险的,因为从那时通过包括该标题在另一个程序,我会得到的命名空间导入到我的程序,也许没有意识到,打算或想要它(头包含可以非常深的嵌套)。

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).

所以我的问题是double:我是对的使用命名空间不应该在头文件中使用,和/或有一些方法undo it,like:

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 {
.
.
.
}

同样的问题: c> #include 所有与它对应的头文件 .cpp 文件需要,只有头文件定义需要的那些头文件, $ c> .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.

谢谢。

推荐答案

你绝对不应该使用使用命名空间在标题中,正是你说的原因,它可以意外地改变代码在任何其他包含该标题的文件。没有办法撤消使用命名空间这是另一个很危险的原因。我通常只是使用 grep 或类似的,以确保使用命名空间不是在标题中调用,而不是尝试任何更复杂。也许静态代码检查器也标记这一点。

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 ,因为您可以完全控制这样的forward声明类。

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.

这篇关于“using namespace”在c ++标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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