Why is using namespace std; considered bad practice?
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.
What is the function of using namespace:std; in C++?
One concept in c++
are namespaces. This organizes your code in a way.
What is using namespace std;
do now? Let us explore this by example.
#include <iostream>
int main() {
std::cout << "Hello World" << std::endl; // important line
return 0;
}
You see the std
keyword in the line with the comment. This is called a namespace. So you tell the compiler, you want to use cout
from the namespace std
.
With using namespace std;
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl; // important line
return 0;
}
There you tell the compiler, to open the std
namespace four you in this scope. So you can use cout
without the std::
. This could be mistaken to be more efficent to code. But you will run into problems at one point.
In the namespace std
are hunderts or thounsands of functions or constants and so on defined. Most of the time you won't bother with that. But if you define a function with the same name and parameters at one point, you hardly will be able to find the mistake. For example there is std::find
. There may be a chance you define the same function. The compiler errors in this case are a pain. So I would strongly discurage you to use using namespace std;
.
What is using namespace::std in C++
using namespace::std
is the same as using namespace std;
The ::
symbol is the scope resolution operator. When used without a scope name before it , it refers to the global namespace. This means that std
is a top level namespace, and is not enclosed in another.
The spaces before and after the ::
are optional in this case because the lexer can deduce the tokens from the context.
For example, all of the following are valid:
namespace A { namespace notstd{} } // define my own namespaces A and A::notstd
using namespace::std; // the standard library std
using namespace A;
using namespace ::A;
using namespace::A;
using namespace A::notstd;
Update:
As noted in one of the comments, using namespace ::std;
and using namespace std;
may actually lead to different results if the statement comes inside another namespace which contains its own nested namespace std
. See the following (highly unlikely) example:
#include <stdio.h>
namespace A {
namespace std {
int cout = 5;
}
using namespace std;
void f1() {
cout++;
}
}
int main()
{
A::f1();
printf("%d\n",A::std::cout); // prints 6
return 0;
}
Related Topics
Pointer Array and Sizeof Confusion
How to Set the Cout Locale to Insert Commas as Thousands Separators
Implementing a Std::Vector Like Container Without Undefined Behavior
Vector<Unique_Ptr<A> > Using Initialization List
C++11 Std::Mutex in Visual Studio 2012 Deadlock When Locked from Dllmain()
Boost Asio - How to Write Console Server
Are Variadic MACros Nonstandard
Does C++11 Allow Dollar Signs in Identifiers
Why Must Virtual Base Classes Be Constructed by the Most Derived Class
What Does Iota of Std::Iota Stand For
Adding Multiple Executables in Cmake
Should Every Class Have a Virtual Destructor
What Is the Recommended Way to Align Memory in C++11
Differencebetween Const_Iterator and Iterator