How do you import an enum into a different namespace in C++?
Asked Answered
R

6

31

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?

Rosettarosette answered 20/7, 2010 at 18:49 Comment(6)
"But it violates the DRY principle." In this case, I'd reconsider using the principle. "Is there a better way?" enums (unfortunately) aren't scopes, but you can put enum elements into additional struct or namespace.Eran
Unfortunately lots of other code relies on the original enum not being in another scope :/Rosettarosette
Whoa... Had someone asked me something like that, I would have answered it should have worked. Thanks for revealing this dark corner of C++... +1.Puccoon
Note that this is yet another reason to wrap enums inside their own dedicated namespace or struct (for pre-C++0x code)...Puccoon
"Unfortunately lots of other code relies on the original enum not being in another scope", well it is possible to fix "other code" - if you put enum in scope and rebuild everything, you'll get compile errors that will point at locations where enums should be fixed. It is a dangerous operation (you can cause massive screwup by one typo), but it can be done. Enums outside of scopes pollute namespaces, so it is possible that you'll have to put them into scopes in the future anyway...Eran
Under C++11, buzz::bar y = buzz::bar::A; works.Mcloughlin
G
27

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;
}
Gyral answered 20/7, 2010 at 19:12 Comment(0)
A
10

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;
 [..]
}
Achelous answered 20/7, 2010 at 19:32 Comment(0)
M
6

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.

Merci answered 20/7, 2010 at 19:2 Comment(1)
Side comment: In C++0x enums are kind of contexts through which you can access the enumerated values. enum A { a,b }; A::a; will be valid code, and once that is in place, the simple using or typedef approaches will work: namespace A { enum X { a, b }; } namespace B { using A::X; } int main() { B::X x = B::X::a; }Tamarin
C
5

Starting from C++11 you can use enum class. Importing enum class imports all its values:

namespace foo
{

enum class bar {
    A
};

}

namespace buzz
{
using foo::bar;
}

int main()
{
    foo::bar f;
    foo::bar g = foo::bar::A;

    buzz::bar x;
    buzz::bar y = buzz::bar::A;
    buzz::bar z = foo::bar::A;
}

The code above successfully compiles: http://coliru.stacked-crooked.com/a/2119348acb75d270.

Comforter answered 1/3, 2018 at 9:2 Comment(1)
This is not a solution to the question. The poster wants to be able to type buzz::bar x = buzz::A;, which doesn't work with class enumerations either.Glori
U
2

There is finally a good solution for this in C++20. The using enum declaration imports all of the enumeration values into the current scope. In your example:

namespace foo {
enum bar {
  A
};
}

namespace buzz {
using foo::bar;
using enum foo::bar;
}

int main() {
  foo::bar f;
  foo::bar g = foo::A;

  buzz::bar x;
  // now this works
  buzz::bar y = buzz::A;
}

Note that you still also need a plain using declaration in order to get the enumeration’s name in the scope.

Uralian answered 1/7, 2023 at 4:21 Comment(0)
S
1

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.

Sonde answered 20/7, 2010 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.