Accessing scoped enumerators via class member access operator

93 views Asked by At

Sample unit:

struct Supercalifragilisticexpialidocious
{
    enum class Frob
    {
        Foo, Bar, Baz, Qux
    };

    Frob frob;
};

Supercalifragilisticexpialidocious maker();

void f()
{
    auto g = maker();

    // Allowed?
    if ( g.frob == g.Frob::Baz ) {}
}

This code is accepted by gcc 9.x and 10.x, but rejected by clang 10.x and 11.x (godbolt link) with an error like:

<source>:17:28: error: 'Supercalifragilisticexpialidocious::Frob::Baz' is not a member of class 'Supercalifragilisticexpialidocious'

My question is: is this code correct (i.e. is it just a clang bug?)


Background: I'd like to test g.frob == Supercalifragilisticexpialidocious::Frob::Baz but without having to repeat the classname. And in a more general situation where we obtained the object by auto, the classname might not immediately be obvious, or could be difficult due to templates.

I came up with the workaround of decltype(g)::Frob::Baz which seems to work in both compilers for the above code -- although if g were a reference, this is again accepted by gcc and rejected by clang.

0

There are 0 answers