Stuart MacMartin wrote:
[color=blue][color=green]
>> Absolutely. This is what Stroustrup says in TC++PL, and it is far more
>> important than the idea many programmers seem to have that the main
>> purpose
>> is to avoid name clashes. That can be done by using the C-style
>> category_subcategory_name approach.[/color]
>
> Maybe my previous reply was too wordy.
>
> I think I've completely missed the point.
> How does using "::" instead of "_" improve modularization?
>
> Stuart[/color]
In many ways, there isn't a whole lot of difference. The '_' approach is a
convention which facilitates the hierarchical organization of identifiers
according to their names. C++ namespaces are a formalization of this
concept built into the language. Among the advantages they offer are
scoping and abbreviation. The primary "hard" technical advantage to
namespace scoping is to prevent or resolve name collisions. For me that is
secondary to the psychological advantage of restricting the number of
program entities visible in a given context.
One simple "keystroke" advantage to using namespaces over "_" is that when
you are working in a given namespace all the local symbols don't require
resolution. So
foo_bar_baz_fun1();
foo_bar_baz_fun2();
foo_bar_baz_fun3();
foo_bar_baz_fun4();
can become:
namespace foo {
namespace bar {
namespace baz {
fun1();
fun2();
fun3();
fun4();
}
}
}
If you use foo::bar::baz::fun1(); a lot in a given scope you can employ a
using declaration:
using foo::bar::baz::fun1();
fun1();
It's best to introduce non-local symbols into the smallest possible
enclosing block. Depending of the tradeoffs there are times when it may
even make sense to to have a using directive:
using namespace foo::bar::baz;
One example of where I find that approach useful is with a math library I
have which contains many templated operands and associated overloaded
operators. I can simply pull the whole namespace in, and "do math" without
having to worry about introducing each symbol individually. I do this
within a function block rather than at the namespace level, in order to
prevent namespace pollution. In most cases, however, I prefer to either
introduce the fully qualified identifier with a using declaration, or
simply use it in it's long form.
Another convention I have adopted is to synchronize my namespace names with
my directory names. I've taken to calling the result a package. Take a
look at
www.openscenegraph.org for an example of what I'm talking about.
They use the term 'library' instead of 'package'.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell