Variadic recursive preprocessor macros - is it possible?

前端 未结 6 901
一生所求
一生所求 2020-11-28 07:11

I\'ve run into a little theoretical problem. In a piece of code I\'m maintaining there\'s a set of macros like

#define MAX_OF_2(a, b)       (a) > (b) ? (a         


        
6条回答
  •  不知归路
    2020-11-28 08:00

    No, because the preprocessor only takes one "swipe" at the file. There's no way to get it to recursively define macros.

    The only code that I've seen do something like this was not variadic, but used default values the user had to pass:

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

    assuming all values were non-negative.

    Inline functions should give you the same for C++ at least. As you state, it's probably better left to a function with variable arguments similar to printf().

提交回复
热议问题