简体   繁体   中英

Can't initialize an enum with a value from its underlying type

enum class Color { red, blue, green };
Color x = Color{5}; 

The second statement gives me error (main.cpp:20:23: error: cannot convert 'int' to 'Color' in initialization) . I was going through the c++ book it says it's OK to initialize the way in the second statement. I'm confused whether can we use int in initializing the enum type (Color here).

Initialization of scope enumerations with an integer without cast is a C++17 feature, see cppreference . You have to enable C++17 in your compile step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM