Enumerations and an error in tour of C++

Hi,

I'm reading Tour of C++ 2nd edition. In section 2.5 Enumerations, it says:

enum class Color {red, green, blue};

Catching attempted conversions to an enum is a good defense against errors, but
often we want to initialize an enum with a value from its underlying type (by
default, that’s int), so that’s allowed, as is explicit conversion from the
underlying type:


1
2
Color x = Color{5}; // OK, but verbose
Color y {6}; // also OK 


But in effect, I get two errors for these two statements!

1
2
3
enum class Color {red, green, blue};
 Color y { 2 }; // Error a value of type "int" cannot be used to initialize an entity of type "Color"	
 Color x = Color{ 1 }; // Error a value of type "int" cannot be used to initialize an entity of type "Color" 


I also looked at the Errata of the book but didn't see it mentioned there!
Last edited on
> But in effect, I get two errors for these two statements!

Initialising a scoped enumeration this way was not allowed prior to C++17.
https://en.cppreference.com/w/cpp/language/enum#Scoped%20enumerations

Snippet: http://coliru.stacked-crooked.com/a/f46ada03dd5c5bb4
OK, thank you.

But my compiler is VS community 2017 up-to-date. Why should it not support C++17?
Works for me in VS 2017 CE.
What did you set for C++ Language Standard in Properties->C++->Language-> C++ Language Standard ?
I don't think VS 2017 enables all C++17 features by default. You might have to use the /std:c++17 (or /std:c++latest) compiler flag to turn this feature on.
Last edited on
Thank you both very much.
That field was blank but I chose "ISO C++ Latest Draft Standard (/std:c++latest)" and now works. :)
Topic archived. No new replies allowed.