Why use "std::cout" instead of “using namespace std;”?

Hi, I was wondering why code like std::cout is used instead of putting “using namespace std;” to avoid writing std:: all the time.

1 Like

It’s considered bad practice


Wow thanks, I had no idea. I learned the “using namespace std;” through an App called “learn C++.”

I ran into this same issue with another online tutorial! I’m glad that Ben explained it a little better about how we’ll be able to eliminate having to do both. :smiley:

Hi I am Levan please can you tell me more detail why is “using namespace std” considered bad practice?

Was the link I provided not helpful?

when you make order “using namespace std” namespace refer only std or all functions and variables which is given in program file?

in the link is talking about ambiguous variables and which variables is meaning?
pleas can you write me in summary here?

When you use using namespace std; you are making everything within the std namespace accessable without needing to prefix it with std::.

The example given in the link talks about a problem like the following

namespace Triangle
    int area(int width, int height)
        return width * height / 2;
namespace Rectangle
    int area(int width, int height)
        return width * height;
using namespace Triangle;
using namespace Rectangle;
int main()
    int x = area(3,5); //ambiguous call, Triangle::area or Rectangle::area?

thank you :slight_smile: I understand

When making your own classes, that objection applies. When using the standard library, the objection is patently silly. The whole point of having a standard library is to make life easier for future programmers. The way to do that is to simply use the std namespace. No one should be duplicating anything from the standard library, regardless of whether it’s been included with the using namespace directive or not. The only important rule is just to never put it in a header file.

Refer to http://www.cplusplus.com/doc/tutorial/namespaces/ for details.

“No one should be duplicating anything from the standard library”
Whilst that’s a bit of an exaggeration, the std namespace is large and includes many things it’s impossible to know whether or not a name you or some other library you are using are going to use the same names for different purposes.

The general guidence is you should never do it in a header file and it’s best practice not to do it in a source file, but if you insist on doing so, to localise it as much as possible. Or use a using declaration instead, that way you’re being explicit with what you add to the global namespace.

#include <iostream>
using std::cout;
int main()
    cout << "Hello\n";

C++ Primer 5th Ed:

Other remarks:

This may have already been stated, but as I have come to understand it, “using” a namespace is a bad idea because you can end up having multiple namespaces with methods that have the same name. Therefore, it’s better to just be specific about it because you circumvent this problem altogether.

Is that right, or is there more to it?

1 Like

Yes that is correct, and from what I understand it is actually beneficial by being easier to read your code or have someone else look through and follow if you have, for example “std::” included instead of using namespace.

In addition to the excellent feedback comments above, a variation of the ‘using’ directive with limited scope can be used as well, such as for example:

using std::iostream;
using std::endl;

This form of using directive avoids making the entire std namespace visible (which is huge), and allows you to scope-down for the compiler (and others reading the code) to the minimal visibility of std names listed, in this case only iostream and endl, and gives you the benefit of not having to use std:: before each name you listed with a using directive.

Where is code of game?

Hi @Alessandro_Capitano and dear Community,

to write “std::cout” instead of “using namespace std”+“cout” is more friendly if someone else is reading your code. The one, who is new to your code, doesn’t need to look where the command’s origin is. The programmer can it see the namespace directly within that line.

Kind regards

Privacy & Terms