using namespace std;

  • Thread starter zahy[dot]bnaya[At]gmail[dot]com
  • Start date
Z

zahy[dot]bnaya[At]gmail[dot]com

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

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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]

Pros: You know what you get.
Cons: A bit more to type.
while some others use:

cout<< "yadayada"<<endl;

usually they have:

using namespace std;
somewhere.
[which I'll call it alternative 2]

Pros: Little less to type.
Cons: Might introduce weird problems. Not recommended.

The problem with "using namespace std;" is that you drag everything from
std into your namespace which might cause problems when there is a
function in std:: that has the same name as one of your functions. I
usually use the third method, "using std::cout;" which lets you use cout
without typing std:: in front of it every time and avoids the problems
with "using namespace std;".

For a more throughout explanation see the faq:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

Erik Wikström
 
D

Dakka

zahy[dot]bnaya[At]gmail[dot]com said:
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;
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

Dykstra's Observation:
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
 
M

Markus Schoder

Erik said:
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]

Pros: You know what you get.
Cons: A bit more to type.
while some others use:

cout<< "yadayada"<<endl;

usually they have:

using namespace std;
somewhere.
[which I'll call it alternative 2]

Pros: Little less to type.
Cons: Might introduce weird problems. Not recommended.

The problem with "using namespace std;" is that you drag everything from
std into your namespace which might cause problems when there is a
function in std:: that has the same name as one of your functions. I
usually use the third method, "using std::cout;" which lets you use cout
without typing std:: in front of it every time and avoids the problems
with "using namespace std;".

I see people repeating the argument about dragging everything from
namespace std into the global namespace with "using namespace std". In
reality only what you actually include (directly or indirectly) from
the standard library becomes visible. This makes a significant
difference I would say even so the issue in general still remains.
 
M

Michiel.Salters

Markus said:
I see people repeating the argument about dragging everything from
namespace std into the global namespace with "using namespace std". In
reality only what you actually include (directly or indirectly) from
the standard library becomes visible.

Yes. But since every standard header potentially includes every other
header,
this means you have to assume all names can be dragged in. This is a
difference
with C.

HTH,
Michiel Salters
 
C

Cy Edmunds

Dakka said:
zahy[dot]bnaya[At]gmail[dot]com said:
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.
[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
 
D

Dakka

Cy said:
Dakka said:
zahy[dot]bnaya[At]gmail[dot]com said:
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.
[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.

--
--dakka

Dykstra's Observation:
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
 
J

Jim Langston

Dakka said:
Cy said:
Dakka said:
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.
[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.
 

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