Why not using namespace std?

Hello :)

Was wondering why some people prefere not to use namespace std;
and type std::cout<< x; instead of just cout<< x; .

So my question would be, what are the downsides of using namespace std;?

Thanks on advance
An important reason for namespaces to exist is to avoid name clashes. Now if you were to import all existing namespaces, that would defeat their purpose to some extent, as you'd have name clashes again (although you could resolve these by explicitly specifying the right namespace). However, it is generally fine to import the std namespace, as most library designers will avoid name conflicts with the standard library. The chance that you will run into problems by importing std is very low. However, it will save you a lot of time, since you don't have to prepend std:: to every single use of a standard library function, which will also make your code more readable.
Just keep using namespace std; out of headers, because that would force the std namespace on everyone who includes one of your headers.
Last edited on
Athar wrote:
However, it will save you a lot of time, since you don't have to prepend std:: to every single use of a standard library function

I'd argue that std:: enables auto completion, which also saves time and helps to avoid typos.
Depends on auto-completion settings
closed account (z05DSL3A)
So my question would be, what are the downsides of using namespace std;?

using Declarations, ie:
1
2
3
using std::cout;
using std::cin;
using std::endl

add names to the scope in which they are declared. The effect of this is:
» a compilation error occurs if the same name is declared elsewhere in the same scope;
» if the same name is declared in an enclosing scope, the the name in the namespace hides it.

The using directive, ie using namespace std;, does not add a name to the current scope, it only makes the names accesable from it. This means that:
» If a name is declared within a local scope it hides the name from the namespace;
» a name in a namespace hides the same name from an enclosing scope;
» a compilation error occurs if the same name is made visible from multiple namespaces or a name is made visible that hides a name in the global name space.
Topic archived. No new replies allowed.