I understand this is an old question, but it can help anyone looking for an easy way to keep multiple arrays / enums in sync.
In my case, I just wanted to check the compilation time to determine if my lists were out of sync, and since the sizeof operator is not evaluated before, the preprocessor does this, it was the easiest way to do this.
In a header file of some kind ...
enum ColorType { Red=0, Blue, Green, ColorType_Max };
In the same header or in another file, maybe ...
char const* ColorTypeNames[]= { "Red", "Blue", "Green" };
In the cpp file somewhere ...
const int s_ColorTypeCount = (int)ColorType_Max; const int s_ColorNameCount = (int)(sizeof(ColorTypeNames)/sizeof(char const*)); const int s_ColorErrorA = 1/(s_ColorTypeCount/s_ColorNameCount); const int s_ColorErrorB = 1/(s_ColorNameCount/s_ColorTypeCount);
Only if two sizes match will be equal to 1. s_ColorErrorA and s_ColorErrorB are equal to 1. Since the variables are constants, this will lead to a division of the compilation time by zero error when the two counter variables are different.
source share