As an extension to the standard, the ## preprocessor operator can be used to "eat" a trailing comma for __VA_ARGS__. This is supported in all my compilers of interest (GCC, clang, MSVC).
However, their handling of it is not the same. In particular, MSVC breaks in a way I don't understand (example below).
How can I write the MYLOG macro so that it works in all platforms?
Example program (live link)
#include<cstdio>
#define LOG_HELPER(...) printf(__VA_ARGS__)
#define MYLOG(fmt, ...) LOG_HELPER("mylog: " fmt, ##__VA_ARGS__)
#define MYPRINTF(fmt, ...) printf( "printf: " fmt, ##__VA_ARGS__)
int main() {
// Works in all cases I care about (GCC, clang, MSVC)
MYPRINTF("foo\n");
// Works in GCC+clang, but not MSVC!
MYLOG("foo\n");
return 0;
}
The error MSVC gives is: error C2059: syntax error: ')'
Indeed, looking at the processed output, the MYLOG line expands to:
printf("foo\n", ) // <- Note trailing comma - bad!
Is there a semi-clean way to get this to work in all cases?
Note: My main desire here is to get this to work in a cross-platform way. I realize ## is non-standard, but I'm more interested in "works on MSVC+GCC+clang" than I am with strict standards compliance.
Also: I am aware of __VA_OPT__ coming in C++20, but it is not an option for me right now.