Home:ALL Converter>Variadic macros arguments

Variadic macros arguments

Ask Time:2013-08-11T09:40:47         Author:Max Frai

Json Formatter

I have an assert macros which looks like:

#define ASSERT(condition, ...) \
  (condition) ? (void)0 : MyLogFunction(__LINE__, __VA_ARGS__)

MyLogFunction is a variadic template too:

template<typename... Args>
void MyLogFunction(int line, const Args&... args) {/*code*/}

Everything works well except the case when I don't want to insert additional information into assert call.

So this works nice:

ASSERT(false, "test: %s", "formatted");

But this isn't:

ASSERT(false);

I believe there is a way to handle situation when no variadic args has has been passed into macro call and there is a way to insert something like simple string "" instead of __VA_ARGS__

Author:Max Frai,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18167956/variadic-macros-arguments
v154c1 :

Not really a solution for the macros, but a simple workaround would be to provide a helper variadic function template, which can get 0 parameters and do the condition checking there:\n\n#define ASSERT(...) \\\n MyLogHelper(__LINE__, __VA_ARGS__)\n\ntemplate<typename... Args>\nvoid MyLogFunction(int line, const Args&... ) {/*code*/}\n\ntemplate<typename... Args>\nvoid MyLogHelper(int line, bool condition, const Args&... args)\n{\n if (!condition) MyLogFunction(line,args...);\n}\n",
2013-08-11T08:50:54
yy