should "using namespace std" be used?

should "using namespace std" be used?

Post by Pep » Thu, 22 Jun 2006 16:40:30


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?
 
 
 

should "using namespace std" be used?

Post by Jim Langst » Thu, 22 Jun 2006 16:50:51


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.

 
 
 

should "using namespace std" be used?

Post by Pep » Thu, 22 Jun 2006 17:26:50


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.


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.
 
 
 

should "using namespace std" be used?

Post by Sumit Raja » Thu, 22 Jun 2006 17:33:32


This is covered in the FAQ:
http://www.yqcomputer.com/ ++-faq-lite/coding-standards.html#faq-27.5

Regards,
Sumit.
 
 
 

should "using namespace std" be used?

Post by Pep » Thu, 22 Jun 2006 17:39:53


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

should "using namespace std" be used?

Post by Daniel T » Thu, 22 Jun 2006 21:55:19

In article <e7b0ko$ch9$ XXXX@XXXXX.COM >, Pep < XXXX@XXXXX.COM >






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.
 
 
 

should "using namespace std" be used?

Post by ricecak » Thu, 22 Jun 2006 22:22:32


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
 
 
 

should "using namespace std" be used?

Post by Pep » Thu, 22 Jun 2006 22:29:34


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

should "using namespace std" be used?

Post by ricecak » Thu, 22 Jun 2006 22:48:52


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

http://www.yqcomputer.com/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
 
 

should "using namespace std" be used?

Post by Richard He » Thu, 22 Jun 2006 23:29:41

In message <e7bio4$7kn$ XXXX@XXXXX.COM >, Marcus Kwok
< XXXX@XXXXX.COM > writes



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
 
 
 

should "using namespace std" be used?

Post by Howar » Thu, 22 Jun 2006 23:53:56


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
 
 
 

should "using namespace std" be used?

Post by mlimbe » Fri, 23 Jun 2006 00:34:27


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
 
 
 

should "using namespace std" be used?

Post by Cy Edmund » Fri, 23 Jun 2006 01:15:12

"Howard" < XXXX@XXXXX.COM > wrote in message
news:84dmg.46946$ XXXX@XXXXX.COM ...

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


 
 
 

should "using namespace std" be used?

Post by Cy Edmund » Fri, 23 Jun 2006 01:30:25


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

You must be kidding. It is "using namespace std;" which defeats the purpose
of the namespace mechanism!
 
 
 

should "using namespace std" be used?

Post by Cy Edmund » Fri, 23 Jun 2006 01:36:02


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