The problem that I have is that I have two enums in two different files which should have the same sets of constants (two different processes are generated by the two files along with other files). I want the enums to be in sync i.e. when some one adds a new value to enum x and forgets to update the other enum, I want to throw a compilation error. Is that possible?
Why aren't you putting this declaration in a single header file and then including it in the two locations it is needed?
Have each of the enums end with a known enum, such as LAST_ENUM_1 and LAST_ENUM_2. Use a #if in a file that has access to both header files to compare.
#if LAST_ENUM_1 != LAST_ENUM_2
#error Enums have different sizes
#endif
The names used in enums need to be unambiguous, so you'll have a problem: either the compiler has access to both definitions, then the enums can't be identical because of the name problem or the compiler has access to only one definition a time, then it has nothing to check against.
Even the number of elements (as proposed by Robert) can't be checked at compile time (the preprocesser doesn't know anything about the enums). If you really can't have one common header file, the easiest thing to do would be a runtime check at the start of your application.
use like
enum EMyEnum
{
JOE = 0,
BLACK = 1,
WHITE = 2,
END_OF_ENUM = 3
}
if you use like that format, maybe you can handle everything easier
I really like the other answers better than the one I will sugest now...
If all other solutions don't work for you, write a simple perl script that checks if they are the same and make sure that the perl script is called from your makefile. It will solve your problem, but try to avoid if you can.
Since the compiler looks at one source file (translation unit, TU) at a time, there is no way for it to complain about a mismatch between the current TU and some other TU that it is not looking at.
You need to fix things so that you have one copy of the definition of the enum that is used by both programs - so it belongs in a header that is included by both. Pretty much anything else is too error prone for comfort.