The new C ++ standard makes it possible to use a new type of enumeration, for me personally it is much more convenient than the old ordinary enum "enumerations", but there is one problem - it is not very convenient for me personally, here’s an example and the essence of the problem:

 enum class Colors: DWORD { RED = 0xFFFF0000, GREEN = 0xFF00FF00 ... } 

Begin to use:

 void draw2DBox(DWORD col) { ...//Не важно что здесь } int main() { draw2DBox(Colors::RED )//Компилятор считает Colors::RED как какой-то объект а не член типа DWORD return 0; } 

It is necessary to do type conversion:

 int main() { draw2DBox((DWORD)Colors::RED )//Так компилируется - но это жутко не удобно return 0; } 

Maybe you can somehow do without casting?

UPD:

I use VS 2015

  • @VladD - ah! forgot the most important thing - I use VS 2015 - Duracell
  • @VladD, why should? They were specifically introduced to auto-enrollment NOT to work. - Qwertiy
  • enum Colors : DWORD will work. enum class Colors : DWORD - will not. Feel the difference. - AnT
  • @Qwertiy: Ugh, I read the condition as void draw2DBox(Colors col) . - VladD
  • @VladD, looked at the story - it was then corrected)) - Qwertiy

3 answers 3

Does not work, should not work and will not work.

The idea of ​​introducing the enum class was the isolation of its values ​​and the appearance of a special type. Implicit casting is not available for it by design:

http://ideone.com/QQkRvZ

 enum class test : int { value = 0 }; int main() { test t = 0; return test::value; } 
 prog.cpp: In function 'int main()': prog.cpp:11:11: error: cannot convert 'int' to 'test' in initialization test t = 0; ^ prog.cpp:12:15: error: cannot convert 'test' to 'int' in return return test::value; ^ 

    The prohibition of implicit type casting is one consequence of the use of the class keyword in the definition of enum. The effect of this keyword is twofold: first, it declares the scoped enum (as opposed to the "normal" unscoped enum); secondly, it prohibits implicit coercion to an integral type.

    At the same time, in C ++ you can “base” enum on the integer type you have chosen without specifying the keyword class

     enum Colors : DWORD { RED = 0xFFFF0000, GREEN = 0xFF00FF00 ... }; 

    Here such an enum-type will be freely converted to an integer type implicitly, just as you wanted.

    However, by removing the word class , you will also make your enum unscoped, along with the restrictions on the casts. now the constants from inside enum will be visible in the global namespace as RED , GREEN , etc. Those. there is no need to use Colors::RED , Colors::GREEN . This is not very pleasant, but this is for you to judge whether this option suits you or not.

    (In this case, if you wish, you can continue to refer to these identifiers using qualified names — Colors::RED , Colors::GREEN , etc. This feature appeared in C ++ 11 in general for all enum-types. But in fact this is not more than syntactic sugar: for all enum types declared without the class keyword, the names of the constants fall into an encompassing namespace in a crowd.)

    If you still really want to use an implicit cast, but at the same time get a complete analogue of scoped enum, then the required behavior can be "emulated" using a wrapper from a struct

     struct Colors { enum : DWORD { RED = 0xFFFF0000, GREEN = 0xFF00FF00 ... }; }; 
    • I didn’t want that, because in the future there might be a problem with redefining any functions and names of other variables, I just wanted the enum class only without type conversion, it’s very convenient for me to call the enumeration class and select members in it - Duracell
    • @Duracell: Then just enclose your enum in a namespace or struct and everything. - AnT
    • I already did) thanks! - Duracell
    • IMHO, the namespace will be much more appropriate than the struct. - val
    • one
      @val: Where as ... The advantage of a struct as a wrapper is sometimes that a struct type can be passed to templates as an argument. Is this a possibility in this case - a separate question ... - AnT

    Maybe you can somehow do without casting?

    Can. But for this you have to either change the signature of the called function with

     void draw2DBox(DWORD col); 

    on

     void draw2DBox(Colors col); 

    Or add the appropriate overload that will do the necessary conversion inside:

     void draw2DBox(Colors col) { draw2DBox(static_cast<DWORD>(col)); }