views:

327

answers:

7

Possible Duplicate:
Why is 'using namespace std;' considered a bad practice in C++?

Every time I use using namespace std I always get that "thats a terrible programming habit". Now I'm graduating this December with my B.S. in C.S. but I don't claim to know everything, but no one has ever explained why this is so bad. I understand what it does but I honestly don't see a huge deal with it.

Anyone care to explain? In my mind it just makes typing cout a whole lot more bearable than std::cout.

I can understand why you wouldn't want to put it in a header file, but just in a normal implementation file... I dont see why it would be a problem.

+2  A: 

My preference is to:

  1. never put a using directive in a header file (the things that include your header may not like the fact that you forced them to have the using directive).

  2. always do things like using std::cout; at the top of the implementation files so I don't have to do std::cout everywhere in my code.

TofuBeer
terminology check: using namespace std; and the like are called using directives (not using statements). using std::cout etc. are called using declarations. :)
Armen Tsirunyan
Been a long time since I did c++ - thx I'll fix it :-)
TofuBeer
+2  A: 

a "good practice" that I am aware of is not to put using namespace in include files, but be free to use it to your taste in your private .cpp files. I know people who like everything to be fully qualified, and some (like me) who assume that string is an std::string unless stated otherwise.

The reason for this is that if/when others use your include file (and this happens always), they are forced to accept your programming style.

Good luck!

davka
+8  A: 

found this useful post elsewhere:

Namespaces separate and organize functionality. You can have a "xander333::sort()" function and it won't conflict with "std::sort()" or "boost::sort()" or any other sort(). Without namespaces there can be only one sort().

Now let's say you've put "using namespace std;" in all your source files and you've implemented a simple templated function called "fill()" in the global namespace of one of your files. This file also depends on a header from libFoo -- "foo.hpp". Version 2.1 of libFoo comes out and all of a sudden your program no longer compiles. You version of "fill()" suddenly conflicts with another "fill()"! What happened???

It turns out that the folks implementing libFoo included in the new version of "foo.hpp" when they didn't before. Now you have all of the standard algorithms being included in your source file, and your "using namespace std;" has pulled them all into the global namespace. std::fill() now directly conflicts with your fill().

More insidious, you've gotten your code to compile by renaming your fill() to xander333_fill(), but something's not working right -- your report numbers are off. It turns out that your custom divides() function, which does fixed precision math, is no longer being called because the templated function from (also newly included by "foo.hpp") makes for a better match because you're calling types did not exactly match the declared types.

Thread with relevant discussion is here:

http://www.cplusplus.com/forum/unices/27805/

Orbit
Or to state more simply, it's easy to get name collisions in the future and they can be insidious to correct after the fact.
Inverse
+6  A: 

There is no problem using using namespace std in your source file when you make heavy use of the stl and know for sure that nothing will collide.

However, very often you don't need to use using namespace std or not in the entire file:

Did you know you can:

void somefunction()
{
  // Use it in a particular scope
  using namespace std;

  cout << "test" << endl;
}
ereOn
+1  A: 

It's primarily about good housekeeping. If you're not really going to use more than a few identifiers in a namespace, why clutter up your own namespace by dumping all of the identifiers from that namespace into yours? It's preferable to use using std::cout. However, if you use a namespace very heavily and it doesn't cause any collisions, go ahead and use using namespace.

Eric Mickelsen
A: 

Avoiding using statements for entire namespaces helps prevent unintentional conflicts between libraries. Supposed you made your own class that had the same name as something in std, then unless you explicitly use std:: you will have name conflicts.

It's probably best to try to avoid conflicts like this in the first place, but if you specify the namespace for each member in your code it will be less ambiguous.

If you get tired of typing std::cout all the time, you can use a using statement for just that member.

bde
A: 

Another reason to not use using other than avoiding potential naming collisions is to speed up your IDE and possibly compiles.

If you're using Visual Studio, using namespace std and/or using namespace boost can kill intellisense entirely. There are a lot of symbols in these namespaces that you may not realize and dumping them into the global namespace can be ludicrous.

Inverse