I am currently trying to use two headers defining (partially) the same constants. Typically the constants should be the same but my plan is to add a check to be absolutely sure. The problem can be described by the following minimal example:
#include "header1.h" //Defines MY_CONSTANT 99
#include "header2.h" //Also defines MY_CONSTANT 99
Compiling is possible but the warning
warning: "MY_CONSTANT" redefined
should not be ignored since I want to be sure both headers are defining the same value.
My plan was to undef the define before including the second header with checking the values are equal. I've tried to save the first value of the constant as C++ const and added a static assertion but it seems like the compiler is not doing what I need:
#include "header1.h" //Defines MY_CONSTANT 99
const uint64_t MY_CONSTANT_OLD = MY_CONSTANT;
#undef MY_CONSTANT
#include "header2.h" //Defines again MY_CONSTANT 99
static_assert(MY_CONSTANT_OLD == MY_CONSTANT, "Redefined with other value"); //Check same definition
MY_CONSTANT’ was not declared in this scope
How could I solve the problem of redefining the constants with getting a compiler error if the values are not equal?