 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Pep Guest
|
Posted: Wed Jun 21, 2006 9:10 am Post subject: should "using namespace std" be used? |
|
|
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? |
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Wed Jun 21, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
| Quote: | 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-lite/coding-standards.html#faq-27.5
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com> |
|
| Back to top |
|
 |
Pep Guest
|
Posted: Wed Jun 21, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
Sumit Rajan wrote:
| Quote: |
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
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-lite/coding-standards.html#faq-27.5
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) 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  |
|
| Back to top |
|
 |
Pep Guest
|
Posted: Wed Jun 21, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
Jim Langston wrote:
| Quote: | "Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
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::atol(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.
| Quote: | 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. |
|
| Back to top |
|
 |
Jim Langston Guest
|
Posted: Wed Jun 21, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
| Quote: | 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. |
|
| Back to top |
|
 |
Cy Edmunds Guest
|
Posted: Thu Jun 22, 2006 8:33 am Post subject: Re: should "using namespace std" be used? |
|
|
"Daniel T." <daniel_t (AT) earthlink (DOT) net> wrote in message
news:daniel_t-FFB63F.18015921062006 (AT) news (DOT) west.earthlink.net...
| Quote: | In article <Buemg.24212$W97.23089 (AT) twister (DOT) nyroc.rr.com>,
"Cy Edmunds" <spamless.cedmunds (AT) rochester (DOT) rr.com> wrote:
"Daniel T." <daniel_t (AT) earthlink (DOT) net> wrote in message
news:daniel_t-DD306F.08552021062006 (AT) news (DOT) west.earthlink.net...
In article <e7b0ko$ch9$1@pop-news.nl.colt.net>, Pep <pep (AT) nowhere (DOT) com
wrote:
Sumit Rajan wrote:
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
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-lite/coding-standards.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,
I don't think you have thought this through. What happens when a revised
standard comes out and the standard namespace gets another large
injections
of names? For that matter, what happens when you refactor your own code
and
introduce a symbol which conflicts with a standard identifier?
The compiler informs me of the issue and I disambiguate the code.
Maintainability is too important to compromise for the sake of avoiding
typing std:: in front of a few symbols.
That is exactly why I think putting std:: in front of all the symbols is
a bad idea. Pre-namespace, each library had to prepend every function
and type in its code with some "unique" symbol, this had to be typed
every-time one used any type or symbol from that library.
The whole point of the namespace mechanism is to remove the need for
those warts,
|
I think the point is to give the programmer a reasonable way to avoid name
collisions. Namespaces only remove warts if you take the unsafe appoach of
automatically placing a using clause at the top of each of your programs.
Doing this is the functional equivalent of using neither warts nor
namespaces.
so why on earth would you voluntarily put them right back
In old fashioned C interfaces we had to put warts on the names to prevent
collisions. For instance:
mylib_open();
mylib_close();
As you point out, it isn't a great step forward to have to type
mylib::open();
mylib::close();
However, there was never a practice of writing
std_vector<double> x;
std_cout << y;
Hence, putting potentially hundreds of names in your namespace by writing
using namespace std;
seems kind of crazy, at least for new code. (I could see it as a measure for
migrating legacy code.) In effect, namespaces allow us to put warts on all
the standard library names which, given their sheer number, seems like a
damned good idea to me.
I personally don't mind writing
mylib::open();
That, together with a telling header such as
#include "mylib/mylib.h"
gives the reader of the code a pretty good idea of where things are coming
from. And if the namespace is too long there is a mechanism to create an
alias.
| Quote: |
and refusing to have using
declarations and directives in your code defeats the purpose of the
namespace mechanism.
You must be kidding. It is "using namespace std;" which defeats the
purpose
of the namespace mechanism!
As I show above, I'm not kidding.
|
Now I understand the disconnect: we have radically different ideas about the
purpose of namespaces. |
|
| Back to top |
|
 |
Pep Guest
|
Posted: Thu Jun 22, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
Howard wrote:
| Quote: |
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7avs9$c4m$1@pop-news.nl.colt.net...
Jim Langston wrote:
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
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::atol(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.
I don't find it particularly inconvient to type the extra characters for
things lke std::cout, so most of the time I just type it out.
But if I'm working with other developers, I'll often put using statements
(such as "using std::cout;") at the top of a given function. That way,
the using statements for that function are right there at the entry to the
function, where other developers can immediately see what the following
code
is referring to. (Which is good self-documentation!)
-Howard
|
Agreed that this is a reasonable approach that I have adopted in the past
but I think my problems with the namespaces are that I invariably get
drafted in to radically upgrade and support really bad legacy code which
has routines that extend to over 1,000 lines of code in one single method,
I kid you not!
The current project I am maintaining has hundreds of routines like this,
sometimes multiple > 1,000 lines of code per method in one source file!
So at this point you can forgive developers for not noticing anything at the
top of the routine, which in this case I suppose the only safe option is to
either
use the namespace std:: prefix to all enclosed routines
or
insist that std:: namespace is the default for the project and that any
routines, templates or classes used from other namespaces that override the
std:: namespace must be prefixed with the namespace tag
I guess this is going to become one of those legacy support issues when c++
is advanced to something new  |
|
| Back to top |
|
 |
Pep Guest
|
Posted: Thu Jun 22, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
Cy Edmunds wrote:
| Quote: |
"Howard" <alicebt (AT) hotmail (DOT) com> wrote in message
news:84dmg.46946$mF2.39514@bgtnsc04-news.ops.worldnet.att.net...
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7avs9$c4m$1@pop-news.nl.colt.net...
Jim Langston wrote:
"Pep" <pep (AT) nowhere (DOT) com> wrote in message
news:e7at5c$aeq$1@pop-news.nl.colt.net...
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::atol(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.
I don't find it particularly inconvient to type the extra characters for
things lke std::cout, so most of the time I just type it out.
But if I'm working with other developers, I'll often put using statements
(such as "using std::cout;") at the top of a given function. That way,
the using statements for that function are right there at the entry to
the function, where other developers can immediately see what the
following
code is referring to. (Which is good self-documentation!)
-Howard
I agree that it is good documentation, but I find it hard to maintain. If
you refactor your code in such a way that the function is no longer used
you have to remember to get rid of your using clause also. Of course it is
not an error to claim to be using something you are not, but it is
misleading.
Cy
|
It can be hard to maintain but no more than refactoring a deprecated class
such as ostrstream for ostringstream, in both cases you have to find all
occurrences of the item being refactored.
Thanks to everyone for their input on this. I guess it is as most things in
the real world, there are many ways of handling problems each with their
own pros and cons and supporters and detractors  |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Fri Jun 23, 2006 4:42 am Post subject: Re: should "using namespace std" be used? |
|
|
"Cy Edmunds" <spamless.cedmunds (AT) rochester (DOT) rr.com> wrote:
| Quote: | "Daniel T." <daniel_t (AT) earthlink (DOT) net> wrote:
"Cy Edmunds" <spamless.cedmunds (AT) rochester (DOT) rr.com> wrote:
[snip]
In effect, namespaces allow us to put warts on all the standard
library names which, given their sheer number, seems like a damned
good idea to me.
[snip]
Now I understand the disconnect: we have radically different ideas about
the
purpose of namespaces.
So you think the reason the namespace mechanism was introduced is so
that the standard library names would have warts?
Of course! It does other useful things, too, all of which have to do with
*adding* warts. It certainly does nothing to remove warts. Consider:
Old way:
#include <iostream.h
cout << 44;
My way:
#include <iostream
std::cout << 44; // now we have warts
Your way:
#include <iosteam
using namespace std;
cout << 44; // same warts (none) as old way
So the net effect of namespaces is to introduce new warts although the using
clause gives us a way to sort of break even with some extra effort.
In spite of the unattractive name, I think warts are useful for avoiding
name conflicts as the code evolves.
|
"As the code evolves" makes it sound like some huge bug-a-boo that you
are somehow magically avoiding by putting "std::" in front of a good
chunk of the code in a cpp file.
This name conflict issue that you are so concerned about, that you add
an extra five plus characters to every reference of every type in your
code is a chimera. The rare instance when it comes up, the compiler
invariably points it out, and is easy to fix. The problem simply doesn't
exist. |
|
| Back to top |
|
 |
Bo Persson Guest
|
Posted: Sat Jun 24, 2006 9:10 am Post subject: Re: should "using namespace std" be used? |
|
|
"Markus Schoder" <a3vr6dsg-usenet (AT) yahoo (DOT) de> skrev i meddelandet
news:449c60a3$0$11067
| Quote: |
There is still the risk that new names are introduced in namespace
std in
the new standard conflicting with a heavily used name in your
project.
I consider this risk however to be fairly small since new names will
very
likely be mostly introduced through new header files thus not
affecting
existing code.
|
Right now new features are being added to existing headers in the
draft for the next standard. New features in their own headers, like
type_traits, are likely to be used by other parts of the standard
library, thus included indirectly.
I consider the risk to be fairly LARGE.
Bo Persson |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|