using namespace std;

using namespace std;

Post by Randy Yate » Mon, 18 Oct 2004 01:03:47


I've seen this creep into c++ and have never heard of it. It apparently
has something to do with the standard C++ library header files (e.g., <string>,
<fstream>, etc.). Any illunination?
--
% Randy Yates % "My Shangri-la has gone away, fading like
%% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
%%% 919-577-9882 %
%%%% < XXXX@XXXXX.COM > % 'Shangri-La', *A New World Record*, ELO
http://www.yqcomputer.com/ ~yatescr
 
 
 

using namespace std;

Post by Artie Gol » Mon, 18 Oct 2004 01:46:37


Don't get out much, eh? ;-)

Starting from the standardization of C++ in 1998, entities defined in
the standard C++ library were placed in the standard namepsace (`std').

I would recommend taking a look at an appropriate C++ book (see:

http://www.yqcomputer.com/

for a list of suggestions).

HTH,
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."

 
 
 

using namespace std;

Post by Randy Yate » Mon, 18 Oct 2004 02:04:06

Artie Gold < XXXX@XXXXX.COM > writes:



If you consider C++ to be the center of the world, then no, I don't get
out much. I've been busy writing C and assembly code for digital signal
processors (the TI C54x and C55x) for a few years.


Thanks for the reference, Artie.
--
% Randy Yates % "How's life on earth?
%% Fuquay-Varina, NC % ... What is it worth?"
%%% 919-577-9882 % 'Mission (A World Record)',
%%%% < XXXX@XXXXX.COM > % *A New World Record*, ELO
http://www.yqcomputer.com/ ~yatescr
 
 
 

using namespace std;

Post by Iacop » Mon, 18 Oct 2004 02:42:43


The C++ language uses the namespace to declare scopes. For example: if you
use your own set of functions that draws circles and one of them is called
drawCircle(int x, int y, int range) and you also use a third-part library
with another function with the same name and same parameters, which one is
called ? there is a kind of collision! (don't mix up with overload, it's a
redeclaration of the same function with same name but different parameters,
we are talking about function with same names and same parameters).
To avoid this problem, everyone should create his own namespace so you have
to specify the namespace before using a function (bye bye collisions)!

using namespace std; means that you don't need to specify the namespace to
use functions declared inside it (std in this case)! That's why you are
sure you will not make collisions.

example:

// Without using namespace std;

#include <stdio.h>
#include <iostream>

int main () {
std::cout << "This is a console output\n";
return 0;
}

in the other hand:

// With using namespace std;

#include <stdio.h>
#include <iostream>

using namespace std;

int main () {
cout << "This is a console output\n";// You dont need std:: because the
// compile will search cout into
//the namespace
return 0;
}

As you can see, it's an easy and basic idea.

--
--------------------------------------------------------------
Iacopo < XXXX@XXXXX.COM >
GPG Public Key: http://www.yqcomputer.com/
Fingerprint: B0A8 BC97 C53E 172D 0C77 80B3 C30E 835F 5C31 65AE
--------------------------------------------------------------
 
 
 

using namespace std;

Post by Artie Gol » Mon, 18 Oct 2004 05:06:43


No insult was intended -- hence the smiley.
--ag

--
Artie Gold -- Austin, Texas

"If you don't think it matters, you're not paying attention."
 
 
 

using namespace std;

Post by Randy Yate » Mon, 18 Oct 2004 08:43:42

Artie Gold < XXXX@XXXXX.COM > writes:




Smiling while you insult someone doesn't mean it is no longer an
insult, in my book.

:)
--
% Randy Yates % "She's sweet on Wagner-I think she'd die for Beethoven.
%% Fuquay-Varina, NC % She love the way Puccini lays down a tune, and
%%% 919-577-9882 % Verdi's always creepin' from her room."
%%%% < XXXX@XXXXX.COM > % "Rockaria", *A New World Record*, ELO
http://www.yqcomputer.com/ ~yatescr
 
 
 

using namespace std;

Post by pedicin » Mon, 18 Oct 2004 10:02:12


apparently
<string>,
for Beethoven.
tune, and

Kind of high-strung, aren't you? <insert smiley here>
 
 
 

using namespace std;

Post by E. Robert » Mon, 18 Oct 2004 11:44:52


That's correct.
Namespaces were introduced when C++ was standardized (1998).
It took some time for compiler developers to comply with the standard
but all of them pretty much complied a few years ago.
 
 
 

using namespace std;

Post by Gianni Mar » Tue, 19 Oct 2004 04:46:42


...

That would do it ! :-0
 
 
 

using namespace std;

Post by Rolf Magnu » Sun, 24 Oct 2004 19:03:00


Actually, "using namespace ..." destroys the advantage of avoiding
colisions, because it will copy everything from that namespace into the
global namespace. The actual reson to put it into an own namespace was
exactly to _not_ have it in the global one.


Why did you include that one?


I'd recommend that only for small programs though.
 
 
 

using namespace std;

Post by zahy[dot]b » Fri, 02 Jun 2006 18:49:49

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.
 
 
 

using namespace std;

Post by Erik Wikst » Fri, 02 Jun 2006 18:58:25


Pros: You know what you get.
Cons: A bit more to type.


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.yqcomputer.com/ ++-faq-lite/coding-standards.html#faq-27.5

Erik Wikstr
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
 
 
 

using namespace std;

Post by Dakk » Fri, 02 Jun 2006 19:21:31


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.
 
 
 

using namespace std;

Post by Markus Sch » Fri, 02 Jun 2006 19:42:00


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.
 
 
 

using namespace std;

Post by Michiel.Sa » Fri, 02 Jun 2006 19:52:12


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