A using directive makes names from another namespace available in the current scope. It should only be used when those names do not create an
ambiguity with other names, otherwise, it is better to fully qualify the names you want to use.
When you write a header file, you don’t know from which context it will be included. Therefore, if this header contains using directives, you
cannot be sure that they will not create ambiguities in that context. Those ambiguities could lead to compilation failures or, worse, to a different
function being selected by overload resolution depending on the order of inclusion of headers.
A using declaration behaves in the same way but only for one name. Because of their much narrower scope, this rule does not apply to using
Noncompliant Code Example
void foo ( char_t a );
void foo( int32_t a );
inline void bar ( )
foo ( 0 );
using namespace NS1; // Noncompliant
int32_t m1 ( )
bar ( ); // bar calls foo ( char_t );
void m2 ( )
bar ( ); // bar calls foo ( int32_t );
The issue only happens if the using directive is at global scope or at namespace scope. If is is inside a function body, it will cease to be in
effect at the end of the current scope, and will not propagate to the users of the header file.
- MISRA C++:2008, 7-3-6 - using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used
in header files.
- C++ Core Guidelines SF.7 - Don’t write
using namespace at global scope in a header file