Suppose I have a macro
FOO(a, b)
and I want to invoke this macro with a certain (fixed) set of possible values for a
; and a certain fixed set of values for b
. Thus, if my set of b values is bar
and baz
and my set of b values is fiz
and bang
, I want:
FOO(bar, fiz)
FOO(bar, bang)
FOO(baz, fiz)
FOO(baz, bang)
separated either by new lines or by semicolons, or both - that's a minor issue so let's ignore it; the exact order of the 4 invocations is also not important.
Now, if I only had one 'dimension' of parameters, I could use William Swanson's mechanism (as described here on the site; and there's even a github repository for it), and write
MAP(SINGLE_PARAMETER_MACRO, bar, baz, quux)
to obtain
SINGLE_PARAMETER_MACRO(bar)
SINGLE_PARAMETER_MACRO(baz)
SINGLE_PARAMETER_MACRO(quux)
but the thing is, I have two dimensions; and it seems to be impossible/tricky to separate your __VA_ARGS__
into two different sets and iterate the two-dimensional space of pairs of elements from these sets.
Can this be (reasonably) done?
Notes:
- I'm interested in a C-preprocessor-based solution, but if you have something that only works in C++ for some strange reason, that's ok too.
- Solution must be compile-time only, and valid mostly everywhere in your C/C++ translation unit; specifically, within class definitions and at file scope.
- You might be guessing this is an XY problem, and you're right; but please don't pick at my motivation since both X and Y are interesting IMHO.
- If you can maintain a lexicographic order of the macro invocations, that would be nice.