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.

2
  • Do note that C++20 has not yet been ratified so it is not surprising that compilers do not have full support. Commented Sep 12, 2019 at 19:46
  • @NathanOliver yes, I know. It wasn't meant as a criticism. Just an observation. Commented Sep 12, 2019 at 19:50

1 Answer 1

9

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...)

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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.
@JustinTime-ReinstateMonica I don't understand your point?
Just commenting on how they solved the issue of using enum E and using E meaning different things, @Barry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.