C++ Logo

std-proposals

Advanced search

Re: Enum Class Inheritance

From: Henry Miller <hank_at_[hidden]>
Date: Wed, 01 Sep 2021 13:51:13 -0500
There are two issues that I can think of: How large is the first enum, right now the system is allowed to choose the size based on the number of elements, there is no way to know that the inherited will fit. Second, right now enum values beyond the end of the enum are undefined, now you force them to be defined and there are some who want to use that undefined.

What I've been thinking of more of reserved ranges so that we can specify there are valid values that we need to pass through but otherwise are not defined in this scope.

 enum class CommonSystem {
     T1,
     T2,
     T3,
     SOME_KEYWORD SubSystemOne :100,
    SOME_KEYWORD SubSystemTwo :100,
    SOME_KEYWORD SubSystemThree 400:2,
 };
 
 enum class SubSystem : CommonSystem::SubSystemOne {
     U1, // starts with T3 + 1
     U2,
     U3,
 };
 enum class SecondSubSystem : CommonSystem::SubSystemTwo {
     U1, // starts with T3 + 100
     U2,
     U3,
    SOME_KEYWORD SubSubsystem: 5,
 };

enum class SubSubSystem :SecondSubSystem::SubSubsystem {
    U4, // T3_100+3
    U5,
}
 enum class ThirdSubSystem : CommonSystem::SubSystemThree {
     U1, // starts with T3 + 400
     U2,
     U3, // compile error, only 2 elements allowed in this range
 };

-- 
  Henry Miller
  hank_at_[hidden]
On Wed, Sep 1, 2021, at 13:08, Murat Hepeyiler via Std-Proposals 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 13:51:42