Why is the definition of a member function required to be in the same namespace as its class?
To avoid having to write an obscene number of definitions for each deriving class in this codebase, I have created an implementation stub as a class template and am defining the actual functions based on a type enum.
That'd be all fine and dandy if it weren't the case that C++ requires the definition of a member function to be in the same namespace as its class. In this case, I am not simply specializing for each type; there are other things going on which necessitate a separate namespace -- hence the problem.
GCC and Clang report a similar error, suggesting this is a Standard thing.
Both GCC and Clang know precisely what is going on (otherwise they would not be able to restrict it), and are therefore not stupid enough to understand and correctly compile the situation. I consider this limitation arbitrary because the class is clearly scoped and there is no reason to suggest a compiler cannot figure out what the definitions pertain to.
So, is there any rationale for requiring the definition of a member function to be in the same namespace as its class? Where can I find this requirement in the specification?
Here's an example of what I'm up to:
namespace A {
enum class Type : unsigned {
SomeType,
// others
}
class Base {
protected:
virtual void f() = 0;
// other functions
// ctor, dtor, operator= definitions (=delete and =default)
};
template<Type type, class Data>
class Impl final : public Base {
private:
Data m_data;
void f() override;
public:
// default ctor, dtor, operator= definitions
// also use a Data&& ctor for instantiation
};
} // namespace A
// later
namespace B {
struct SomeTypeData {
// simple POD/SL of data for this particular type
};
void A::Impl<Type::SomeType, SomeTypeData> f() {
// implementation
}
// other area-specific definitions: type info, group-specific
// functions, instantiation, etc.
} // namespace B
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]