Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining preprocessor macros and variables

Tags:

c++

macros

In my C++ code I have to execute certain code under two conditions: because of a preprocessor macro OR a boolean variable check. For example:

bool done=false;
#ifdef _DEBUG
executeDebugCode();
done=true;
#endif

if (inputParam && !done)
    executeDebugCode();

Is there a way to write the above code in a more elegant way, without repeating the executeDebugCode() function call two times?

EDIT: the executeDebugCode() function should be executed once, and if one of the two condition is met. For example a function that should be executed in DEBUG mode only, that could be set by preprocessor macro or command line parameter.

like image 818
ABCplus Avatar asked Jun 29 '26 14:06

ABCplus


1 Answers

Assuming that you want to execute this code only once, if at least one of these conditions is true:

    if ( inputParam
#ifdef DEBUG
        || true
#endif
       )
{
    executeDebugCode();
}
like image 93
Alex F Avatar answered Jul 02 '26 05:07

Alex F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!