Is it possible to write a C macro that returns the number of its arguments?
I want something that does:
foo(1) -> 1
foo(cat, dog) -> 2
foo(red, green, blue) -> 3
Even better if this macro can be defined in such a way that it works with ## so that
foo(1) -> bar1(1)
foo(cat, dog) -> bar2(cat, dog)
foo(red, green, blue) -> car3(red, green, blue)
Thanks!
EDIT: I really want a macro, not a function. Suggestions to use functions will be downvoted.
It can be done – the mechanism was explained in the comp.std.c newsgroup in January 2006. There was another question about this recently on SO 2124339.
I stashed the code away, just in case…
It works fine as long as there are no more than 64 arguments. Here’s the test code I used: