473,407 Members | 2,326 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,407 software developers and data experts.

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 4052
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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.

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.com> wrote in message
news:e7**********@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.

Jun 21 '06 #3

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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-lit....html#faq-27.5

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

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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-lit....html#faq-27.5

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.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**********@pop-news.nl.colt.net>, Pep <pe*@nowhere.com>
wrote:
Sumit Rajan wrote:

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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-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******@earthlink.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******@earthlink.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.com> wrote:
Marcus Kwok wrote:
Daniel T. <da******@earthlink.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**********@news-int.gatech.edu>, Marcus Kwok
<ri******@gehennom.invalid> writes
Pep <pe*@nowhere.com> wrote:
Marcus Kwok wrote:
Daniel T. <da******@earthlink.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

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
Jim Langston wrote:
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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


Jun 21 '06 #11
Daniel T. 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.

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.


As Daniel T. notes, there are different schools of thought on the
matter. The rule from _C++ Coding Standards_, item 59 (italics in
original) is: "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

Jun 21 '06 #12

"Howard" <al*****@hotmail.com> wrote in message
news:84*******************@bgtnsc04-news.ops.worldnet.att.net...

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
Jim Langston wrote:
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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
Jun 21 '06 #13

"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
In article <e7**********@pop-news.nl.colt.net>, Pep <pe*@nowhere.com>
wrote:
Sumit Rajan wrote:
>
> "Pep" <pe*@nowhere.com> wrote in message
> news:e7**********@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-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,


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?
Maintainability is too important to compromise for the sake of avoiding
typing std:: in front of a few symbols.

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!

See "C++ Coding Standards" by Sutter & Alexandrescu for a more
reasonable rule regarding the "using" keyword.

Jun 21 '06 #14

"Marcus Kwok" <ri******@gehennom.invalid> wrote in message
news:e7**********@news-int.gatech.edu...
Pep <pe*@nowhere.com> wrote:
Marcus Kwok wrote:
Daniel T. <da******@earthlink.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


From the cited article:

In short, a good long-term solution for namespace usage should follow at
least these rules:

Namespace Rule #1: Avoid using directives entirely, especially in header
files.

The part of the article recommending using clauses has to do with updating
legacy code. This is a lot different than "Adding "using namespace std" in
source files is fine...".
Jun 21 '06 #15
Pep wrote:
Is it best to include the code "using namespace std;" in the source


Yes.

The argument that you want to avoid namespace conflict is a very weak
one.

The only time you wouldn't want to include "using namespace std;" is
when you are writing code that makes absolutely no use of standard c++
namespace objects.

Jun 21 '06 #16
In article <Bu*******************@twister.nyroc.rr.com>,
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
In article <e7**********@pop-news.nl.colt.net>, Pep <pe*@nowhere.com>
wrote:
Sumit Rajan wrote:

>
> "Pep" <pe*@nowhere.com> wrote in message
> news:e7**********@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-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,


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, so why on earth would you voluntarily put them right back
in again?
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.
Jun 21 '06 #17
In article <e7**********@news-int2.gatech.edu>,
ri******@gehennom.invalid (Marcus Kwok) wrote:
Daniel T. <da******@earthlink.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.


I had started to word it much like you did, so I guess we are on the
same page. :-)
Jun 21 '06 #18
In article <e7**********@news-int.gatech.edu>,
ri******@gehennom.invalid (Marcus Kwok) wrote:
Pep <pe*@nowhere.com> wrote:
Marcus Kwok wrote:
Daniel T. <da******@earthlink.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


Mr. Sutter later revised his opinion on this matter, which is why I
explicitly referenced his recent book.
Jun 21 '06 #19

"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
In article <Bu*******************@twister.nyroc.rr.com>,
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
> In article <e7**********@pop-news.nl.colt.net>, Pep <pe*@nowhere.com>
> wrote:
>
>> Sumit Rajan wrote:
>>
>> >
>> > "Pep" <pe*@nowhere.com> wrote in message
>> > news:e7**********@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-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,
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 again?
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.
> 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.
Jun 22 '06 #20
Pep
Howard wrote:

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
Jim Langston wrote:
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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 ;)

Jun 22 '06 #21
Pep
Cy Edmunds wrote:

"Howard" <al*****@hotmail.com> wrote in message
news:84*******************@bgtnsc04-news.ops.worldnet.att.net...

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
Jim Langston wrote:

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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 :)

Jun 22 '06 #22
In article <7c******************@twister.nyroc.rr.com>,
"Cy Edmunds" <sp***************@rochester.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?
Jun 22 '06 #23
Daniel T. <da******@earthlink.net> wrote:
In article <e7**********@news-int.gatech.edu>,
ri******@gehennom.invalid (Marcus Kwok) wrote:
Here is a good article on namespace usage (especially on adding
namespaces to existing code):

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


Mr. Sutter later revised his opinion on this matter, which is why I
explicitly referenced his recent book.


Oh ok, thanks for the info. I will check it out sometime.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 22 '06 #24

"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
In article <7c******************@twister.nyroc.rr.com>,
"Cy Edmunds" <sp***************@rochester.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.

Cy
Jun 22 '06 #25
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote:
"Cy Edmunds" <sp***************@rochester.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.
Jun 22 '06 #26

"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@pop-news.nl.colt.net...
Jim Langston wrote:
"Pep" <pe*@nowhere.com> wrote in message
news:e7**********@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.


Personally, I never use the using statement at all. I always just prefix
std:: as I don't find it that difficult at all. Of course typing 70+ wpm
helps. I find that not using using at all means I never have to worry about
what I'm bringing into the unnamed namespace and never get the hard to find
problems that others run into.

I used to program in C and I had run into the problem of two different
modules using the same function names and it was a pain to deal with. I
wound up hacking one of the modules renaming the functions everywhere. I
was overjoyed to find that C++ has namespaces to get rid of this problem,
and seeing how it's something that's extremely useful I'm going to do
everything I can to make sure I don't short circuit it, which I see as the
using statement as doing.
Jun 23 '06 #27

"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote:
> "Cy Edmunds" <sp***************@rochester.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.


So you think the committee put namespaces into the language because they are
stupid? Name conflicts are a common problem in large projects.

The problem which doesn't exist is having to type std:: once in a while.

Cy
Jun 23 '06 #28
In article <mK******************@twister.nyroc.rr.com>,
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote:
> "Cy Edmunds" <sp***************@rochester.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.


So you think the committee put namespaces into the language because they are
stupid? Name conflicts are a common problem in large projects.


No, I think they put the namespace mechanism into the language so that
library venders wouldn't have to make every-one put warts at the
beginning of all the types used in a program. I explained this already.
The problem which doesn't exist is having to type std:: once in a while.


Well, I guess that depends on how extensively you use standard
components and libraries that actually are in namespaces. If you are
only prepending the namespace "once in a while" I agree with you
completely
Jun 23 '06 #29
Cy Edmunds wrote:
"Daniel T." <da******@earthlink.net> wrote in message
news:da****************************@news.west.eart hlink.net...
"Cy Edmunds" <sp***************@rochester.rr.com> wrote:
"Daniel T." <da******@earthlink.net> wrote:
> "Cy Edmunds" <sp***************@rochester.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?
[snip]
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.


So you think the committee put namespaces into the language because they
are stupid? Name conflicts are a common problem in large projects.

The problem which doesn't exist is having to type std:: once in a while.


You seem to miss the point. On the rare occasion were you hit a name
conflict you can use explicit scope resolution to resolve it. This is what
you cannot not do without namespaces.

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.

Jun 23 '06 #30

"Markus Schoder" <a3*************@yahoo.de> skrev i meddelandet
news:449c60a3$0$11067

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
Jun 24 '06 #31

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

Similar topics

5
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
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 ...
8
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...
11
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?...
4
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
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
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
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...
0
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
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.