If you are using gcc 8+
, clang 6+
or MSVC 2019
(source), then you can also use the (newer) __VA_OPT__
macro, which conditionally expands if __VA_ARGS__
is non-empty.
So, we can convert the two FOO
and BAR
macros into one:
#define FOO(s, ...) printf(FOOs __VA_OPT__(,) __VA_ARGS__)
and so, FOO("hello!")
will expand to printf("hello!")
, and FOO("x = %d", 5)
will expand to printf("x = %d", 5)
.
This is a relatively new feature (introduced in C++2a) so your compiler might not support it yet.