Skip to main content
We are printing s, not FOO
Source Link
talbi
  • 210
  • 3
  • 11

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.

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(FOO __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.

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(s __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.

Source Link
talbi
  • 210
  • 3
  • 11

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(FOO __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.