Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Macros #if directive with non integer definition

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading