The issue of redefining inheritance

Given this popular solution for "enum inheritance" (probably dubbed "extension"), would it be possible to raise a compiler error if I override the identifier of the base class enumerator in the derived class?

Example:

struct Enum
{
    enum
    {
        One = 1,
        Two,
        _last
    };
};

struct EnumDeriv : Enum
{
    enum
    {
        Three = Enum::_last,
        Four,
        One  //<--- this compiles but I'd like to get a compiler error instead 
    };
};

Live demo

What happens is that EnumDeriv::Onemasks (is that the right term?) Definition Enum::One. So, EnumDeriv::Onenow it will display the "unexpected" integer value 5.

This can create some hard-to-debug errors if we exit Enumand inadvertently redefine some of its enumerator identifiers.

, "" "enum inheritance", , .

+4

Source: https://habr.com/ru/post/1672879/


All Articles