C ++: how to check that an enum has only unique values

we use VS 2008

there is a large listing that is populated by many developers

this enum is of type __int64 (Microsoft extension), and I want the compiler to complain about non-unique values ​​in the enum.

If this were a normal enumeration, I would do the following:

enum E1
{
    E11 = 0x01F00,
    E12 = 0x01F00,
    E13
};
#pragma warning(push)
#pragma warning(error: 4061)
#pragma warning(error: 4062)
void F(E1 e1)
{
    switch (e1)
    {
    case E11:
    case E12:
    case E13:
        return;
    }
}
#pragma warning(pop)

and the function F will have an error if E1 has two identical values

and another error would occur if the developer forgot to add a new value to switch

but my enum is of type __int64 (or long long)

and when I try to make the same switch for E1 e1, it cuts off the values ​​and complains about values ​​whose difference is either 0x100000000 or 0x200000000 ....

e1 __int64, , ( )

: - , ? VS VS 2008 ( ++) : __int64 ?

+3
2

, () . , . , , .

enum Bit_Index
{
    FLAG1_INDEX,
    FLAG2_INDEX,
    FLAG_FANCY_INDEX,
    LAST_INDEX
};

#define DECLARE_BIT_VALUE(att) att##_VALUE = 1ULL << att##_INDEX
enum Bit_Value
{
    DECLARE_BIT_VALUE(FLAG1),
    DECLARE_BIT_VALUE(FLAG2),
    DECLARE_BIT_VALUE(FLAG_FANCY),

    // Declared NOT using the macro so we can static assert that we didn't forget
    // to add new values to this enum.
    LAST_BIT   // Mainly a placeholder to prevent constantly having to add new commas with new ids.
};
#undef DECLARE_BIT_VALUE

static_assert, , :

// Make sure to the best of our abilities that we didn't mismatch the index/bit enums.
BOOST_STATIC_ASSERT((LAST_BIT - 1) == (1U << (LAST_INDEX - 1)));
+2

- , .

. . , . .

0

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


All Articles