Struct enum

From vegard.wiki
Jump to navigation Jump to search

Not to be confused with "enum structs" (or "enum classes"), the struct enum is a construct for encapsulating enum values in a struct, e.g.:

struct Foo {
    enum Type {
        A,
        B,
        C,
    };
};

Usage:

int main(int argc, char *argv[])
{
    // error: 'A' was not declared in this scope
    //auto x = A;

    // works, 'y' has type Foo::Type
    auto y = Foo::A;

    // works, 'z' has type Foo::Type
    Foo::Type z = Foo::A;

    // works!
    int u = Foo::A;

    // error: invalid conversion from 'int' to 'Foo::Type'
    //Foo::Type v = u;

    // error: invalid conversion from 'int' to 'Foo::Type'
    //Foo::Type v(u);

    // works!
    auto w = Foo::Type(u);

    return 0;
}

See also