I have an enum in a namespace and I'd like to use it as if it were in a different namespace. Intuitively, I figured I could use 'using' or 'typedef' to accomplish this, but neither actually work. Code snippet to prove it, tested on GCC and Sun CC:
namespace foo
{
enum bar {
A
};
}
namespace buzz
{
// Which of these two methods I use doesn't matter,
// the results are the same.
using foo::bar;
//typedef foo::bar bar;
}
int main()
{
foo::bar f; // works
foo::bar g = foo::A; // works
buzz::bar x; // works
//buzz::bar y = buzz::A; // doesn't work
buzz::bar z = foo::A;
}
The problem is that the enum itself is imported but none of its elements. Unfortunately, I can't change the original enum to be encased in an extra dummy namespace or class without breaking lots of other existing code. The best solution I can think of is to manually reproduce the enum:
namespace buzz
{
enum bar
{
A = foo::A
};
}
But it violates the DRY principle. Is there a better way?
Wrap the existing namespace in a nested namespace which you then "use" in the original namespace.
namespace foo
{
namespace bar_wrapper {
enum bar {
A
};
}
using namespace bar_wrapper;
}
namespace buzz
{
using namespace foo::bar_wrapper;
}