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.

3 Answers 3

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.

2
  • 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, 2013 at 19:00
  • @Olumide you could take a look at ENUM_BINARY_PARAMS boost.org/doc/libs/1_54_0/libs/preprocessor/doc/ref/…
    – David
    Sep 13, 2013 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.)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.