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
You might consider this cheating, since it is not recursive and it doesn't do the work in the preprocessor. And it uses a GCC extension. And it only works for one type. It is, however, a variadic MAX_OF_N macro:
#include
#include
#define MAX_OF_N(...) ({\
int ra[] = { __VA_ARGS__ }; \
*std::max_element(&ra[0], &ra[sizeof(ra)/sizeof(int)]); \
})
int main() {
int i = 12;
std::cout << MAX_OF_N(1,3,i,6);
}
Oh yes, and because of the potential variable expression in the initializer list, I don't think that an equivalent of this (using its own function to avoid std::max_element) would work in C89. But I'm not sure variadic macros are in C89 either.
Here's something that I think gets around the "only one type" restriction. It's getting a bit hairy, though:
#include
#include
#define MAX_OF_N(x, ...) ({\
typeof(x) ra[] = { (x), __VA_ARGS__ }; \
*std::max_element(&ra[0], &ra[sizeof(ra)/sizeof(ra[0])]); \
})
int main() {
int i = 12;
std::cout << MAX_OF_N(i+1,1,3,6,i);
}