ARTICLE AD BOX
According to cppreference, it is allowed to define an unscoped enum value by using prior values of the same enum, e.g. like this:
enum MyEnum_U { u_foo = 2, u_bar = 5, u_baz = u_foo + u_bar + 2 };The prior case is pretty clear to me, but I am unsure whether the same is also allowed for a scoped enum class as I found no examples for this case. Something like this:
enum class MyEnum_S : int { foo = 2, bar = 5, baz = foo + bar + 2 };I am not sure if this is a defined or undefined behaviour as scoped enum values should not be implicitly convertible to their native type. For example if I try to add those enum values outside of the definition scope I get the expected compiler errors that there is no defined operator+.
I can also write the last value definition like
baz = static_cast<int>(foo) + static_cast<int>(bar) + 2
or even as
baz = static_cast<int>(MyEnum_S::foo) + static_cast<int>(MyEnum_S::bar) + 2
Are all above cases well-defined C++ behaviour or does something depend on undefined behaviour and thus should be omitted?
I tested with the latest GCC and MSVC versions and they accept all variations.
Note: If it matters, I try to write compiler independent code which is backward compatible to C++14 but answers which depend on later C++ standards are also welcome.
