473,511 Members | 10,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using namespace std

so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G

Aug 8 '06 #1
6 9000

Halcyon wrote:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G
Please see section 27.5 of this group's FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-27.5

--
Alan Johnson

Aug 9 '06 #2
"Halcyon" <ga**************@gmail.comwrites:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G
take a look at vol.1 pages
91
414
757
and others from the inedx, it is a e-book for free, I bought the hard
copy and it is very good.
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
Aug 9 '06 #3

Alan Johnson wrote:
Please see section 27.5 of this group's FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-27.5
Thanks for that. I missed it.
--
Alan Johnson
Aug 9 '06 #4
Halcyon wrote:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?

cheers,
G
First of all, I'd be leery of anyone who tells you never to do something
and can't explain why. Second, "using namespace std" is OK provided the
scope of such a directive is limited. The general rule of thumb that
follows from this is that it's OK in a .cpp file but should not be used
in a .h file.

What's the difference you ask? A .h file may be #included all over the
place and anyone who #includes your .h file would, perhaps unwittingly,
also include the using directive.

And what's so bad about that, you ask? Well you may be content to have
cout mean std::cout and vector mean std::vector, but others may not feel
the same way. If someone else has a piece of code where they've defined
their own vector class-- perhaps a linear algebra package-- and they
include your header file (including using namespace std; and #include
<vector>) the compiler will complain about an ambiguous name.
Aug 9 '06 #5

Mark P wrote:
>
First of all, I'd be leery of anyone who tells you never to do something
and can't explain why.
Second, "using namespace std" is OK provided the
scope of such a directive is limited. The general rule of thumb that
follows from this is that it's OK in a .cpp file but should not be used
in a .h file.
right, that's what i gleaned. But considering that fact that i wrote
the .cpp file, and i know what i'm doing, it should definitely be safe
in my .cpp file.

however, on reading the various arguements, i think i'm going to play
it safe and stick to using std::cout/std::string/std::vector.
>
What's the difference you ask? A .h file may be #included all over the
place and anyone who #includes your .h file would, perhaps unwittingly,
also include the using directive.

And what's so bad about that, you ask? Well you may be content to have
cout mean std::cout and vector mean std::vector, but others may not feel
the same way. If someone else has a piece of code where they've defined
their own vector class-- perhaps a linear algebra package-- and they
include your header file (including using namespace std; and #include
<vector>) the compiler will complain about an ambiguous name.
thanks for the lucid explanation.

cheers,
G

Aug 9 '06 #6
In article <11**********************@75g2000cwc.googlegroups. com>,
"Halcyon" <ga**************@gmail.comwrote:
so i've been "using namespace std" happily in all my source files at
the global scope, and i then go to to use cout, vector, string etc
without having to use std:: everytime. However today i was informed
that i should eschew "using namespace std" and use "using std::string",
"std::vector" et al.

I don't quite get what the potential drawbacks of "using namespace std"
are...
i perused a few books, but most authors *do* use "using namespace std",
and the person who asked me to make the change wouldn't explain.

I was under the impression that the using keyword just lets the
compiler know where to look...
i mean, would it make a difference in code size or anything?
The book "C++ Coding Standards" by Sutter and Alexandrescu specifically
OK what you are doing.

There really is no reason not to put "using namespace std" in your cpp
file after all your includes, unless you aren't using anything from the
standard namespace of course. You will notice that the reason everyone
(who says not to do it) says not to do it, is that your code may not
compile. If it does compile, what's the problem? If it doesn't, because
of some ambiguity, just qualify the ambiguous names and it will.
Aug 9 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1929
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
8
4894
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using...
3
1902
by: Pranav Shah | last post by:
What is the differrence between using the "using" caluse outside of the namespace definition and inside the namespace. Example Outside: using System; namespace Example.Outside { }
0
1862
by: Simon | last post by:
I need call a LoginUser API from MC++ dll, but when I try to call the I have always the same exception: "System.NullReferenceException: Object reference not set to an instance of an object. at...
3
1461
by: Brian Gideon | last post by:
I stumbled across something odd today about the placement of the using keyword. Section 9.3.2 of the C# v1.1 specification did not answer my question. My confusion is isolated to what happens in...
8
2395
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
12
2811
by: Keith Patrick | last post by:
Can someone tell me the difference in terms of actual implications using: namespace MyNamespace { using System; class MyClass {...} } vs. using System;
30
4065
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
3
1905
by: Wayne Shu | last post by:
When I read the chapter of the namespace of the book C++ Primer(3e). It explain the using directive as follow: "A using directive makes the namespace member names visible as if they were declared...
2
4371
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
0
7148
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
7367
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
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
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...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.