Re: using namespace std;
"Dakka" <"news at electro dot mine dot nu"> wrote in message
news:447f93af$0$22205$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
Cy Edmunds wrote:
"Dakka" <"news at electro dot mine dot nu"> wrote in message
news:447ebf98$0$7859$5a62ac22@per-qv1-newsreader-01.iinet.net.au...
zahy[dot]bnaya[At]gmail[dot]com wrote:
Hi all,
Since I am always confusing this, I want to know once and for all what
is the right way of doing this.
I've noticed that some programs use:
std::cout<< "yadayada"<<endl;
[which I'll call it alternative 1]
while some others use:
cout<< "yadayada"<<endl;
usually they have:
using namespace std;
somewhere.
[which I'll call it alternative 2]
now, I can understand that both has the same effect since the "using
namespace" is somehow equivelant to std:: before the cout.
I am more of an alternative 2 person but every now and then it does not
work untill I explicitly type the std:: before the statement.
what are the pros and cons of using each of the alternatives?
what should I use? Thanks.
General rule: never put this statement in a header file;
That I certainly agree with.
Advice: use it where ever else you wish - your compiler will tell you
when you have namespace resolution conflicts. If you get weird
inexplicable errors, comment it out, put std:: in try again.
--
--dakka
[snip]
I think your Advice is poor. Let's say you compile and test your code and
find no namespace conflicts. So far, so good. Now something happens:
1) the code is ported to a new platform
2) the code is recompiled with a different compiler (which may comprehend
a modified standard)
3) the code needs to be recompiled because of a change in a non-standard
header
4) the code needs to be recompiled because it has been changed
In any of these cases name conflicts might suddenly appear. And whoever
gets the error messages isn't going to thank you for trying to save
yourself a few keystrokes.
Maintainability is key in software development. Keystrokes are nothing.
Think of the last major project you worked on. What fraction of the time
was taken by the actual typing?
Cy
Doubtful. Remember that std namespace uses well known members and
identifiers that for the most part seamlessly port. As I said, when it
doesn't compilers let you know. If the using statement resides in a
compilation unit it is easy to remove it as I said. Your comments are more
relevant with custom namespaces.
I remember a while back someone posted in this newsgroup a problem he had
compiling a rather simple program. It turns out he was using namespace std;
and the class he was trying to create had the same name of something in the
std he was including. Compilers aren't usually very good at telling you
exactly what's wrong, only what they think is wrong. Sometimes they compile
anyway using hiding and you get weird results.
Also, "std namespace uses well known members" in a perfect world. This is
not a perfect world, especially with Windows compilers. Windows headers are
well known for adding stuff to the stl in std that doesn't belong.
This is one reason I'm one of those that never use use namespace std;, I'll
always just use the std:: everywhere, it's not that big of a deal. And yes,
this is in big projects. I've been programming many years and I'm one of
those that am very glad that C++ has namespaces, as I've run into name
collision quite a bit over the years and it can be frustrating. Seeing as
now we have it, I'm not going to short circuit it by bringing everything
into the unnamed namespace anyway.