Macro with n number of arguments

Possible duplicate:
C / C ++: how to make a variable macro (variable number of arguments)

Just wondering if this is possible at all. Therefore, instead of how IM processes logging and messages with several parameters, they need to have several different macros for each case, for example:

#define MSG(             msg                                    )
#define MSG1(            fmt, arg1                              )
#define MSG2(            fmt, arg1, arg2                        )
#define MSG3(            fmt, arg1, arg2, arg3                  )
#define MSG4(            fmt, arg1, arg2, arg3, arg4            )
#define MSG5(            fmt, arg1, arg2, arg3, arg4, arg5      )
#define MSG6(            fmt, arg1, arg2, arg3, arg4, arg5, arg6)

Is there a way to define only one macro that can take any number of arguments?

thank

+3
source share
2 answers

Well, since @GMan didn't want to put this as the answer itself, look at the variable macros that are part of C99.

++. Variadic ++, : GCC MSV++, MSVC2005.

+2

, - , ++ :

#define CSVTHROW( msg )         \
{                                 \
    std::ostringstream os;         \
    os << msg;                     \
    throw CSVED::Exception(os.str());   \
}                               \

, :

CSVTHROW( "Problem caused by " << x << " being less than " << y );

throw .

+2

Source: https://habr.com/ru/post/1727743/


All Articles