It's legal to define namespace members outside of their namespace as long as their name is prefixed with the name of their namespace, and the definition actually occurs in a namespace that encloses it. It can't happen in a namespace that's nested inside the member namespace.
namespace A { void f(); }
void A::f() { } // prefix with "A::"
namespace B { }
void B::f() { } // invalid! not declared in B!
namespace C { void f(); }
namespace D { void C::f() { } } // invalid! D doesn't enclose C
namespace E {
void f();
namespace F {
void E::f() { } // invalid! F is nested inside E!
}
}
It's the same stuff as for class members, where you can also define functions outside of their class, as long as you prefix the names with the name of the class. However as for classes, namespace members must be first declared in their respective namespace before they can be defined outside out it.