On Feb 9, 5:42 pm, "toton" <abirba...@gmai l.comwrote:
Hi,
Is it possible to have a class level namespace opening instead of
file level .
Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};
}}}}
Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.
thus in the file,
namespace parser = com::my_company ::my_project::p arser;
Thus it is in the file scope.
Then use it as parser::my_pars er in the header;
Putting your namespace alias in the header would normally be something
to avoid. By putting it in a header, you are making it visible to
anyone who includes that header. Unless this is intentional and that
clients using the header expect this behavior, it would normally be
considered bad practice, bad design or both. Having said that, your
own situation may make this a perfectly valid choice, so take my
comments with a grain of salt if you like. ;)
A more dangerous habit would be to put the following in a header:
// maybe append ::parser to the following, but see below
using namespace com::my_company ::my_project;
The above would bring my_project's contents into scope, which includes
parser, but also everything else in my_project. You could
append ::parser to the above using statement, but then there would be
no point in having the parser namespace at all. The reason I mention
this is to contrast it to what a namespace alias does for you. The
using statement makes the parser namespace redundant, whereas your
namespace alias makes parser's contents available via more than one
"scope path". This is not an error, but it does create ambiguity in
your interface. Again, if this is intentional, then fine, but normally
you wouldn't want to intentionally create such ambiguity when you have
a choice. In your own .cpp files, go nuts and use the namespace alias
as you want (that's what it is there for), but in header files, you
should give serious thought to whether you really want to do that to
your interface.
--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia