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

Can I test the value of a preprocessor directive?

I have a preprocessor directive that I do not set, so I cannot change it, it is either true or false.

Normally I would have done :

#ifdef DIRECTIVE
// code
#endif

But this will always run, since DIRECTIVE is always defined.

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

Is there a way that I can do basically the equivalent of:

#if DIRECTIVE
#endif

I guess I could do

bool DirectiveValue = DIRECTIVE;
if (DirectiveValue){

}

But I was really hoping the second code block was possible in some way.

Thanks for any insight!

>Solution :

The preprocessor has an #if statement, so you can do things like: #if DIRECTIVE, which (just like in C normally) tests as false if the value of he expression is zero, and true if the value of the expression is non-zero.

Although it’s not clear whether it provides a real advantage for you, there’s also a kind of intermediate form: if constexpr (DIRECTIVE), which is evaluated at compile time, no run time, so it resembles an #if to some degree in that way–but still using normal C++ syntax, and integrated with the language in general, instead of being its own little thing with slightly different rules than the rest of the compiler.

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