Take the 2-minute tour ×

Possible Duplicate:

The other day when I asked a question someone replied saying if someone asks a question, show them the right way to do it instead of using namespace std; which I thought was a bit weird, as using namespace std; is way easier, But I guess I'm failing right now as I am a 'beginner' coder and you guys know better.

So I guess my question is: Why std:: instead of using namespace std;?


share|improve this question

marked as duplicate by Cody Gray, Tadeusz Kopec, MSalters, Tony D, FredOverflow Mar 29 '11 at 8:05


See stackoverflow.com/questions/1452721/… –  Sapph Mar 29 '11 at 7:18

6 Answers 6

Namespace(s) are additional qualifiers for our variables. Lets say we have 'string' defined in std and now if we define a 'string' in mynamespacealso.

Now, if I write using namespace std;at the top of a file, then from there onwards a string becomes ambiguous for a compiler.

One can however take a middle approach, of strictly not having using namespace std;in a header(.h) file, since others might want to use your class and can get conflicts. While for an implementation (.cxx) file, you can be careful to use it if you are sure there won't be any conflicts.

share|improve this answer






share|improve this answer

I think it is some what a preference thing. Some people like to see the explicit namespaces when using the classes. One exception is I never to use a using namespace std in a header file. As this can unexpectedly change the behaviour of a class that is using this header file.

share|improve this answer

I don't think it's the case that more experienced programmers use explicit namespaces, see e.g. Do you prefer explicit namespaces or 'using' in C++?

Note however that you should never import namespaces in header files and that in some cases explicit namespaces are clearer, for example with the functions std::min() and std::max()

share|improve this answer

Simply put, you are less likely to use the wrong types or functions by mistake, or name conflicts. Say you are using your own math library, plus std, and declare using both of them, in some arbitrary order. Now, they both define function pow. Which pow are you using when you invoke pow? I think it is worth the extra typing.

share|improve this answer

From C++ FAQ:

[27.5] Should I use using namespace std in my code?

Probably not.

People don't like typing std:: over and over, and they discover that using namespace std lets the compiler see any std name, even if unqualified. The fly in that ointment is that it lets the compiler see any std name, even the ones you didn't think about. In other words, it can create name conflicts and ambiguities.


share|improve this answer
+!: Nice quote, says it all. –  Binary Worrier Mar 29 '11 at 7:16
Are there any efficiency gains or is that a non-issue? –  chustar Mar 29 '11 at 7:19
@chustar: What do you mean by "efficiency"? –  Cody Gray Mar 29 '11 at 7:22
That is all done during compilation, there is no difference in the resultant machine code. –  Ed S. Mar 29 '11 at 7:29
@chustar: It might compile a tiny bit faster with std::foo because of resolved ambiguities. But I don't think it matters much. –  LumpN Mar 29 '11 at 7:31
