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

Is #define X defined(Y) a valid C/C++ expression?

I read somewhere that

#define X defined(Y)

was an invalid expression, but it works (?)

Here an example

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

#define WIN_PLAT defined(_WIN32)

#if WIN_PLAT
#    undef  WIN_PLAT
#    define WIN_PLAT 1
#else
#    undef  WIN_PLAT
#    define WIN_PLAT 0
#endif

Thanks y’all.

Edit: fixed ‘duplicate definitions’ warning in the else branch.

>Solution :

Macros are text replacements, as long as you replace defined(Y) back into a preprocessor instruction it will work. It will however not work if you try to use it in C++ code:

cout << X; // error: undefined symbol defined
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