|
|
|
|
Don’t write using namespace at global scope in a header file Reason: Doing so takes away an #includer’s ability to effectively disambiguate and to use alternatives. It also makes #included headers order-dependent as they may have different meaning when included in different orders. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using-directive |
Don't write namespace usings in a header file or before an #include. Summary Namespace usings are for your convenience, not for you to inflict on others: Never write a using declaration or a using directive before an #include directive. Corollary: In header files, don't write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. (The second rule follows from the first, because headers can never know what other header #includes might appear after them.) |
Use using namespace directives for transition, for foundation libraries (such as std), or within a local scope (only) Reason: using namespace can lead to name clashes, so it should be used sparingly. However, it is not always possible to qualify every name from a namespace in user code (e.g., during transition) and sometimes a namespace is so fundamental and prevalent in a code base, that consistent qualification would be verbose and distracting. https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-using |
In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable. |
In implementation files (e.g. .cpp files), the rule is more of a stylistic rule, but is still important. Basically, using explicit namespace prefixes makes the code clearer, because it is immediately obvious what facilities are being used and where they are coming from. And more portable, because namespace clashes cannot occur between LLVM code and other namespaces. The portability rule is important because different standard library implementations expose different symbols (potentially ones they shouldn't), and future revisions to the C++ standard will add more symbols to the std namespace. As such, we never use 'using namespace std;' in LLVM. |