How to prefix __VA_ARGS__ elements in a macro

89 Views Asked by At

Is it possible to prefix each element of variadic parameters with something else?

#define INPUTS(...)  ???

Function(INPUTS(a, b, c))

should become

Function(IN a, IN b, IN c)
1

There are 1 best solutions below

0
Dúthomhas On

Take a look at Boost.Preprocessor.

While daunting, it can be used to do all kinds of weird things, like exactly this:

#include <boost/preprocessor.hpp>

#define MAKE_IN_ARG(r,_,arg) (IN arg)
#define ARGS(...) \
  BOOST_PP_SEQ_TO_TUPLE( \
    BOOST_PP_SEQ_FOR_EACH(MAKE_IN_ARG,, \
      BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
      
void f ARGS( int a, float b, char c )
{
  //...
}

Produces:

void f (IN int a, IN float b, IN char c )
{

}

That said...

You are making a mistake. Don’t do this. Just type out the IN in front of every input argument when you write the function header. Your life will be significantly easier.