3

Is there a way to use some kind of using directive to directly access members of an enum class type?

enum class Foo {
    Foo1,
    Foo2,
    ...
};

int main() {
    auto foo = Foo::Foo1;
    ??? // magic command to make the lines below work
    auto foo1 = Foo1;
    auto foo2 = Foo2;
    ...
}

I know I can do it with namespaces, so an alternative would be to use a namespace and a traditional enum:

namespace Foo {
    enum FooEnum {
        Foo1,
        Foo2,
        ...
    };
}

int main() {
    auto foo = Foo::Foo1;
    using namespace Foo;
    auto foo1 = Foo1;
    auto foo2 = Foo2;
    ...
}

However, I would like to keep the advantages of the class enums (like type safety, etc).

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
  • Wait... you say you want to use class enums, then why would you want to write the second line? You cannot even write `Foo foo2 = Foo2`, can you? So why would you expect it to work if you replace `Foo` by `auto`? – CompuChip Nov 20 '15 at 13:08
  • @CompuChip No, you're right. The question has nothing to do with auto, I also would like to make the line `Foo foo2 = Foo2` work with my magic line. – Jan Rüegg Nov 20 '15 at 13:10
  • Jan, thanks, then I understand the question. – CompuChip Nov 20 '15 at 13:11
  • 1
    see http://stackoverflow.com/questions/9450338/equivalent-of-using-namespace-x-for-scoped-enumerations – Sander De Dycker Nov 20 '15 at 13:14
  • @SanderDeDycker Seems like I didn't look hard enough when searching my question, thanks :) – Jan Rüegg Nov 20 '15 at 13:16

0 Answers0