I have inherited a horrible bit of legacy code which includes about 1000 lines of utility class definition that needs to appear before the "real" code in a source file. To avoid clashes with other modules that might also have associated legacy classes, I have put the utility class into an unnamed namespace:
namespace {
class OldUtils {
OldUtils();
int foo();
double bar();
};
OldUtils::OldUtils() {
// hundreds of lines
}
int OldUtils::foo() {
// hundreds more lines
}
...
}
class ActuallyInteresting {
// uses OldUtils
};
But I would prefer to have the ActuallyInteresting
code that people will be (actually) interested in near the top of the file, e.g. starting on line 50, than right at the bottom e.g. starting on line 1000. Splitting the horrid utility class into a separate compilation unit isn't an option, for higher-level reasons I won't go into!
So I am wondering if it is possible to put the short class declaration -- without method definitions -- in an unnamed namespace at the top of the file, and the much longer method definitions in another unnamed namespace at the bottom:
namespace {
class OldUtils {
OldUtils();
int foo();
double bar();
};
}
class ActuallyInteresting {
// uses OldUtils
};
namespace {
OldUtils::OldUtils() {
// hundreds of lines
}
int OldUtils::foo() {
// hundreds more lines
}
...
}
Will these two "separate" unnamed namespaces be treated as the same scope within the compilation unit, or will different unique namespaces be generated for each? Does the standard have anything to say about this?