When a function is declared in a namespace, for example:
namespace Mine
{
void f();
}
there are two alternatives for defining the function. You can either
qualify the function name with the namespace name:
void Mine::f()
{
// ...
}
or you can reopen the namespace:
namespace Mine
{
void f()
{
// ...
}
}
In C++PL 3rd (section 8.2.9.3), where this example comes from,
Stroustrup recommends the first form. His rationale is that, if you
inadvertently mismatch the name or signature, the compiler will catch
the error with the first form, but not the second. In other words,
this triggers a compile error (no ff() declared in Mine):
void Mine::ff() // oops, I meant f()
{
// ...
}
but this does not; it adds a new function instead:
namespace Mine
{
void ff()
{
// ...
}
}
I do not find this rationale very convincing, because you will still
get a link error when you try to call f(). (Also, the rationale only
applies to non-member functions. With members, you would get a
compile error with either form.) However, perhaps I have missed
something.
The second form has the benefit that it is less verbose in the usual
case where many functions are being defined in the same namespace.
Also, it avoids cluttering the function definition.
Which would you consider the preferred form? And is it the same for
both members and non-members?
Ivan Johnson
--
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]