Are Variadic macros nonstandard?

余生长醉 提交于 2019-11-27 09:23:55

Quote Wikipedia:

Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard.

So it's standard from C99 and C++11 onwards, but a GNU extension in C++03.

As of C++11, variadic macros are now included in standard C++. Section 16.3 of the C++11 standard specifies variadic macros such that they are compatible with variadic macros from C99 (the second form in the question).

Here is an example of a standard-conforming variadic macro definition in C++:

#define foo(x, y, ...)    bar(x, y, __VA_ARGS__)

In the form of your example "Number 2", they are standard in C99, and generally a C++ compiler's preprocessor is the same for C and C++ compilation.

They are also supported Microsoft VC++ despite its otherwise stubborn resistance to C99 compliance. So between that and GCC there are few reasons to avoid using them. Even on most embedded systems compilers I use they are supported.

Avoid the "Number 1" form however, that is firmly GCC specific, and no doubt deprecated.

Standard says in 16.3 Macro replacement:

The identifier _ _ VA_ARGS _ _ shall occur only in the replacement-list of a function-like macro that uses the ellipsis notation in the parameters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!