Using namespace std

I'm fairly new to coding but I've noticed something on this forum (at least, maybe in other places); why is "std:cout"
used instead of using namespace std and then just using cout?

Is there some kind of benefit to this or is it just a case of better formatting/looking professional?
This has been covered many times.
Here's a good explanation.
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
using namespace std; is bad practice for a variety of reasons, all of which you can find on google. One of those reasons is that it completely negates the entire point of namespaces.

One recent example of why it's bad:
http://www.cplusplus.com/forum/general/176013/
In that case there were multiple versions of pow and the compiler didn't know which to pick - this is the entire point of namespaces to begin with.

Another example from this forum (I don't have the link) - someone tried to declare a class called hash and got all sorts of weird errors when they tried to use it, unaware that there existed std::hash().
In fact, some are speculating this is what has happened at United Airlines. Their computers at the moment are worthless. Boarding passes and tickets being written by hand. Talk about a terrorists dreams come true!!
How could using namespace cause this you ask?? Continue reading...

From Stack Overflow -- consider this: You are using two libraries called Foo and Bar:
Everything works fine, 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 have used foo::Blah() and bar::Quux() then the introduction of foo::Quux() would have been a non-event. 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.

Topic archived. No new replies allowed.