Pros and cons "using namespace"

S

SirMike

BigMan said:
When should one use the using directive (i.e. using namespace)?
When "using" namespaces ;))
I do it this way: when I use more than one namespace i declare variables
like std::variable, mynamespace::variable etc.
When use only one namespace I write "using namespace namespace_name" at
the top of the file.
 
B

BigMan

OK, suppose you have a file that uses only one namespace. You must have
used the using directive (i.e. using namespace <namespace_id>). Now you
have to modify this file and add code that refers to another namespace?
Do you keep the using directive or do you rewrite existing code and
substitute using declarations (e.g. using std::cout) for the using
directive (e.g. using namespace std)?
 
H

Heinz Ozwirk

SirMike said:
When "using" namespaces ;))
I do it this way: when I use more than one namespace i declare variables
like std::variable, mynamespace::variable etc.
When use only one namespace I write "using namespace namespace_name" at
the top of the file.

But as soon as you are using ony named namespace, you are using multiple namespaces - the global namespace and the named one. So, by your rule, you should never use "using namespace <whatever>".

Heinz
 
S

SirMike

Heinz said:
But as soon as you are using ony named namespace, you are using multiple namespaces - the global namespace and the named one. So, by your rule, you should never use "using namespace <whatever>".
You're right :) I wrote it like a dumb :D
Mostly I work with std. When I need to use other namespace, simply write
namespace::blahblah
 
J

Jeremy J

When should one use the using directive (i.e. using namespace)?

I usually only use it in .cpp (or .cc *wink*) files. If I get a name
collision then I manually (search and replace) change the affected
names. Not the best, but I find that my clarity of code improves
vastly when I omit std:: or boost:: before the several dozen library
calls in a typical source file.

using namespace std;
using namespace boost;

I find that names rarely (well never, for me) collide, but I am
usually using std with boost, and some pains have been taken with
boost to avoid clashing with namespace std. I am sure that other
libraries are less carefull.

J. Jurksztowicz
 
R

Ravi

Jeremy said:
only in source files

I do this as well. I don't think it's a bad practice to 'using' a
namespace as long as the code maintained doesn't change dependencies on
code that could be in other projects. This means generally don't place
it in a header file.

You could scope the using namespace std/boost/whatever; in a class, in
the header file, but this does not remove the dependency completely,
for example if someone else inherits your class, they are stuck with
your using statement. -I'm not sure on that actually. Someone quote the
standard.

I don't put the using statement in any "public" code (header files).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top