Does "using namespace std" cost ?

T

Teddy

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 ?
 
B

benben

Teddy said:
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 ?

"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.
 
D

David White

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

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.
Does "using namespace std" cost ?

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

DW
 
J

Jim Langston

Teddy said:
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 ?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top