Date: Wed, 1 Sep 2021 15:04:15 -0400
To expand on Henry Miller's response: Consider what "inheritance" means in
C++. Would you want the following to compile?
SubSystem ss = SubSystem::U1;
CommonSystem cs = ss; // With ordinary inheritance, this would
"slice." What does that mean for enums?
CommonSystem& r = ss; // With ordinary inheritance, a base reference
can bind to a derived object. What does that mean for enums?
I'm quite confident that we don't want either of the above lines to compile.
Vice versa, consider:
CommonSystem cs = CommonSystem::T1;
SubSystem ss = cs; // With ordinary inheritance, this would not
compile.
SubSystem& r = cs; // With ordinary inheritance, this would not
compile.
Arguably, the initialization of `ss` should Just Work. Should the reference
binding to `r` also be required to work?
Henry points out that you have an issue with sizeof. I think it would be
perfectly natural to mandate that sizeof(SubSystem) ==
sizeof(CommonSystem), and if the compiler sees that that would cause
overflow, then the program is simply ill-formed, easy peasy.
Notice that if we follow this rule, then adding too many extra enumerators
to the "base" enum can cause the "derived" enum to become ill-formed. This
is fine. Adding members to a base *class* can equally well cause a derived
*class* to become ill-formed.
Normal inheritance works like this:
struct Animal {};
struct Cat : Animal {};
But "enum inheritance" would have to work "backwards," like this:
enum Cat { LION, TIGER };
enum Animal : Cat { BEAR };
This is perhaps a strong enough reason to think that "inheritance" is not
the right metaphor here.
Also notice that "multiple inheritance" has a (sometimes strained) meaning
with classes, but I don't know what it would mean for enums.
enum Cat { LION, TIGER };
enum Mustelid { OTTER, WEASEL };
enum Animal : Cat, Mustelid { BEAR }; // Does (Animal::LION ==
Animal::OTTER)? Why or why not?
–Arthur
On Wed, Sep 1, 2021 at 2:10 PM Murat Hepeyiler via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hi, why is enum class inheritance not allowed? I have faced up a problem
> that I need to extend the enum class for each system. However, there is no
> good tool to do it in C++ as far as I know.
> Let me explain more briefly, assume that there are more than one
> subsystems that are using the same common base system. The common system
> contains such as memory, time management controllers and these controllers
> consist of two-part. One of them is a common one and the other one is
> system-dependent. I want to keep their name and id in the enum class.
> Although I can extend functionalities with inheritance, I cannot extend
> their enums. To do this, I have two options. The first one is putting the
> all enums same enum class or duplicate names for each sub-system. As you
> guess, these solutions are not good solutions to remove dependencies or
> duplications. As a reason for it, what about adding the inheritance for
> enum classes?
>
> Maybe it can be;
>
> enum class CommonSystem {
> T1,
> T2,
> T3,
> };
>
> enum class SubSystem : CommonSystem {
> U1, // starts with T3 + 1
> U2,
> U3,
> };
>
> enum class <class name> : <underlying type>, <parent class...> {
> };
>
> Best Regards,
> --
> Murat Hepeyiler
> 5G Software Engineer & C++ Developer
> www.linkedin.com/in/murathepeyiler
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
C++. Would you want the following to compile?
SubSystem ss = SubSystem::U1;
CommonSystem cs = ss; // With ordinary inheritance, this would
"slice." What does that mean for enums?
CommonSystem& r = ss; // With ordinary inheritance, a base reference
can bind to a derived object. What does that mean for enums?
I'm quite confident that we don't want either of the above lines to compile.
Vice versa, consider:
CommonSystem cs = CommonSystem::T1;
SubSystem ss = cs; // With ordinary inheritance, this would not
compile.
SubSystem& r = cs; // With ordinary inheritance, this would not
compile.
Arguably, the initialization of `ss` should Just Work. Should the reference
binding to `r` also be required to work?
Henry points out that you have an issue with sizeof. I think it would be
perfectly natural to mandate that sizeof(SubSystem) ==
sizeof(CommonSystem), and if the compiler sees that that would cause
overflow, then the program is simply ill-formed, easy peasy.
Notice that if we follow this rule, then adding too many extra enumerators
to the "base" enum can cause the "derived" enum to become ill-formed. This
is fine. Adding members to a base *class* can equally well cause a derived
*class* to become ill-formed.
Normal inheritance works like this:
struct Animal {};
struct Cat : Animal {};
But "enum inheritance" would have to work "backwards," like this:
enum Cat { LION, TIGER };
enum Animal : Cat { BEAR };
This is perhaps a strong enough reason to think that "inheritance" is not
the right metaphor here.
Also notice that "multiple inheritance" has a (sometimes strained) meaning
with classes, but I don't know what it would mean for enums.
enum Cat { LION, TIGER };
enum Mustelid { OTTER, WEASEL };
enum Animal : Cat, Mustelid { BEAR }; // Does (Animal::LION ==
Animal::OTTER)? Why or why not?
–Arthur
On Wed, Sep 1, 2021 at 2:10 PM Murat Hepeyiler via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hi, why is enum class inheritance not allowed? I have faced up a problem
> that I need to extend the enum class for each system. However, there is no
> good tool to do it in C++ as far as I know.
> Let me explain more briefly, assume that there are more than one
> subsystems that are using the same common base system. The common system
> contains such as memory, time management controllers and these controllers
> consist of two-part. One of them is a common one and the other one is
> system-dependent. I want to keep their name and id in the enum class.
> Although I can extend functionalities with inheritance, I cannot extend
> their enums. To do this, I have two options. The first one is putting the
> all enums same enum class or duplicate names for each sub-system. As you
> guess, these solutions are not good solutions to remove dependencies or
> duplications. As a reason for it, what about adding the inheritance for
> enum classes?
>
> Maybe it can be;
>
> enum class CommonSystem {
> T1,
> T2,
> T3,
> };
>
> enum class SubSystem : CommonSystem {
> U1, // starts with T3 + 1
> U2,
> U3,
> };
>
> enum class <class name> : <underlying type>, <parent class...> {
> };
>
> Best Regards,
> --
> Murat Hepeyiler
> 5G Software Engineer & C++ Developer
> www.linkedin.com/in/murathepeyiler
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2021-09-01 14:04:32