C++ Logo

std-proposals

Advanced search

Re: Enum Class Inheritance

From: Murat Hepeyiler <murathepeyiler_at_[hidden]>
Date: Thu, 2 Sep 2021 21:47:52 +0300
To Henry Miller,
You have pointed out there will be a problem with fit the value. Actually
good point of it, I think child class underlying type can be different than
parent class unless there is no narrowing conversion. I mean;

enum class System : int {
     // ...
     END, // integer limit
};

enum class SubSystem : long int, public System {
};

There is no narrowing conversion, thus System values also fit the SubSystem
enum. I do not it is just an idea.

To be honest, I could not your reserved range idea. Could explain more?

To Arthur O'Dwyer,
Actually, I want to compile bind to base reference code. I think it can be
a valid state for many scenarios.

Also, you have asked that what will be the result for the below case. I
think this statement needs to be invalid. enum values can be like this;


LION = 0
TIGER = 1
OTTER = 2
WEASEL = 3
BEAR = 5

> enum Cat { LION, TIGER };
> enum Mustelid { OTTER, WEASEL };
> enum Animal : Cat, Mustelid { BEAR }; // Does (Animal::LION ==
>Animal::OTTER)? Why or why not?

Enumeration starts with the first parent class then goes on like this. Only
if these values are hardcoded, then this statement can be true.

To Ivan Matek,
Thank you for your suggestion. But I cannot create an object, I have
limited memory area. I have solved my problem like this,

class Base {
    enum {
       B1,
       B2,
   };
};

class Derived : public Base {
    enum {
       D1,
       D2,
   };
};

std::cout << Derived::B1; // valid code

Actually, this is what I suggested. But the problem is (Base::B1 ==
Derived::D1) returns true. I think D1 starts with B2+1, that will be a more
viable solution. This is kind of a workaround, but enum class inheritance
could be a more elegant solution. That is why I would like to discuss it.

Best Regards,

Ivan Matek <libbooze_at_[hidden]>, 2 Eyl 2021 Per, 18:16 tarihinde şunu yazdı:

> Hi, would using a std::variant help your usecase?
> You would be defining a struct for every enum, and concatenation of types
> is not trivial, but it may work.
> https://godbolt.org/z/6cqqeE6WM
>
> On Wed, Sep 1, 2021 at 8:09 PM Murat Hepeyiler via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>> I have faced up a problem that I need to extend the enum class for each
>> system.
>>
>

-- 
Murat Hepeyiler
5G Software Engineer & C++ Developer
www.linkedin.com/in/murathepeyiler

Received on 2021-09-02 13:48:31