3

Is it possible to use a recursive macro to write as many function arguments as required. For example:

void foo( const char (&row1)[3] , const char (&row2)[3] , const char (&row3)[3] )
{
}

void foo( const char (&row1)[3] , const char (&row2)[3] , const char (&row3)[3] , const char (&row4)[3] , const char (&row5)[3] , const char (&row6)[3] )
{
}

I am aware that variadic templates were introduced to solve problems like this but I am limited to C++98 ATM.

Olumide
  • 5,397
  • 10
  • 55
  • 104

3 Answers3

5

You can have a look at the boost::preprocessor library which gives you a whole preprocessing toolkit. It provides preprocessor metaprogramming tools including repetition and recursion.

You can see an example in this answer.

Community
  • 1
  • 1
David
  • 9,635
  • 5
  • 62
  • 68
  • Thanks. What part of the preprocessor library should I pay close attention to? I am not at all familiar with the library and but like to solve the problem ASAP. – Olumide Sep 13 '13 at 19:00
  • @Olumide you could take a look at ENUM_BINARY_PARAMS http://www.boost.org/doc/libs/1_54_0/libs/preprocessor/doc/ref/enum_binary_params.html – David Sep 13 '13 at 20:13
0

Macros can't be recursive, since macro expansion won't expand the name of a macro in the process of being expanded. C++11 did introduce the concept of macros with a variable number of arguments, using __VA_ARGS__, but I don't know too much about it. (I use very few macros.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

Traditionally, the developer would create a set of macros, each with a different number of arguments:

#define foo1(arg1)
#define foo2(arg1, arg2)
#define foo3(arg1, arg2, arg3)
...
josh poley
  • 7,236
  • 1
  • 25
  • 25