J
Jim Langston
Daniel Kraft said:Hi all,
in my C++ projects, I usually make "heavy use" of namespaces to
"encapsulate" my code. When I have for instance something like this:
namespace project
{
namespace part1 { ... }
namespace part2 { ... }
}
And I want to use this somewhere else, there seem to be four
possibilities:
1) using namespace project; using namespace part1; using namespace part2;
2) using project:art1::Class1; using project:art2::Class2;
3) namespace p1=project:art1;
4) prepend everything with project:art2::
When I'm writing a cpp-file, I usually find myself using 1) -- I think
this is not really good, but as I'm affecting only my own code therein, it
shouldn't be too bad.
But what to do for a header-file which might be included by someone else?
1) and 2) bring things into his namespace he does not want, and even 3)
introduces a namespace-shortcut he does not need.
So only 4) seems to leave everything without impact, except that I do not
really want to type project:art2:: every time and at least to my taste
this makes the code more unreadable.
What are best practices for this problem, if there are any? Or what would
you suggest to do?
In my own code I don't make heavy use of namespaces, but I do use a
namespace around my utility header (std::string trim funciton, stream
converstion, etc...) and I just used a small name for the namespace: jml
(which happen to be my initials). so I go with 4.
Now, project::this project::that does seem to be a lot of typing, maybe you
could do a 5)
#define prj project
then just have to do
prj::this prj::that ?
I would presume that most namespace names would be rather largers (such as
boost: so a 3 letter shortcut shouldn't hurt you.