Hi,
C++11 strong enums ensure that we can not interchange 2 such enum classes as we can not interchange two independent structs.
However we can static_cast two enum classes
EC2 ec = static_cast<EC2>(e);
EC2 ec = EC2(e);
while we can not static cast two independent structs
S2 ec = static_cast<S2>(s2);
See http://melpon.org/wandbox/permlink/ne5oPVPMkkrswIxy
I'm asking this because the Boost.ScopedEnum emulation for C++98
of a C++11 enum class uses a struct to represent the scope and so
the cast between two emulated scoped enums is not supported in
C++98. I find that this is a good thing.
I'm wondering if the support of the cast between two C++11 enum
classes was intentional or not.
Do you find it a desirable feature?
Note that if the static_cast between two enum classes was not
supported we could always do it using two static casts
EC2 ec = static_cast<EC2>(static_cast<int>(e));
Hi,
C++11 strong enums ensure that we can not interchange 2 such enum classes as we can not interchange two independent structs.
On Monday, January 9, 2017 at 11:45:33 AM UTC-5, Vicente J. Botet Escriba wrote:Hi,
C++11 strong enums ensure that we can not interchange 2 such enum classes as we can not interchange two independent structs.
No, it does not.
It ensures that we cannot implicitly convert integers to strong enums or strong enums to integers. That is the purpose of the feature: to prevent inappropriate implicit conversions.
Strongly typed `enum`s in C++ are still integers, even if they're not implicitly convertible from/to integers. Explicit conversions are therefore able to convert them from/to integer types. And since enums are integers, you can explicitly convert them from/to enum types.
Le 09/01/2017 à 18:31, Nicol Bolas a écrit :
On Monday, January 9, 2017 at 11:45:33 AM UTC-5, Vicente J. Botet Escriba wrote:Hi,
C++11 strong enums ensure that we can not interchange 2 such enum classes as we can not interchange two independent structs.
I meant implicitly here of course. the next paragraph continue with explicit conversions. Sorry for the imprecision.No, it does not.
It ensures that we cannot implicitly convert integers to strong enums or strong enums to integers. That is the purpose of the feature: to prevent inappropriate implicit conversions.
Strongly typed `enum`s in C++ are still integers, even if they're not implicitly convertible from/to integers. Explicit conversions are therefore able to convert them from/to integer types. And since enums are integers, you can explicitly convert them from/to enum types.
My question is if you believe this behavior is desirable.
I don't. Even if enums are integer types they are strong types.
I would expect them to be able to accept an explicit conversion to the underlying type only and not other integer types, and in particular not to other strong enums (enum classes). If I want to have this behavior I would need to use a struct similar to the one in Boost.ScopedEnums, wrapping the underlying type :(
I know it is too late to change it. I just want to know if this was intentional (considered during the design) or if it is there by omission.
Do you find it a desirable feature?
Note that if the static_cast between two enum classes was not supported we could always do it using two static casts
EC2 ec = static_cast<EC2>(static_cast<int>(e));
On segunda-feira, 9 de janeiro de 2017 23:45:09 PST Vicente J. Botet Escriba
Specifically to other integer types besides the underlying one, it would be
simply impossible to do it, as those conversions are implicit already. That
is, given
enum E : int {};
E e {};
int i = e;
ushort u = i; // no error
Then this should be allowed without an error:
ushort u = e;