Is it possible to have a compile-time check on array values?
example:
typedef enum
{
dummy0 = 0,
dummy1,
dummy2
} eDummyEnum;
typedef struct
{
eDummyEnum name;
int value;
} sDummyStruct;
const sDummyStruct array[]=
{
{dummy0, 3},
{dummy1, 5},
{dummy2, 6}
}
Is there any possibility to check if array[dummy1].name == dummy1
at compilation time?
Something along these lines, perhaps:
constexpr sDummyStruct array[]=
{
{dummy0, 3},
{dummy1, 5},
{dummy2, 6}
};
constexpr bool checkArray(int index) {
return (index >= sizeof(array)/sizeof(array[0])) ||
(array[index].name == index && checkArray(index + 1));
}
static_assert(checkArray(0), "bad array");