Gankidi Vasu
Gankidi Vasu

Reputation: 61

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).

Upvotes: 1

Views: 870

Answers (1)

Thomas Sablik
Thomas Sablik

Reputation: 16454

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.

Upvotes: 3

Related Questions