I have a C++ macro-based DSL that defines a macro like this:
#define RETURNS(...) \
enable_if_t<__VA_ARGS__ WHEN
#define WHEN(...) \
, EAT_ ## __VA_ARGS__ >
#define EAT_requires
This is for use like:
template<class T>
auto some_function(T t) ->
RETURNS(int)
(requires SomeConcept<T>)
Which expands to:
template<class T>
auto some_function(T t) ->
enable_if_t<int, SomeConcept<T>>
(When C++20 concepts are enabled, this expands to a real requires
clause.)
I would prefer the order of the parameters to be flipped. That is, I would like it to generate this:
template<class T>
auto some_function(T t) ->
enable_if_t<SomeConcept<T>, int>
I think it's not possible. Can some clever PP hacker prove me wrong?
If it is tolerable to omit an open paren, you can achieve it like this:
#define UNWRAP(...) __VA_ARGS__
#define RETURNS(...) \
WHEN ((__VA_ARGS__),
#define WHEN(x, ...) \
enable_if_t<EAT_ ## __VA_ARGS__, UNWRAP x>
#define EAT_requires
template<class T>
auto some_function(T t) ->
RETURNS(pair<int, int>)
requires SomeConcept<T, int>)
Input:
template<class T>
auto some_function(T t) ->
RETURNS(pair<int, int>)
requires SomeConcept<T, int>)
Output:
template<class T>
auto some_function(T t) ->
enable_if_t< SomeConcept<T, int>, pair<int, int> >
Why don't you just use something like
template<class T,bool B>
using reverse_enable_if_t=enable_if_t<B,T>;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With