“using namespace” in c++ headers

后端 未结 9 2352
失恋的感觉
失恋的感觉 2020-11-22 01:32

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

9条回答
  •  天涯浪人
    2020-11-22 02:06

    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.

提交回复
热议问题