By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

using namespace std

P: n/a
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

Aug 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Halcyon wrote:
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-lit....html#faq-27.5

--
Alan Johnson

Aug 9 '06 #2

P: n/a
"Halcyon" <ga**************@gmail.comwrites:
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
Aug 9 '06 #3

P: n/a

Alan Johnson wrote:
Please see section 27.5 of this group's FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-27.5
Thanks for that. I missed it.
--
Alan Johnson
Aug 9 '06 #4

P: n/a
Halcyon wrote:
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.
Aug 9 '06 #5

P: n/a

Mark P wrote:
>
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

Aug 9 '06 #6

P: n/a
In article <11**********************@75g2000cwc.googlegroups. com>,
"Halcyon" <ga**************@gmail.comwrote:
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.
Aug 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.