What's the problem with "using namespace std;"?
c++, c++-faq, namespaces, std, using-directives
Solution
Consider 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.
Problem
I have heard `using namespace std;` is wrong, and that I should use `std::cout` and `std::cin` directly instead. Why is this? Does it risk declaring variables that share the same name as something in the `std` namespace? Are there performance implications?
Related problems
- how to provide a swap function for my class?
- Why does a swap function taking two pointers only work with using namespace std?
- How to use an iterator?
- What is a namespace alias?
- C++ Standard Library: How to write wrappers for cout, cerr, cin and endl?
- Does C++11 change the behavior of explicitly calling std::swap to ensure ADL-located swap's are found, like boost::swap?
- using std::<type> v.s. using std namespace