Flip the order of X and Y in MACRO(X)(Y)

  • A+
Category:Languages

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> > 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: