By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

using namespace std; or using std::whatever

P: n/a
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?

Oct 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a

<pa*****@kirks.netwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
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

Oct 2 '06 #2

P: n/a
pa*****@kirks.net wrote:
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

Oct 2 '06 #3

P: n/a

"Howard" <al*****@hotmail.comwrote in message
news:6T*********************@bgtnsc05-news.ops.worldnet.att.net...
>
<pa*****@kirks.netwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
>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?
"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
Oct 2 '06 #4

P: n/a

mlimber wrote:
>
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?

Oct 2 '06 #5

P: n/a
pa*****@kirks.net wrote:
mlimber wrote:

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

Cheers! --M

Oct 2 '06 #6

P: n/a
mlimber wrote:
>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:
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

--
Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm
Oct 3 '06 #7

P: n/a
pa*****@kirks.net wrote:
mlimber wrote:
>>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?
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.
Oct 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.