Why is "using namespace std;" considered bad practice?

I've been told by others that writing using namespace std; in code is wrong, and that I should use std::cout and std::cin directly instead.

Why is using namespace std; considered a bad practice? Is it inefficient or does it risk declaring ambiguous variables (variables that share the same name as a function in std namespace)? Does it impact performance?


This is not related to performance at all. But consider this: you are using two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

Everything works fine, and you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo 2.0, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event.

Why Is the Sky Blue?, directions by all the gases and particles in the air. Blue light is scattered more than the other colors because it travels as shorter, smaller waves. This is why we​  4. the reason for which: That is why he returned. n. 5. a question concerning the cause or reason for which something is done, achieved, etc.: a child's unending whys. 6. the cause or reason: the whys and wherefores of the Cold War.


I agree with everything Greg wrote, but I'd like to add: It can even get worse than Greg said!

Library Foo 2.0 could introduce a function, Quux(), that is an unambiguously better match for some of your calls to Quux() than the bar::Quux() your code called for years. Then your code still compiles, but it silently calls the wrong function and does god-knows-what. That's about as bad as things can get.

Keep in mind that the std namespace has tons of identifiers, many of which are very common ones (think list, sort, string, iterator, etc.) which are very likely to appear in other code, too.

If you consider this unlikely: There was a question asked here on Stack Overflow where pretty much exactly this happened (wrong function called due to omitted std:: prefix) about half a year after I gave this answer. Here is another, more recent example of such a question. So this is a real problem.


Here's one more data point: Many, many years ago, I also used to find it annoying having to prefix everything from the standard library with std::. Then I worked in a project where it was decided at the start that both using directives and declarations are banned except for function scopes. Guess what? It took most of us very few weeks to get used to writing the prefix, and after a few more weeks most of us even agreed that it actually made the code more readable. There's a reason for that: Whether you like shorter or longer prose is subjective, but the prefixes objectively add clarity to the code. Not only the compiler, but you, too, find it easier to see which identifier is referred to.

In a decade, that project grew to have several million lines of code. Since these discussions come up again and again, I once was curious how often the (allowed) function-scope using actually was used in the project. I grep'd the sources for it and only found one or two dozen places where it was used. To me this indicates that, once tried, developers don't find std:: painful enough to employ using directives even once every 100 kLoC even where it was allowed to be used.


Bottom line: Explicitly prefixing everything doesn't do any harm, takes very little getting used to, and has objective advantages. In particular, it makes the code easier to interpret by the compiler and by human readers — and that should probably be the main goal when writing code.

This Should Be V.R.'s Moment. Why Is It Still So Niche?, Why Is It Still So Niche? Virtual escapism is perfect for a pandemic, but V.R. still seems far from a mainstream technology. the reason for which: That is why he returned. noun, plural whys. a question concerning the cause or reason for which something is done, achieved, etc.: a child's unending hows and whys.


Terry Moore: Why is 'x' the unknown?, Why is 'x' the symbol for an unknown? In this short and funny talk, Terry Moore gives the surprising answer. Sunlight reaches Earth's atmosphere and is scattered in all directions by all the gases and particles in the air. Blue light is scattered more than the other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time.


I recently ran into a complaint about Visual Studio 2010. It turned out that pretty much all the source files had these two lines:

using namespace std;
using namespace boost;

A lot of Boost features are going into the C++0x standard, and Visual Studio 2010 has a lot of C++0x features, so suddenly these programs were not compiling.

Therefore, avoiding using namespace X; is a form of future-proofing, a way of making sure a change to the libraries and/or header files in use is not going to break a program.

Why is Cats?, Don't get cocky. Nebula exclusive Les Miserables video: https://watchnebula.com​/lindsay-ellis-tom-hoopers-les-miserables/ Get a free month  Five whys (or 5 whys) is an iterative interrogative technique used to explore the cause-and-effect relationships underlying a particular problem. The primary goal of the technique is to determine the root cause of a defect or problem by repeating the question "Why?".


Short version: don't use global using declarations or directives in header files. Feel free to use them in implementation files. Here's what Herb Sutter and Andrei Alexandrescu have to say about this issue in C++ Coding Standards (bolding for emphasis is mine):

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.)

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, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.

Coronavirus: Why is NYC reporting surge in virus deaths?, New York is now reporting an additional 3778 deaths presumed to be coronavirus-linked. Cholesterol is a substance made in the liver that’s vital to human life. You can also get cholesterol through foods. Since it can’t be created by plants, you can only find it in animal products like meat and dairy. In our bodies, cholesterol serves three main purposes: It aids in the production of sex hormones.


What We Know So Far About SARS-CoV-2, Why was this seventh coronavirus the one to go pandemic? Suddenly, what we do know about coronaviruses becomes a matter of international  Germs from unwashed hands can get into foods and drinks while people prepare or consume them. Germs can multiply in some types of foods or drinks, under certain conditions, and make people sick. Germs from unwashed hands can be transferred to other objects, like handrails, table tops, or toys,


Why Is COVID-19 Coronavirus Causing Strokes In Young And , Young and middle-aged people with COVID-19 are presenting to hospitals with severe strokes. We are just beginning to unravel the reasons  Why a negative times a negative makes sense Our mission is to provide a free, world-class education to anyone, anywhere. Khan Academy is a 501(c)(3) nonprofit organization.


Why is the CDC 'sitting on the sidelines' in the Covid-19 fight?, A CDC veteran asks: Why is the agency 'sitting on the sidelines' in the fight against Covid-19? By Pierre E. Rollin. March 26, 2020. CDC lab Liberia In Monrovia  Many people don't understand why or how other people become addicted to drugs. They may mistakenly think that those who use drugs lack moral principles or willpower and that they could stop their drug use simply by choosing to. In reality, drug addiction is a complex disease, and quitting usually takes more than good intentions or a strong will.