473,413 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

using namespace std; or using std::whatever

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
7 2199

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

"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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: C. Alexander | last post by:
I'm making a 'whiteboard' application. I'm trying to have 2+ connected users able to draw at the same time. However, if User1 draws a line, when User2 is drawing, on User1 screen, it will draw...
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
12
by: Calum Grant | last post by:
In older C++ computer books, you'll often see using namespace std; even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered better to qualify names to make it clearer what...
2
by: Bill Nguyen | last post by:
I would like to add a new VB.NET project using the same folder being used by another project so that I can share several forms already creaded by the other project. However, .NET created a new...
133
by: Alan Silver | last post by:
Hello, Just wondered what range of browsers, versions and OSs people are using to test pages. Also, since I don't have access to a Mac, will I have problems not being able to test on any Mac...
6
by: Bob Palank | last post by:
I'm considering using VJ# in a first programming course in addition to or in place of JBuilder and the J2SE. Given install problems other students have had, VJ# seems like a nice alternative. I...
10
by: The Cool Giraffe | last post by:
I got a hint recently (guess where, hehe) and was directly pointed to the vector class. Now, i have no issues using that way but i'm concerned about the performance issue. Which is fastest...
5
by: jgraveskc | last post by:
I know this sounds nasty, but let me explain. I have what was a class that contains a whole buncha function pointers (for OpenGL extensions) that are used in the app. I was porting this app to...
5
by: cappieins | last post by:
Hi, i've got a problem using PHP FPDF library to create PDF files send inline to a browser window. The created PDF file opens correctly in the browser (IE 6), but if I want to save it to my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.