Reputation: 93
I was integrating some C++ in somelse's one and found out we adopt two different strategies regarding the use of using namespace commands.
For cleanliness of source code, which of the two is the most correct solution?
namespace foo
{
using namespace bar;
}
or
using namespace bar;
namespace foo
{
}
Thanks a lot for your help,
T.
Upvotes: 5
Views: 263
Reputation: 2210
This question is already answered, but I want to reiterate the point mooted by Toby Speight
.
One should never write something like using namespace bar
.
Good practice is to use using
as a aliasing aid for long/hierarchial namespaces
. Like
using po = boost::program_options;
And then use
po.notify(vm);
Either of two alternatives suggested in question are only good for discussions or interviews.
Upvotes: 0
Reputation: 76298
The two are not equivalent. In the first case the namespace bar
is imported in the namespace foo
so for every bar::x
you can access it as foo::x
. In the latter the namespace bar
is imported in the global namespace (or the namespace that wraps both up) and it can be accessed as ::x
.
I'd recommend to always choose the narrowest possible solution for you. Even to the point of including the namespace only in the function you actually need it. So if you are in doubt go with the first one.
Upvotes: 7