Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One line #if statement

I have a config file with a lot of defines that are used to include modules during the complie time. In addition this means that I have to check very oft inside of the code for defines. Every check requires 3 lines is it possible to do this in one line.

#if FUNC_ENABLED
function_optional_call();
#endif

I'm looking for something like.

CALL(function_optional_call(),FUNC_ENABLED);

Is there a makro possible doing this or do I have to use the 3 line expression. I know its possbile to define a makro for every makro.

Skip function call if not defined

But is it possible to generate a generall makro.

like image 653
Schafwolle Avatar asked Oct 29 '25 02:10

Schafwolle


1 Answers

#if FUNC_ENABLED
function_optional_call();
#endif

is a canonical way of writing a compiler switch. It is clear, it is readable, every programmer understand what it does.

Now this code:

CALL(function_optional_call(),FUNC_ENABLED);

is completely mysterious. It is not clear what it is for, why there are macros... it doesn't even look like valid C.

Thus the first form is far superior, to the latter, which is just ugly, bad practice. So don't try to re-write the former into the latter.


If you worry about repeating the same line over and over, it would rather seem that the problem is an incorrect placement of the compiler switch. Put it inside the function instead. Or if you can't modify the function, make a wrapper.

like image 149
Lundin Avatar answered Oct 30 '25 17:10

Lundin



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!