问题
Somewhere in my code, I have preprocessor definition
#define ZOOM_FACTOR 1
In another place I have
#ifdef ZOOM_FACTOR
#if (ZOOM_FACTOR == 1)
#define FONT_SIZE 8
#else
#define FONT_SIZE 12
#endif
#else
#define FONT_SIZE 8
#endif
The problem is when I change ZOOM_FACTOR
value to floating point
value, for example 1.5
, I'm getting compile error C1017: invalid integer constant expression
.
Does anyone know why am I getting this error and is there any way to make a comparison between integer
and floating point number
within preprocessor directive?
回答1:
The error is because the language does not permit it.
As per the C++ standard, [cpp.cond]/1:
The expression that controls conditional inclusion shall be an integral constant expression.
Instead of defining ZOOM_FACTOR
as floating point value 1.5
, why not define it as a multiple of such value. For example, multiply with a constant such as 2
and then make your comparisons.
来源:https://stackoverflow.com/questions/56144648/preprocessor-invalid-integer-constant-expression-comparing-int-to-double