Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic macros with zero arguments, and commas

Consider this macro:

#define MAKE_TEMPLATE(...) template <typename T, __VA_ARGS__ >

When used with zero arguments it produces bad code since the compiler expects an identifier after the comma. Actually, VC's preprocessor is smart enough to remove the comma, but GCC's isn't. Since macros can't be overloaded, it seems like it takes a separate macro for this special case to get it right, as in:

#define MAKE_TEMPLATE_Z() template <typename T>

Is there any way to make it work without introducing the second macro?

like image 412
uj2 Avatar asked Aug 25 '10 05:08

uj2


Video Answer


1 Answers

In case of GCC you need to write it like this:

#define MAKE_TEMPLATE(...) template <typename T, ##__VA_ARGS__ >

If __VA_ARGS__ is empty, GCC's preprocessor removes preceding comma.

like image 154
qrdl Avatar answered Sep 30 '22 06:09

qrdl