11

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
uj2
  • 2,255
  • 2
  • 21
  • 32
  • I think you can do it. I did something very similar here: [How to easily create fully “variadic” functions with C++ 98 standard?](https://stackoverflow.com/questions/59331919/how-to-easily-create-fully-variadic-functions-with-c-98-standard/). – Evandro Coan Dec 15 '19 at 00:08

3 Answers3

13

No, because the macro invocation MAKE_TEMPLATE() does not have zero arguments at all; it has one argument comprising zero tokens.

Older preprocessors, apparently including GCC at the time this answer was originally written, sometimes interpreted an empty argument list as you'd hope, but the consensus has moved toward a stricter, narrower extension which more closely conforms to the standard.

To get the answer below to work, define an additional macro parameter before the ellipsis:

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

and then always put a comma before the first argument when the list is not empty:

   MAKE_TEMPLATE(, foo )

Old answer

According to http://gcc.gnu.org/onlinedocs/gcc/Variadic-Macros.html, GCC does support this, just not transparently.

Syntax is:

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

Anyway, both also support variadic templates in C++0x mode, which is far preferable.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Thanks. btw, is this standard behavior, or a gcc thing? – uj2 Aug 25 '10 at 06:14
  • 1
    @uj2: It's GCC; the standard simply forbids empty variadic lists. By the way, this is mixing C99 with C++ anyway, so this code is strictly non-standard unless you're in C++0x… in which case you should… – Potatoswatter Aug 25 '10 at 06:16
  • Don't mind the template, it's just a toy example. How is this necessarily 0x, doesn't C++98/03 define variadic macros? – uj2 Aug 25 '10 at 06:22
  • §16.3/9: "A preprocessing directive of the form `# define identifier lparen identifier-listopt ) replacement-list new-line` defines a function-like macro with parameters, similar syntactically to a function call." — that's all there is to it. – Potatoswatter Aug 25 '10 at 06:29
  • @uj2: No, C++03 does not have variadic macros or templates. C++0x provides both. – GManNickG Aug 25 '10 at 06:58
  • Note about the standard forbidding empty variadic lists: Since the syntax is the same to pass zero arguments or one empty argument (i.e. `MACRO()`), the standard does actually allow empty variadic lists, only in the case of no other parameters… – Potatoswatter Mar 26 '12 at 00:44
3

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.

qrdl
  • 34,062
  • 14
  • 56
  • 86
1

First of all beware that variadic macros are not part of the current C++. It seems that they will be in the next version. At the moment they are only conforming if you program in C99.

As of variadic macros with zero arguments, there are tricks à la boost to detect this and to macro-program around it. Googel for empty macro arguments.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177