+1  A: 

No, it isn't - macros cannot be recursive. And the macros you posted are not variadic, which means "having different numbers of parameters".

anon
Thanks, I removed the variadic tag.
Helltone
+7  A: 

It is possible, but you have to do some manual work and have an upper limit.

#define BUILD0(x) x[0]
#define BUILD1(x) BUILD0(x), x[1]
#define BUILD2(x) BUILD1(x), x[2]
#define BUILD3(x) BUILD2(x), x[3]
#define BUILD(x, i) BUILD##i(x)

And not that i should an integer literal, not a constant computed value.

BTW, the preprocessor is more powerful than what is usually though, but the use of that power is quite tricky. Boost provides a library which ease some things, included iteration. See Boost Preprocessor Library. There is another library for such things, but its name escapes me at the moment.

Edit: The boost preprocessor library use a similar technique. With additional tricks to solve some corner cases problems, share implementation macros between higher level facilities, work around compiler bugs, etc... the usual boost complexity which is normal in the context of a general purpose library but which at times prevents easy understanding of the implementation principles. Probably the most noticeable trick is to add an indirection level so that if the second parameter can be a macro, it is expanded. I.E with

#define BUILD_(x, i) BUILD##i(x)
#define BUILD(x, i) BUILD_(x, i)

one can make the call

#define FOO 42
BUILD(x, FOO)

which isn't possible with what I exposed.

AProgrammer
Thanks, its interesting how BUILD1..BUILD3 are almost identical... couldn't I have something like #define BUILD_(x, j) BUILD_(x, j-1), x[j]
Helltone
It wouldn't have a stopping condition, and macros can't be recursive to boot.
Blindy
+1 for a nice explanation!
Subtwo
"its name escapes me" Are you referring to Chaos PP?
Steve Jessop
@onebyone, right.
AProgrammer