Re: namespace naming guidelines
sure@europa.com wrote:
alex wrote:
sure@europa.com wrote:
In C++ Coding Standards by Sutter and Alexandrescu, there are two
guidelines dedicated to namespace scoping:
57. Keep a type and its nonmember function interface in the same
namespace.
58. Keep types and functions in separate namespaces unless they're
specifically intended to work together.
If I follow this advice I'll be creating lots more namespaces.
Although the book implies I should have a lot of namespaces, it
doesn't offer suggestions for regular ways to name them.
Does the book really implies that?
Um, that's the way I read it.
I don't read it that way.
The first rule simply says that two different things have to be
in the same namespace. It's pretty essential if ADL is to work
correctly.
I don't quite understand the second. I suspect that the intent
is to avoid ADL accidentally finding functions, but if the
function takes a specific type, it falls under the first rule,
and if it doesn't, then ADL won't pull it in anyway.
My own practice is to use rather larger namespaces, with no
namespaces at the application level, and just one namespace per
large, multiproject library. I tried being more precise, but it
didn't work for me---I ended up with qualified names that filled
a complete line.
I believe that both guidelines are
aimed at making Argument Dependent Lookup (Koenig lookup) do the
right thing.
The way I read it, if I have one class and a collection of free
functions that cooperate with that class, then I should wrap all
those together in a unique namespace.
It doesn't say that the namespace has to be unique. It says
that they have to be in the same namespace. If you consider the
standard library, for example, this rule would mean that all of
the operators on std::complex must be in the same namespace as
the class, but it doesn't say that other things can't be in that
namespace.
Of course it's not really that simple. Say I have a few closely
interacting classes, e.g. Vector and Matrix, then I'd lump that
related set of classes plus their free functions in one namespace.
But the whole point is to make ADL do the right thing.
Sutter and Alexandrescu thought it was important enough to
devote two guidelines to it. They also selected guideline 58, the
second one, as the most important guideline in the namespaces
and modules section.
I presume they gave reasons. My experience with namespaces is
that they create extra work for not much benefit. For
independant libraries, they're more or less necessary, but other
than that, I'd avoid them.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]