we use VS 2008
there is a big enum, which is populated by many developers
this enum has a type __int64 (a Microsoft extension), and I want to make compiler complain about non unique values in enum.
if it was a usual enum I would do like this:
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 would have an error, if E1 has 2 same values
and it would have another error, if a developer forgot to add a new value to switch
but my enum has type __int64 (or long long)
and when I try do the same switch for E1 e1 it truncates values and complains on values, wich difference is either 0x100000000, or 0x200000000 ....
if I cast e1 to __int64, the compiler does not complain, if a developer forgets to add a new value to the switch (so the whole check function becomes useless)
the question: does someone know what I can do about it? or maybe VS 2008 (or C++) has another instrument to ensure enum : __int64 has only unique values?
don't explicitly specify values (other than the first one)
. – Doorntemplate< size_t >
free function definition parametrized with the enum with internal linkage. If a value is used twice it will violate ODR and you'll get a compilation error, if it works you get a ton of useless functions which hopefully your compiler will optimize away. It's very hacky but I think it would work in theory at least :P – Celenacelene