Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor "invalid integer constant expression" comparing int to double

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?

like image 637
arsdever Avatar asked May 15 '19 08:05

arsdever


Video Answer


1 Answers

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.

like image 57
P.W Avatar answered Oct 14 '22 21:10

P.W