“using namespace” in c++ headers

2018-12-31 06:15发布

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

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

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.

Thank you.

9条回答
临风纵饮
2楼-- · 2018-12-31 07:16

You need to be careful when including headers inside of headers. In large projects, it can create a very tangled dependency chain that triggers larger/longer rebuilds than were actually necessary. Check out this article and its follow-up to learn more about the importance of good physical structure in C++ projects.

You should only include headers inside a header when absolutely needed (whenever the full definition of a class is needed), and use forward declaration wherever you can (when the class is required is a pointer or a reference).

As for namespaces, I tend to use the explicit namespace scoping in my header files, and only put a using namespace in my cpp files.

查看更多
情到深处是孤独
3楼-- · 2018-12-31 07:20

You are right that using namespace in header is dangerous. I do not know a way how to undo it. It is easy to detect it however just search for using namespace in header files. For that last reason it is uncommon in real projects. More experienced coworkers will soon complain if someone does something like it.

In real projects people try to minimize the amount of included files, because the less you include the quicker it compiles. That saves time of everybody. However if the header file assumes that something should be included before it then it should include it itself. Otherwise it makes headers not self-contained.

查看更多
泪湿衣
4楼-- · 2018-12-31 07:22

With regards to "Is there some way to undo [a using declaration]?"

I think it is useful to point out that using declarations are affected by scope.

#include <vector>

{   // begin a new scope with {
    using namespace std;
    vector myVector;  // std::vector is used
}   // end the scope with }

vector myOtherVector;   // error vector undefined
std::vector mySTDVector // no error std::vector is fully qualified

So effectively yes. By limiting the scope of the using declaration its effect only lasts within that scope; it is 'undone' when that scope ends.

When the using declaration is declared in a file outside of any other scope it has file-scope and affects everything in that file.

In the case of a header file, if the using declaration is at file-scope this will extend to the scope of any file the header is included in.

查看更多
登录 后发表回答