Is it legal to write recursive macros with __VA_OPT__
?
GCC and Clang appear to not replace recursively, but I'm not sure if it's intentional (as __VA_OPT__
support is very recent).
C++ spec (§19.3.1/3:
__VA_OPT__
):Otherwise, the replacement consists of the results of the expansion of content as the replacement list of the current function-like macro before rescanning and further replacement
Does the highlighted section above mean recursion isn't possible?
For example, to add a list of variadic macro parameters:
#define RECURSE(mFIRST, ...) + mFIRST __VA_OPT__(RECURSE(__VA_ARGS__))
int main(int argc, char const* const argv[])
{
return 1 RECURSE(2, 3, 4);
// Expected result: "return 1 + 2 + 3 + 4;"
}
Both GCC and Clang generate RECURSE
in their post-preprocessing.
// NOTE: Below is the output from g++ -E
int main(int argc, char const* const argv[])
{
return 1 + 2 RECURSE(3, 4);
}
NOTE: If this is possible, more complex variadic macros could be written fairly easy, such as concatenate, as you can create a custom __VA_NO_OPT__
from __VA_OPT__
, which lets you provide completely separate code for 1 and 2+ parameters.