+ 3

Is "using namespace std;" considered bad?

Consider this: you are using two libraries called Foo and Bar: using namespace foo; using namespace 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 had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event. It can also introduce you to a couple of hard-to-find bugs, so its general use should be abandoned at all. If you are sick of typing "std::cout" everytime, there is the way of "using std::cout" and achieving the same as using namespace std - without polluting your namespace.

19th Dec 2016, 11:48 AM
John Doe
John Doe - avatar
1 Resposta
+ 7
^ All of the above. With or without it, it has nothing to do with performance. What it does is to dump std in front of all identifiers. If you rely heavily on a specific namespace, it would be good utilise it. Else, it would be a great option to std::cout, std::cin, etc.
19th Dec 2016, 1:05 PM
Hatsy Rei
Hatsy Rei - avatar