473,695 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

should "using namespace std" be used?

Pep
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" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify that
the std namespace is being used instead of tagging each member of the
namespace?

Jun 21 '06 #1
30 4106
"Pep" <pe*@nowhere.co m> wrote in message
news:e7******** **@pop-news.nl.colt.ne t...
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" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?


For non trivial code, using std namespace; may be okay, but I never use it
then either.

The problem with using std namespace; is that it brings everything into the
unnamed namespace. Usually this doesn't cause problems, until you try to
declare a function or variable or class with the same name as something else
in the std namespace.

If you don't really like doing std::cout std::endl etc... just bring what
you need into the unnamed namespace.

using std::cout;
using std::endl;

now you can use
cout << "blah blah" << endl;
without bringing in everything else.
Jun 21 '06 #2
Pep
Jim Langston wrote:
"Pep" <pe*@nowhere.co m> wrote in message
news:e7******** **@pop-news.nl.colt.ne t...
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" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?
For non trivial code, using std namespace; may be okay, but I never use it
then either.

The problem with using std namespace; is that it brings everything into
the
unnamed namespace. Usually this doesn't cause problems, until you try to
declare a function or variable or class with the same name as something
else in the std namespace.


This is where I dither over the choice. Given that all c++ programmers are
aware of the std namespace and expects it to provide the standard c/c++
enities, shouldn't we place our overrides in a application specific
namespace and then qualify the use of the routines with the namespace tag?

i.e.

namespace foo
{
int atol(const char* val)
{
return(std::ato l(val) * 100);
}
}

cout << foo::atol("12") << endl;

This is very clearly calling atol from a different namespace than std and as
a new developer on the project I would immediately be suspect of the
routine and would want to check out it's functionality.
If you don't really like doing std::cout std::endl etc... just bring what
you need into the unnamed namespace.

using std::cout;
using std::endl;

now you can use
cout << "blah blah" << endl;
without bringing in everything else.


Yet, as a new developer on a project that has been badly documented and laid
out over several hundred source files, I might miss the fact that cout and
endl were brought in like this. As such the mixed used of the imported
cout, imported endl and let's say a locally declared atol might get
confusing as you would naturally assume that the std namespace has been
employed and therefore are using std::atol instead of foo::setw.

Jun 21 '06 #3

"Pep" <pe*@nowhere.co m> wrote in message
news:e7******** **@pop-news.nl.colt.ne t...
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" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?

This is covered in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-27.5

Regards,
Sumit.
--
Sumit Rajan <su*********@gm ail.com>
Jun 21 '06 #4
Pep
Sumit Rajan wrote:

"Pep" <pe*@nowhere.co m> wrote in message
news:e7******** **@pop-news.nl.colt.ne t...
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" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?

This is covered in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-27.5

Regards,
Sumit.
--
Sumit Rajan <su*********@gm ail.com>


Whilst I agree with the FAQ in a new project, it does not really address the
scenario I placed in my reply to Jim Langston. Yes it is possible to
decide at the begginning of a project but badly documented projects which I
have worked on do not make the distinction very clearly and when the long
time serving inmates of the project leave, they take that sort of knowledge
with them. In the past I have wasted 3 days on a simple bug simply because
it was not documented that some of the std entities had been replaced with
locally defined ones.

Then again maybe I should simply stick to contracts that involve properly
documented designs, yeah right :)

Jun 21 '06 #5
In article <e7**********@p op-news.nl.colt.ne t>, Pep <pe*@nowhere.co m>
wrote:
Sumit Rajan wrote:

"Pep" <pe*@nowhere.co m> wrote in message
news:e7******** **@pop-news.nl.colt.ne t...
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" << std::endl;

Myself I am not sure which I prefer, it is certainly easier to specify
that
the std namespace is being used instead of tagging each member of the
namespace?

This is covered in the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-27.5


Whilst I agree with the FAQ in a new project, it does not really address the
scenario I placed in my reply to Jim Langston. Yes it is possible to
decide at the begginning of a project but badly documented projects which I
have worked on do not make the distinction very clearly and when the long
time serving inmates of the project leave, they take that sort of knowledge
with them. In the past I have wasted 3 days on a simple bug simply because
it was not documented that some of the std entities had been replaced with
locally defined ones.

Then again maybe I should simply stick to contracts that involve properly
documented designs, yeah right :)


I think the FAQ is completely wrong on this point. "using namespace std"
is fine to use in source files (after all includes,) while no using
directive or declaration should ever be used in a header file.

The supposed "problem" of "using namespace std" allowing the compiler
see all the std names isn't a problem at all, and refusing to have using
declarations and directives in your code defeats the purpose of the
namespace mechanism.

See "C++ Coding Standards" by Sutter & Alexandrescu for a more
reasonable rule regarding the "using" keyword.
Jun 21 '06 #6
Daniel T. <da******@earth link.net> wrote:
I think the FAQ is completely wrong on this point. "using namespace std"
is fine to use in source files (after all includes,) while no using
directive or declaration should ever be used in a header file.


Yes, I think a generalization of this is that "using directives" are
fine when you can control the scope of the directive. Your situation
above is now a specific case of this, though your wording may be easier
to understand for people who don't know the language extensively yet,
since it is more concrete.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 21 '06 #7
Pep
Marcus Kwok wrote:
Daniel T. <da******@earth link.net> wrote:
I think the FAQ is completely wrong on this point. "using namespace std"
is fine to use in source files (after all includes,) while no using
directive or declaration should ever be used in a header file.


Yes, I think a generalization of this is that "using directives" are
fine when you can control the scope of the directive. Your situation
above is now a specific case of this, though your wording may be easier
to understand for people who don't know the language extensively yet,
since it is more concrete.


Hmm, I tend to agree with this view. Adding "using namespace std" in source
files is fine as it is local to the coding point whereas adding to include
files can affect the project in ways not intended.

Then again it would be nice if just once when I join a project I find well
documented code with good design docs, just as of course I always do when
starting a new project ;)
Jun 21 '06 #8
Pep <pe*@nowhere.co m> wrote:
Marcus Kwok wrote:
Daniel T. <da******@earth link.net> wrote:
I think the FAQ is completely wrong on this point. "using namespace std"
is fine to use in source files (after all includes,) while no using
directive or declaration should ever be used in a header file.


Yes, I think a generalization of this is that "using directives" are
fine when you can control the scope of the directive. Your situation
above is now a specific case of this, though your wording may be easier
to understand for people who don't know the language extensively yet,
since it is more concrete.


Hmm, I tend to agree with this view. Adding "using namespace std" in source
files is fine as it is local to the coding point whereas adding to include
files can affect the project in ways not intended.


Here is a good article on namespace usage (especially on adding
namespaces to existing code):

http://www.gotw.ca/publications/migr...namespaces.htm

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 21 '06 #9
In message <e7**********@n ews-int.gatech.edu> , Marcus Kwok
<ri******@gehen nom.invalid> writes
Pep <pe*@nowhere.co m> wrote:
Marcus Kwok wrote:
Daniel T. <da******@earth link.net> wrote:
I think the FAQ is completely wrong on this point. "using namespace std"
is fine to use in source files (after all includes,) while no using
directive or declaration should ever be used in a header file.

Yes, I think a generalization of this is that "using directives" are
fine when you can control the scope of the directive. Your situation
above is now a specific case of this, though your wording may be easier
to understand for people who don't know the language extensively yet,
since it is more concrete.


Hmm, I tend to agree with this view. Adding "using namespace std" in source
files is fine as it is local to the coding point whereas adding to include
files can affect the project in ways not intended.


Here is a good article on namespace usage (especially on adding
namespaces to existing code):

http://www.gotw.ca/publications/migr...namespaces.htm

where you'll find this gem:

"I find it helpful to think of a using directive as a marauding army of
crazed barbarians that sows indiscriminate destruction wherever it
passes"

--
Richard Herring
Jun 21 '06 #10

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

Similar topics

5
1949
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;
17
3504
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example using std::cin; using std::cout;
8
4371
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could somebody explain it to me? I am using MSVC 6.0. Thanks a lot,
11
3238
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator? Then, I found, in this book, the semantic of set::iterator is defined as same as set::const_iterator. Both of them must be const! I tried to read the source of GNU STL(version 3.4.1).They were using a red-black tree to implant it (std::set has a...
4
2706
by: Teddy | last post by:
If I use just a STL container such as std::vector in my program. Is "using std::vector;" better than "using namespace std" ? Does "using namespace std" cost ?
13
141240
by: Squid Seven | last post by:
This is just bizarre. for the following snippet of code: #include <string> using std::string; I get the error message:
25
2574
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For example: with quitCommandButton .enabled = true .default = true end with
0
8623
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9113
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8820
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7655
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6488
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5837
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2265
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.