Something like the following should work, though you might be able to improve it:
#include <boost/preprocessor.hpp>
#define VA_OPT_SUPPORTED_II_1(_) 0
#define VA_OPT_SUPPORTED_II_2(_1, _2) 1
#define VA_OPT_SUPPORTED_I(...) BOOST_PP_OVERLOAD(VA_OPT_SUPPORTED_II_, __VA_OPT__(,))(__VA_OPT__(,))
#define VA_OPT_SUPPORTED VA_OPT_SUPPORTED_I(?)
On Clang trunk, this evaluates to 1 in C++2a mode and 0 in C++17 mode. GCC trunk actually evaluates this to 1 in C++17, but also handles __VA_OPT__
in that mode.
What this does is use BOOST_PP_OVERLOAD
to call either the _1
or _2
version of _II
based on the count of arguments. If __VA_OPT__(,)
expands to ,
, there will be 2 empty arguments. If not, there will be 1 empty argument. We always call this macro with an argument list, so any compiler supporting __VA_OPT__
should always expand it to ,
.
Naturally, the Boost.PP dependency isn't mandatory. A simple 1-or-2-arg OVERLOAD
macro should be easy enough to replace. Losing a bit of generality to make it more straightforward:
#define OVERLOAD2_I(_1, _2, NAME, ...) NAME
#define OVERLOAD2(NAME1, NAME2, ...) OVERLOAD2_I(__VA_ARGS__, NAME2, NAME1)
#define VA_OPT_SUPPORTED_I(...) OVERLOAD2(VA_OPT_SUPPORTED_II_1, VA_OPT_SUPPORTED_II_2, __VA_OPT__(,))(__VA_OPT__(,))
There is one portability warning from Clang:
warning: variadic macros are incompatible with C++98 [-Wc++98-compat-pedantic]
I don't know if this detection is even possible without C++11 variadic macro support. You could consider assuming no support for __cplusplus
values lower than C++11, but Clang still gives the warning even when wrapped in such a check.