Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Наследование enum class #583

Open
mcroitor opened this issue Nov 23, 2023 · 0 comments
Open

Наследование enum class #583

mcroitor opened this issue Nov 23, 2023 · 0 comments

Comments

@mcroitor
Copy link

Не хватает наследования enum class.

На данный момент возможен следующий код:

#include <iostream>

enum class pin_type: uint8_t
{
    d0, d1, d2, a0 = 10, a1,
};
enum class digital_pin_type: uint8_t {
    d0, d1, d2,
};
enum class analog_pin_type: uint8_t {
    a0 = 10, a1,
};

int main()
{
    pin_type pin = static_cast<pin_type>(analog_pin_type::a0); // ok
    digital_pin_type dpin = static_cast<digital_pin_type>(pin_type::d1); // ok
    analog_pin_type apin = static_cast<analog_pin_type>(pin_type::d1); // logic error, but works

    std::cout << (size_t)pin << " " << (size_t)dpin << " " << (size_t)apin;
    return 0;
}

Однако в нем отслеживание на правильность связи между перечислениями ложится на разработчика. Поэтому хочется иметь возможность указать зависимость (наследование) одного перечисления от другого. Ожидаемый код:

#include <iostream>

enum class pin_type: uint8_t
{
    d0, d1, d2, a0 = 10, a1,
};
enum class digital_pin_type: pin_type {
    d0 = pin_type:d0, d1, d2,
};
enum class analog_pin_type: pin_type {
    a0 = pin_type:a0, a1,
};

int main()
{
    pin_type pin = analog_pin_type::a0; // ok
    digital_pin_type dpin = pin_type::d1; // ok
    analog_pin_type apin = pin_type::d1; // compile error

    std::cout << (size_t)pin << " " << (size_t)dpin << " " << (size_t)apin;
    return 0;
}

Ожидаемые свойства:

  • дочернее перечисление содержит некоторый набор значений из базового перечисления
  • дочернее перечисление может содержать псевдонимы значений
  • дочернее перечисление не содержит других значений, на совместимых с базовым перечислением
  • неявное приведение типов, дочернего к базовому
  • при приведении типов от базового к дочернему выдача ошибки / предупреждения
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant