Home > Back-end >  Macros #if directive with non integer definition
Macros #if directive with non integer definition

Time:10-06

I'm having a 20yo legacy code (pure C) which defines:

#define X float

before first function and before another function:

#undefine X
#define X double

I'm writing a code which is supposed to work with both definitions (and it'll be copied into both functions automatically). But it's impossible to make such code in a few cases. For these cases I need to detect if my X is float or double and use #if #else #endif.

The #if (X == float) is resolved as 0 == 0 and won't work.
In theory I can grab into legacy code and modify these definition to make my life easier, but I wonder if there is any macro magic which would allow me to workaround this without touching the legacy? Something with conversion X to string may be? Any ideas?

CodePudding user response:

Concatenate with prefix that expands to something you can control.

#define PREFIX_float   0
#define PREFIX_double  1
#define CONCAT(a, b)   a##b
#define XCONCAT(a, b)  CONCAT(a, b)

#if XCONCAT(PREFIX_, X) == PREFIX_float
  • Related