using namespace std

using namespace std

Post by Halcyo » Thu, 10 Aug 2006 08:54:44


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
 
 
 

using namespace std

Post by Alan Johns » Thu, 10 Aug 2006 09:11:00


Please see section 27.5 of this group's FAQ:

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

--
Alan Johnson

 
 
 

using namespace std

Post by Gary Wessl » Thu, 10 Aug 2006 09:22:34

"Halcyon" < XXXX@XXXXX.COM > writes:


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://www.yqcomputer.com/
 
 
 

using namespace std

Post by Mark » Thu, 10 Aug 2006 09:32:25


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.
 
 
 

using namespace std

Post by Halcyo » Thu, 10 Aug 2006 09:34:08


Thanks for that. I missed it.
 
 
 

using namespace std

Post by Halcyo » Thu, 10 Aug 2006 09:41:23


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.


thanks for the lucid explanation.

cheers,
G
 
 
 

using namespace std

Post by Daniel T » Thu, 10 Aug 2006 11:48:13

In article < XXXX@XXXXX.COM >,



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.