Does "using namespace std" cost ?

Does "using namespace std" cost ?

Post by Tedd » Wed, 06 Jul 2005 10:01:58


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 ?
 
 
 

Does "using namespace std" cost ?

Post by benbe » Wed, 06 Jul 2005 10:16:41


"using namespace std" pulls everything from namespace std to the current
scope. If the current scope if small and managable enough (e.g. within a
small function) and you know you are not having name clashes, go for it. For
larger scope where more than one library is employed, using declarative is
preferred.

 
 
 

Does "using namespace std" cost ?

Post by David Whit » Wed, 06 Jul 2005 10:30:36


That's a matter of opinion and it depends on the circumstances. There
appears to be many people who avoid 'using namespace std;', and recommend to
others to avoid it, because it unnecessarily makes all names in the std
namespace visible at global scope, which can lead to inadvertent name
clashes. In practice, I suspect that this hardly ever happens, and if it
does you will almost certainly get a compiler error and easily fix the
problem. I've used it frequently for years and I've never had a problem,
even in large, complex files. I just don't see the point of creating a long
list of specific 'using's that you have to keep adding to every time you use
a new name when a single line takes care of it. The only thing I would
suggest is avoiding putting any 'using' at global scope in a header file,
because then individual source files that include the header lose control of
them.


In what sense 'cost'? The difference between them is the name lookup during
compilation.

DW
 
 
 

Does "using namespace std" cost ?

Post by Jim Langst » Wed, 06 Jul 2005 12:35:15


Some, including me, would say that using std::vector (or is that using
namespace std:vector?) is better than using namespace std. Personally, I
don't use the using clause at all and just put std:: in front of everything
from the std namespace.

As for the "cost", the cost only comes at compile time, when you compile it.
It doesnt' effect run time whatsoever. It's just for name resolution during
compile.
 
 
 

Does "using namespace std" cost ?

Post by Jaspree » Wed, 06 Jul 2005 14:51:29


It could cause name conflicts if you are using a same variable name in
your program that is available in the std namespace.

This should answer you:
http://www.yqcomputer.com/ ++-faq-lite/coding-standards.html#faq-27.5