If a function is declared as such:
namespace foo {
void bar();
}
Most people define the function like this:
void foo::bar() {
...
}
However I like to do it this way:
namespace foo {
void bar() {
...
}
}
I prefer this style because its saves me from always retyping foo::, which is often tedious in function parameters that accept types declared in that same namespace. Plus its easier to rename the whole namespace.
I’m wondering why I almost never see my style in other peoples code, are there any disadvantages too it, besides the additional indentation level (and you don’t even have to indent namespaces)?
If you use the
foo::barform, and accidentally define the function withincorrect arguments, you will get a compiler error. If you put the
definition in a namespace in the source file, different arguments will
simply result in a different function being defined. You won’t get an
error until you try to link with code which uses your function (which in
the case of a DLL, may not be until runtime).