Variadic recursive preprocessor macros - is it possible? -


I have participated in a little theoretical problem I am creating a set of macros in a piece of code such as

  #define MAX_OF_2 (a, b) (a) & gt; (B)? (A): (B) MAX7_3 (a, b, c) MAX_OF_2 (MAX_OF_2 (a, b), c) # defined MAX_OF_4 (a, b, c, d) MAX_OF_2 (MAX_OF_3 (a, b, c) D) ... etc at least MAX_OF_8  

What I would like to do is replace them with something like this:

  / * Base case # 1 , Single input * / # defined MAX_OF_N (x) (x) / * base case # 2, two input * / # defined MAX_OF_N (x, y) (x) & gt; (why)? (X): (wi) / * recurring definition, arbitrary number of inputs * / # defined MAX_OF_N (x, ...) MAX_OF_N (x, MAX_OF_N (__VA_ARGS_))  

. This is not a valid preprocessor code.

This special case should be resolved by using a function instead of preprocessor macro Ignoring, is it possible to define a variadic MAX_OF_N () macro?

For just clarity, the end result should be a single macro which considers the number of criteria and evaluates the biggest of them. I feel strange that this should be possible, but I am not seeing how.

No, because the preprocessor only takes a "swipe" on the file. There is no way to define macros again.

I noticed that only such a code was not variadic, but the default values ​​were to be used by the user:

  x = MAX_OF_8 ( A, b, -1, -1, -1, -1, -1, -1)  

The values ​​assumed to all were non-negatives.

Inline functions should give you at least equal to C ++. As you say, it is probably printf () .

is better for function with variable arguments.

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -