views:

102

answers:

4

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?

+2  A: 

The problem here is that the using declaration pulls in only the name of the enum, and not the names of its values. Enum's aren't scopes and don't carry along the names of the enumerators. I don't think it is possible to import the enum values themselves. Try wrapping the enum in a struct/namespace and use it.

Abhay
A: 

If you really need to do this, try using namespace foo; instead of using foo::bar;. However, it's a much better idea to encapsulate the enum in a class or in another namespace.

In silico
+3  A: 

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;
}
Mark B
A: 

While i like the approach of Mark B best, because it doesn't break existing code, you can also do the following:

namespace foo {
 enum bar {
  A, B [..]
 };
}

namespace buzz {
 using foo::bar;
 using foo::A;
 using foo::B;
 [..]
}
LumpN