Does "using namespace std" cost ?

This topic is 4710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by ZahlmanI 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 on other sites
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 on other sites
Quote:
 Original post by ZahlmanI 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 on other sites
Quote:
 Original post by DigitalDelusionAlso 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;

• 10
• 17
• 9
• 14
• 41
• Forum Statistics

• Total Topics
631073
• Total Posts
2997754
×