Struct enum

From vegard.wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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