What's an enum class and why should I care?

前端 未结 4 1647
谎友^
谎友^ 2021-01-12 19:21

For one who has never written a line of C++11, and who has, at the moment, no opportunity to program in C++11, can you, in one short paragraph., tell me:

What is an

相关标签:
4条回答
  • 2021-01-12 19:48

    At first I was confused by your question, but I think you want to know the difference between c++ enum and that in c++11. As best as I can understand, in the later, you have strongly typed enums which allows you to scope your them. I think this explains it well. CHEERS

    0 讨论(0)
  • 2021-01-12 19:51

    enum class is called a scoped enumeration. It prevents polluting the namespace where the enumeration appears with the names of the enumerators.

    In C++03, you could do effectively the same thing by putting the enum inside a dedicated class. Perhaps that's the source of the syntax, which is a bit confusing.

    Another difference is that the enumerators of such a type don't convert implicitly to int (static_cast<int> is required). This may be seldom needed but it makes it safe to overload a function taking an int argument with one taking enum type. You can be sure the int won't be called by accident. Or you can define pseudo-integral types with dedicated operator functions, and be sure that built-in operators won't interfere.

    It's a bit annoying that these two unrelated differences come in the same package, and that you can't get an unscoped enumeration with no implicit conversion, but generally both changes are Good Things and enum class is a good default practice in C++11.

    EDIT: A scoped enumeration is defined like this:

    enum class duck { huey, dewey, louie };
    

    and must be used with the scope resolution operator :: like this:

    duck culprit = duck::huey; // or "auto culprit" to avoid redundancy
    

    Note that the :: operator also works with C++03 unscoped enumerations, so the second line above would work even if the first was missing class.

    This might be excessive detail, but class does not go into the elaborated-type-specifier if forward declaring the enumerated type, as in

    void quack( enum duck whom ); // not "enum class"
    

    However, there is a construct new in C++11, the opaque-enum-declaration, which does include the class keyword and defines a complete type.

    enum duck; // duck is declared as incomplete type
    enum class duck; // duck is now complete type; underlying type defaults to int
    

    The keyword struct can be substituted for class with no semantic difference.

    0 讨论(0)
  • 2021-01-12 20:01
    1. You can explicitly specify the storage type when the data size is important (packing it in a struct perhaps).
    2. The enumeration values are scoped within the name of the enum only; before c++11 they leaked into the enclosing scope.
    3. I seem to recall the conversion rules were also changed somewhat...

    In relation to point one 1, the storage size of enums would change before C++11 depending on the largest value assigned to an enumeration. Usually it doesn't matter so much, but when it does you have to resort to ugly hacks to force the size.

    As for point 3, in C++11 enums are not implicitly convertible or comparable to ints or other enum types: useful for avoiding function overloading headaches and other implicit conversion gotchas.

    0 讨论(0)
  • 2021-01-12 20:01

    Personnally I have used it in a tcp based messaging protocol: many of the fields were enum values that I needed to encode inside one byte only, so as to respect the messaging interface..

    All my enums were simply defined this way:

    enum class EnumFieldA : unsigned char { eValProperty1, eValProperty2};
    enum class EnumFieldB : unsigned char { eValProperty1, eValProperty2, eValProperty3};
    enum class EnumFieldC : unsigned char { eValProperty1, eValProperty2, eValProperty3, eValProperty4};
    ...
    

    there are also plenty of thorough answers in this SO question.

    0 讨论(0)
提交回复
热议问题