What are the evils of "using namespace std;"

Started by
8 comments, last by Jampol 12 years, 3 months ago

I wish to avoid falling into bad habits so I'm looking to find out the issues with [font="helvetica, arial, verdana, tahoma, sans-serif"][size="2"][color="#282828"]inputting "[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

using namespace std;" into my code. Thanks for any input on the matter.[/font]

Advertisement
avoid putting using declarations into header files, as that will clutter the global namespace with symbols and increase the risk of naming conflicts wherever these headers are included.
in source files you can use them. However it is often better to import only selected entities. e.g.


using std::vector;
using std::list;
it defeats the point of having namespaces in the first place, and makes your code harder to read, personally i think it makes more sense to std::string than just "string" as all that tells me is a object called string is being used, it doesn't tell me where abouts it has come from
http://stowelly.co.uk/
What was said above. Always use std::string etc in header files and it's fine to use using namespace std; in source files unless the source files are shared by many developers or somebody will sooner or later start getting strange type errors.

I hope it is not too far out of the scope of this question, but my own acquired personal preference for all std:: template types is even to use something like typedef std::vector<int> PersonalItems. This allows for easy swapping of iterated data structures later and using namespace std won't be needed. For example:

typedef std::vector< int > Items;

and this

typedef std::list< int > Items;

can both be used like this:

Items items;
for(Items::const_iterator i=items.begin(); i!=items.end(); i++) {
// do something with *i
}

without any other changes to code. Best if used for things like PersonalItems, Bunnies or Cars. Good IDE is also needed or it will become difficult to track what does Bunnies mean. Creating some global typedefs like MyIntegers for std would defeat the purpose.
Yes the thing with the typedef is definitely true. I personally only use the using directive for quick test scenarios. Also, keep in mind that you can place 'using' into function bodies, you don't have to do it in global space.

I hope it is not too far out of the scope of this question, but my own acquired personal preference for all std:: template types is even to use something like typedef std::vector<int> PersonalItems. This allows for easy swapping of iterated data structures later and using namespace std won't be needed. For example:


Nope not out of the scope of the question, any input is great, examples are even better.

Thanks for the input so far everyone!
Another example: Guru of the Week #30
[TheUnbeliever]
They should never be used in a header because there is no way to "un-use" a namespace.

In addition, SiCrane has often mentioned a specific problem where the order of inclusion, combined with a using declaration, caused the meaning of the code to change. Unfortunately I cannot find a reference for this at the moment.
Yeah, I can't find any of those posts myself right now. IIRC, it goes something like this: a lot of standard library functions are templates with rather generic names. If you're writing a 3D game then you may have functions called <tt>transform()</tt> or <tt>rotate()</tt>, which are also names of standard library algorithms. Let's say you have a hypothetical function <tt>void rotate(D3DVECTOR3 * a, D3DVECTOR3 * b, D3DVECTOR3 * c)</tt>. This uses the standard D3D 3D vector type. However, frequently instead of using <tt>D3DVECTOR3</tt> types you might use <tt>D3DXVECTOR3</tt> types because in C++ those provide operator overloads so you can use + to add vectors, etc. Since <tt>D3DXVECTOR3</tt> derives from <tt>D3DVECTOR</tt> this usually isn't an issue when operating with code that expects <tt>D3DVECTOR</tt> pointers. So this is legal code:

D3DXVECTOR3 a, b, c;
// initialize them somehow
rotate(a, b, c);

However, if you directly or indirectly include the algorithm header and stick in a <tt>using namespace std</tt>, the <tt>rotate()</tt> call will no longer bind to your <tt>rotate()</tt> function, but to <tt>std::rotate()</tt> because there isn't an exact match with your function.
Just wanted everyone to know I had a good look at everyone's responses and once again appreciate the help!

This topic is closed to new replies.

Advertisement