Sign in to follow this  
neverland

Does "using namespace std" cost ?

Recommended Posts

neverland    145
If I use just a STL container such as std::vector in my program. Is "using std::vector;" better than "using namespace std" ? Does "using namespace std" cost ?

Share this post


Link to post
Share on other sites
Endar    668
I don't know for sure, but I really do doubt it.

"using namespace std;" tells the compiler that if it can't find the definition of an object within your code, or since not everyone uses namespaces, the global namespace, that it should look in these other namespaces that you have declared.

If you just have "using std::vector;", then, obviously, for everything else in the STL, you'll have to put the declaration in, unless you're using only the one. And really, even with small things, how many times do you use only one of the STL objects?

Share this post


Link to post
Share on other sites
Max_Payne    757
It makes zero difference, performance-wise. Its a name resolution detail, and changes nothing to the compiled result.

However, its considered cleaner to write out std::vector. The "using" keyword is there for code migration purposes (ie: to get your legacy code to compile without much modifications).

Share this post


Link to post
Share on other sites
silverphyre673    454
No performance cost, but it WILL put all the names of every function and global structure in namespace std into yours. So if you were going to, say, use your own "string" class, you would have to either put it in a separate namespace, or rename it. Here is what (IMHO) you should do:

If you are building a tool of some sort that will be in multiple different projects or used by another person, use this:

#include <vector>

void SomeToolFunction()
{
std::vector<int> some_ints;
}

If you are writing project-specific code, you can do this:

#include <vector>
using std::vector;

int main()
{
std::vector<GIRLS> my_harem;
return 0;
}

Share this post


Link to post
Share on other sites
ricekrispyw    388
I don't think it's too bad, if you're using it quite a bit to say
using std::string;
or something. But in general, write out the whole thing. It improves readability (which most people's code severely lacks).

Share this post


Link to post
Share on other sites
Zahlman    1682
I disagree strongly - scattering "std::" all over the code is extra line noise in most cases. My policy is:

- don't "using" anything in header files, because it will potentially make life difficult for anyone who includes it.
- At the beginning of the source file, go ahead and put "using namespace std;" if you're going to be using any of that stuff. IF you later find that you have a name collision between something of yours and the std namespace, you can change this to "using std::x" for each appropriate x. IF you then find out that you need both the std::foo and your own foo, you can qualify each instance of foo appropriately at that point (while continuing with "using std::bar" etc. where there aren't any conflicts).

This is, I argue, in accordance with the core principles of OAOO and RefactorMercilessly.

Share this post


Link to post
Share on other sites
nmi    978
Quote:
Original post by Zahlman
I disagree strongly - scattering "std::" all over the code is extra line noise in most cases. My policy is:

- don't "using" anything in header files, because it will potentially make life difficult for anyone who includes it.

I agree, but in case of a template class, you need to put the implementation in the header file (i.e. the file that includes gets the full source of that class). The best solution would be to put 'using' inside the class scope, not polluting the global scope.

For inline functions defined in header files, you can put 'using' inside the function.

Share this post


Link to post
Share on other sites
paulecoyote    1065
I think the only cost is compilation time - using namespace std; puts the contents of what is contained in the std namespace from the header files you have included in to the global namespace.

The more names of things the compiler has to look through in order to resolve what it is you are actually refering too, the longer that process takes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
I disagree strongly - scattering "std::" all over the code is extra line noise in most cases. My policy is:

- don't "using" anything in header files, because it will potentially make life difficult for anyone who includes it.
- At the beginning of the source file, go ahead and put "using namespace std;" if you're going to be using any of that stuff. IF you later find that you have a name collision between something of yours and the std namespace, you can change this to "using std::x" for each appropriate x. IF you then find out that you need both the std::foo and your own foo, you can qualify each instance of foo appropriately at that point (while continuing with "using std::bar" etc. where there aren't any conflicts).

This is, I argue, in accordance with the core principles of OAOO and RefactorMercilessly.


Also it gives the added benefit of being able to transparently switcht implementions with only one localized change. For example if you're using std::string in a bazillion places and decide to switch to MuchBetter::string you have quite a job todo if you've put using declarations at the top of the file a one line change will work all the magic out for you.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by DigitalDelusion
Also it gives the added benefit of being able to transparently switcht implementions with only one localized change.



#include <vector>
#include <string>
#include <ftl/string.hpp>

namespace foo_v1
{
using std::vector;
using ftl::string;
}

namespace foo_v2
{
using std::vector;
using std::string;
}

namespace foo = foo_v1;

...

foo::vector v;
foo::string s;

Share this post


Link to post
Share on other sites
smr    2468
Quote:
Original post by Zahlman
I disagree strongly - scattering "std::" all over the code is extra line noise in most cases.


Most definitely. Plus the scope resolution operator "::" is bitch to type.

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