Sign in to follow this  
Megaman_22

Quick C++ "using" ?

Recommended Posts

I haven't had any problem with it, but I was just curious if there was any effect from having redundant "using std::blah" lines in header files. For instance, I almost always have using statements for cout, cerr, and endl. Lets say that I have two headers, Foo.h and Bar.h which both declare these usings, and Foo includes Bar.h. Is there anything wrong or inefficient about that?

Share this post


Link to post
Share on other sites
It's fine to have redundant using statements

However, you should never put a 'using' statement in a header file at file scope. By doing this you affect code that uses your headers, and pollute their global namespace, which can cause major problems for a user of your library if you end up making a name ambiguous

Share this post


Link to post
Share on other sites
To add to what RDragon1 already said, a better idea is to import what you actually need with using declarations:


#include <iostream>

using std::cout;
using std::cerr;
using std::endl;



And to do so in a source file (.cpp) and not a header file. If you must put it in a header, at least don't put it in file scope as RDragon1 mentioned as it makes the using declaration "viral".

Share this post


Link to post
Share on other sites
I'd recommend not to use "using" for "std::cout", "std::string", "std::vector", etc... The namespaces are a good feature to prevent name clashing, and it's a good thing that the whole standard library was also put in a namespace.

When typing complex mathematical formulas with lots of "sin", "cos", "abs", etc... in some function then it can be more clear to have them without "std" everywhere, and then you can do "using" for these mathematical functions in the scope of your function (not the whole C++ file).

[Edited by - Lode on December 24, 2009 5:46:14 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Lode
I'd recommend not to use "using" for "std::cout", "std::string", "std::vector", etc... The namespaces are a good feature to prevent name clashing, and it's a good thing that the whole standard library was also put in a namespace.


I generally agree with you. I just felt like I should point out that importing what you actually use is far better than dumping everything in std into the global namespace.

In my opinion, there is another side to it though. While the risk of name clashing is a valid argument, anyone who experiences clashing between his names and those of the standard library is likely to have other problems (such as bad naming rules!). Very little 3rd party code actually uses the naming convention of the C++ standard library (Boost being the only exception I know of). As such, any clashes are likely to be results of a bad naming convention. Would you consider variable names such as "vector" and "string" to be particularly informative? In my opinion, giving such names to variables is simply a re-statement of their types (consider "std::string string") and should be avoided, not only because it is redundant, but also because it conveys no information whatsoever about the purpose of the variable.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this