5

In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace.

I was wondering if that also means that I can also use it within class definitions like this:

class Foo {
    enum class Color
    {
        red, 
        blue
    };
    using enum Color;
};

int main()
{
    Foo::Color c = Foo::red;
}

Or do I still need to give the full namespace?:

    Foo::Color c = Foo::Color::red;

I tried it in wandbox.org, but it seems that neither gcc nor clang know about using enum yet.

the_summer
  • 439
  • 3
  • 10

1 Answers1

8

Yes, Foo::Red will work fine. using enum E behaves as, from [enum.udecl]:

A using-enum-declaration introduces the enumerator names of the named enumeration as if by a using-declaration for each enumerator.

And the standard contains an example of exactly this case:

[ Note: A using-enum-declaration in class scope adds the enumerators of the named enumeration as members to the scope. This means they are accessible for member lookup. [ Example:

enum class fruit { orange, apple };
struct S {
  using enum fruit;             // OK, introduces orange and apple into S
};
void f() {
  S s;
  s.orange;                     // OK, names fruit​::​orange
  S::orange;                    // OK, names fruit​::​orange
}

— end example ] — end note ]


Note however that there is some controversy around the particular spelling for this feature. enum E is what's known as an elaborated type specifier (much like class C or struct S). Typically, elaborated type specifiers behave exactly the same was as their underlying versions. Elaborating is just meant to disambiguate, and you rarely need to disambiguate, so you wouldn't see it very often. However, in this particular case, using enum E and using E actually mean wildly different and wholly unrelated things. So keep in mind that there is a chance that this feature may not yet actually make C++20, despite currently being in the working draft and even having been published in the CD. As such, it's unlikely that compilers will implement this feature until they're sure it's necessary to implement (C++20 isn't exactly lacking in work for compiler writers...)

Barry
  • 286,269
  • 29
  • 621
  • 977
  • 2
    That's even better than I imagined. Importing enum classes from outside the class. That should make it much easier to move to enum classes now without most users noticing it. – the_summer Sep 12 '19 at 19:48
  • Interestingly, the chosen solution to the elaborated type specifier issue was to define `using enum` as simply the `using` keyword followed by an `enum`'s elaborated type specifier, thus defining `using enum E` as a special case of `using E` rather than as unrelated to `using E`. – Justin Time - Reinstate Monica Feb 01 '21 at 19:54
  • @JustinTime-ReinstateMonica I don't understand your point? – Barry Feb 01 '21 at 21:48
  • Just commenting on how they solved the issue of `using enum E` and `using E` meaning different things, @Barry. – Justin Time - Reinstate Monica Feb 02 '21 at 01:35