Ask coding questions

← Back to all posts
Why is using namespace std bad practice?
LoveFromSkyy (137)

I have seen the statement, "Using namespace std is bad practice". But every time I try to find out I get really confusing answers that make no sense to me. Can someone give me a simple answer as to why it is?

Answered by JBloves27 (1903) [earned 5 cycles]
View Answer
Comments
hotnewtop
programmeruser (613)

Let's say you have two namespaces, foo and bar. Let's say foo has function named qux and bar has a function with the same name. Now let's say you do this:

using namespace foo;
using namespace bar;

Now you have a naming conflict and the compiler complains about it.
using namespace pretty much goes against the meaning of namespaces.

RYANTADIPARTHI (6018)

it gets confusing sometimes.

19wintersp (1142)

Clutter, pretty much. Namespaces are useful for organising stuff, so if you pull everything out of their namespaces (with the using namespace directive) into the global namespace, it becomes cluttered, and you lose your organisation. In addition, if two namespaces have members with the same name (like foo::Baz() and bar::Baz()), using the using namespace directive causes them to break each other in the same way as calling two functions the same name would.