Variadic Macros in C++

It’s looks like variable number of in macros are not supported by C++ compiler. I use PGI Workstation 7.1-4 (32-bit) to compile simple example with variadic macros…

#include <stdio.h>

//#define LOG(args...)	printf("<LOG> " args)
#define LOG(arg, ...)	printf("<LOG> " arg, __VA_ARGS__)

int main(int argc, char *argv[]) {

	LOG("blah-blah %d!\n", 10);

	return 0;
}

PGCC compile this code fine but PGCPP say some error messages…

C:\temp>pgcc -o a.exe a.c

C:\temp>pgcpp -o a.exe a.c
"d:/Program Files/PGI/Microsoft Open Tools 8/include/swprintf.inl", line 89: war
ning:
          use of function "_vswprintf" (declared at line 506 of "d:/Program
          Files/PGI/Microsoft Open Tools 8/include/stdio.h") is deprecated
      int _Ret = _vswprintf(_String, _Format, _Arglist);
                 ^

"d:/Program Files/PGI/Microsoft Open Tools 8/include/swprintf.inl", line 99: war
ning:
          use of function "_vswprintf" (declared at line 506 of "d:/Program
          Files/PGI/Microsoft Open Tools 8/include/stdio.h") is deprecated
      return _vswprintf(_String, _Format, _Ap);
             ^

"d:/Program Files/PGI/Microsoft Open Tools 8/include/swprintf.inl", line 109: wa
rning:
          use of function "__vswprintf_l" (declared at line 507 of "d:/Program
          Files/PGI/Microsoft Open Tools 8/include/stdio.h") is deprecated
      int _Ret = __vswprintf_l(_String, _Format, _Plocinfo, _Arglist);
                 ^

"d:/Program Files/PGI/Microsoft Open Tools 8/include/swprintf.inl", line 119: wa
rning:
          use of function "__vswprintf_l" (declared at line 507 of "d:/Program
          Files/PGI/Microsoft Open Tools 8/include/stdio.h") is deprecated
      return __vswprintf_l(_String, _Format, _Plocinfo, _Ap);
             ^

"a.c", line 5: error: expected an identifier
  #define LOG(arg, ...) printf("<LOG> " arg, __VA_ARGS__)
                   ^

"a.c", line 9: warning: too many arguments in macro invocation
        LOG("blah-blah %d!\n", 10);
                               ^

"a.c", line 9: error: identifier "__VA_ARGS__" is undefined
        LOG("blah-blah %d!\n", 10);
        ^

2 errors detected in the compilation of "a.c".

With GCC and G++ (I use MinGW in Windows) this code works fine.

Can someone tell me how can I compile this code with PGI C++?

Hi,

From our engineer:
variadic_macros (macro containing ellipses) are gnu extension. win32/win64 has been configured to be compatible with Microsoft C, and cannot be fully compatible with GNU.

To compile: Please use pgcpp -Wc,–variadic_macros

Hongyon

hi Hongyon!

Variadic macros are part of C99 and they have to be present in PGCC. As a programmer I would expect that preprocessor behaves same way in PGCPP as in PGCC by default…

Thanks a lot! This helps.

Hi!

This helps for macros declared as:

#define LOG(arg, ...)	printf("<LOG> " arg, __VA_ARGS__)

Do we have a magic key enabling macro like this:

#define LOG(args...)	printf("<LOG> " args)

This macros are not fully compatible… First line of example below will work with both macro definitions but second will cause an error with macro defined with VA_ARGS.

	LOG("blah-blah %s %d!\n", "hello!", 15);
	LOG("blah-blah!\n");

Thanks!


AG

Hi,

Try: -Wc,–extended_variadic_macros

Hongyon

Hi!

It’s works! thanks!

Hmm… Where I can find complete list of this voodoo magic keys? :)

Hi,

Sorry we don’t have them documented. We are planning to make it default for a next release.

Hongyon

Hi Hongyon!

May be it makes not so much sense to make them default but it’s better to make them documented.

Ok. Hopefully last question:
Do we have option like -Wc,–super_puper_gcc_3_compatible_mode.
… including also support of gcc attribute(XYZ) constructions.

Thanks!

Hi,

Here is what our engineer says:

In general, we have to balance our own cross platform compatibility
against microsoft compatibility on windows. Many gnu extensions
conflict severely with microsoft extensions, and the compiler can not be switched at runtime to accept one or the other. Thus, users will not find full gnu compatibility on windows. There is no way for the user to turn on acceptance of the gnu extension __attribute for a windows target.


Hongyon