I hope to do the following using X-macro with c++17, but since template parameter does not support trailing comma, it does not work for the std::variant part. Is there someway around it?
#define LIST_OF_TYPES(X) \
X(Type1) \
X(Type2) \
X(Type3)
#define MAKE_TYPE(name) class name {};
LIST_OF_TYPES(MAKE_TYPE)
#undef MAKE_TYPE
std::variant<
#define MAKE_VARIANT(name) name,
LIST_OF_TYPES(MAKE_VARIANT)
#undef MAKE_VARIANT
>
Yes, there's a workaround:
Without
IDENTITY2(...)this expands toEMPTY(,) Type1 IDENTITY(,) Type2 IDENTITY(,) Type3 IDENTITY().IDENTITY2forces it to expand again, this time toType1, Type2, Type3.Or, with my own macro looping library:
run on gcc.godbolt.org
Slightly more verbose, but more readable in my taste.
Here,
#define MAKE_VARIANT_BODY(n, d, x) d() xis called for each element, withxbeing the element, anddinitially set toEMPTY(4th argument ofSF_FOR_EACH()). After the first (and any subsequent) iteration,dis reassigned toUSE_COMMA(...)(akaCOMMA), so starting from the second iterationd()expands to,instead of.