4 ms·
You can assign integers to enum in C++, just like in C, but not to enum class. Use the latter when you need type safety, the former when you need to easily co
by Leherenn 7y ago
You can assign integers to enum in C++, just like in C, but not to enum class.
Use the latter when you need type safety, the former when you need to easily convert integer <-> enum, which apart from (de)serialisation is rare in my experience. And even in those cases, I personally prefer the explicit casts.
As far as I know, MSVC just implements the C subset of C++.
- jstimpfle 7y agoWhat I mean is this: > enum Foo { A, B, C }; > Foo x = 1; test.cc:2:9: error: invalid conversion from ‘int’ to ‘Foo’ [-fpermissive] Foo x = 1; I prefer not to use explicit casts because 1) using enum types is confusing to me 2) I frequently use values that are not in the enum - most often that's -1 as a way to indicate "missing". 3) I also like to iterate over all values from an enum using a plain for (int i = 0; i < NUM_FOOS; i++) without any fuss. > As far as I know, MSVC just implements the C subset of C++. There are things that work in MSVC C mode that do not work in C++ mode.