using namespace std

H

Halcyon

so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G
 
A

Alan Johnson

Halcyon said:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G

Please see section 27.5 of this group's FAQ:

http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
 
G

Gary Wessle

Halcyon said:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G

take a look at vol.1 pages
91
414
757
and others from the inedx, it is a e-book for free, I bought the hard
copy and it is very good.
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
 
M

Mark P

Halcyon said:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G

First of all, I'd be leery of anyone who tells you never to do something
and can't explain why. Second, "using namespace std" is OK provided the
scope of such a directive is limited. The general rule of thumb that
follows from this is that it's OK in a .cpp file but should not be used
in a .h file.

What's the difference you ask? A .h file may be #included all over the
place and anyone who #includes your .h file would, perhaps unwittingly,
also include the using directive.

And what's so bad about that, you ask? Well you may be content to have
cout mean std::cout and vector mean std::vector, but others may not feel
the same way. If someone else has a piece of code where they've defined
their own vector class-- perhaps a linear algebra package-- and they
include your header file (including using namespace std; and #include
<vector>) the compiler will complain about an ambiguous name.
 
H

Halcyon

Mark said:
First of all, I'd be leery of anyone who tells you never to do something
and can't explain why.
Second, "using namespace std" is OK provided the
scope of such a directive is limited. The general rule of thumb that
follows from this is that it's OK in a .cpp file but should not be used
in a .h file.

right, that's what i gleaned. But considering that fact that i wrote
the .cpp file, and i know what i'm doing, it should definitely be safe
in my .cpp file.

however, on reading the various arguements, i think i'm going to play
it safe and stick to using std::cout/std::string/std::vector.
What's the difference you ask? A .h file may be #included all over the
place and anyone who #includes your .h file would, perhaps unwittingly,
also include the using directive.

And what's so bad about that, you ask? Well you may be content to have
cout mean std::cout and vector mean std::vector, but others may not feel
the same way. If someone else has a piece of code where they've defined
their own vector class-- perhaps a linear algebra package-- and they
include your header file (including using namespace std; and #include
<vector>) the compiler will complain about an ambiguous name.

thanks for the lucid explanation.

cheers,
G
 
D

Daniel T.

"Halcyon said:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

The book "C++ Coding Standards" by Sutter and Alexandrescu specifically
OK what you are doing.

There really is no reason not to put "using namespace std" in your cpp
file after all your includes, unless you aren't using anything from the
standard namespace of course. You will notice that the reason everyone
(who says not to do it) says not to do it, is that your code may not
compile. If it does compile, what's the problem? If it doesn't, because
of some ambiguity, just qualify the ambiguous names and it will.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top