using namespace std; or using std::whatever

P

patrick

Why do some code listings for learning C++ have the entire namespace
std being used while others just specify the parts they want to use?
 
H

Howard

Why do some code listings for learning C++ have the entire namespace
std being used while others just specify the parts they want to use?

You'd really have to ask the person who wrote the code, if you want to know
why they wrote it that way. Personally, I never write "using namespace
std;", because it's overkill. Why do I need the netoire std namespace?
Often, I just need string, cin, cout, and endl. I sometimes write "using
std::cout;" or similar using statements. Other times I just specify the
std:: with whatever I'm using from it. It all depends on how readable it
makes the code, and how much it's really saving me to use a using statement
at all.

-Howard
 
M

mlimber

Why do some code listings for learning C++ have the entire namespace
std being used while others just specify the parts they want to use?

It's a matter of preference in most cases. The rule from _C++ Coding
Standards_, item 59 is (italics in original): "You can and should use
namespace using declarations and directives liberally /in your
implementation files after #include directives/ and feel good about it.
Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the
purposes of namespaces. Rather, they are what make namespaces usable."

Cheers! --M
 
H

Howard

Howard said:
You'd really have to ask the person who wrote the code, if you want to
know why they wrote it that way. Personally, I never write "using
namespace std;", because it's overkill. Why do I need the netoire std
namespace?

"netoire"??? Where the $#%#% did THAT come from? Dyslexic fingers, I
guess. (Sounds French somehow, don't it?) I meant "entire", as in "Why do
I need the ENTIRE std namespace?"

:-}

(By the way, I DO use entire namespaces sometimes, when the namespace isn't
as huge as std - especially if it's my own namespace.)

-Howard
 
P

patrick

mlimber said:
It's a matter of preference in most cases. The rule from _C++ Coding
Standards_, item 59 is (italics in original): "You can and should use
namespace using declarations and directives liberally /in your
implementation files after #include directives/ and feel good about it.
Despite repeated assertions to the contrary, namespace using
declarations and directives are not evil and they do not defeat the
purposes of namespaces. Rather, they are what make namespaces usable."

Cheers! --M

Interesting.

So I use using namespace std; I lose nothing compared to the chap who
careful typed using std::string; using std::cin, and so on? There is
no security or performance price to pay?
 
M

mlimber

Interesting.

So I use using namespace std; I lose nothing compared to the chap who
careful typed using std::string; using std::cin, and so on? There is
no security or performance price to pay?

Well, using namespace xxx will pull in *all* the symbols from that
namespace, which could lead to name collisions, but this is generally
evident at compile-time. For an example, see this post:

http://groups.google.com/group/comp.lang.c++/msg/3544f38ad8a18a20

Cheers! --M
 
R

Renato Golin

mlimber said:
Well, using namespace xxx will pull in *all* the symbols from that
namespace, which could lead to name collisions, but this is generally
evident at compile-time. For an example, see this post:

I generally use "using namespace std" myself partly because I prefer
using cout instead of std::cout and partly because I'm used to.

I never had collision problems with my own classes as I'm a bit careful
when choosing my class names and I think that's the point.

If you get collision between std and other namespaces and you do
have/want to "using std" and "using other" you should be specific only
on that case.

--renato
 
J

Jacek Dziedzic

Interesting.

So I use using namespace std; I lose nothing compared to the chap who
careful typed using std::string; using std::cin, and so on? There is
no security or performance price to pay?

You pay the price if you are "using namespace" within a
_header_ file (bad practice). This way you force everyone
who #includes your header file to pull in all the namespace
symbols. Therefore, as long as header files are concerned
you should take the effort to type in "std::string" etc
and not be "using namespace";
As far as .cpp files are concerned, the first line I type
after the #includes is usually "using namespace std;"

HTH,
- J.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top