3.8. Prefer using declarations to using namespace std
¶
What’s wrong with using namespace std;
?
Nothing, technically. You see it commonly in examples on the web and often the intent is to make an example appear as simple as possible with no extra clutter.
BUT
The using-directive using namespace std;
at any namespace scope introduces
every name from the namespace std
into the global namespace,
which may lead to undesirable name collisions.
This, and other using directives are generally considered bad practice
at file scope of a header file.
Additionally, shadowing names in the standard namespace can lead to unexpected behaviors.
It can be hard to remember every name that might be
imported when using namespace std;
.
Even when only 1 header is included.
The following example seems innocent enough,
until you learn that showpoint is a name
in std::ios
Run the following example twice,
first as is, then remove the line bool showpoint = true;
:
Errors using namespace directives are seldom this obviously wrong.
One final word from two experts:
Summary
Namespace usings are for your convenience, not for you to inflict on others: Never write a
using
declaration or ausing
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
#include
might appear after them.)Discussion
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, namespaceusing
declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.—Herb Sutter and Andrei Alexandrescu, C++ Coding Standards
More to Explore
From: cppreference.com: namespace declarations and namespace aliases
From stack overflow: Why is using namespace std considered bad practice?