C++ 可变参数宏是非标准的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4786649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are Variadic macros nonstandard?
提问by Thomas
For debugbuilds, I usually use Clang, as it formats warnings and errors better, and makes it a little easier to track them down, and fix them.
对于调试构建,我通常使用 Clang,因为它可以更好地格式化警告和错误,并且可以更轻松地跟踪和修复它们。
But recently after adding a Macro with variadic arguments, Clang told me the following (from a dummy project):
但最近在添加了一个带有可变参数的宏之后,Clang 告诉我以下内容(来自一个虚拟项目):
main.cpp:5:20: warning: named variadic macros are a GNU extension [-Wvariadic-macros]
#define stuff3(args...) stuff_i(args)
I know that macroname(args...)
compiles fine in a wide range of compilers, including Visualstudio, Sunstudio, and of course GCC. But just to make sure that clang is right, I tried two other ways of expanding the variadic arguments:
我知道macroname(args...)
在各种编译器中都可以很好地编译,包括 Visualstudio、Sunstudio,当然还有 GCC。但为了确保 clang 是正确的,我尝试了另外两种扩展可变参数参数的方法:
Number 1:
1号:
#define stuff1(...) stuff_i(...)
Number 2:
2号:
#define stuff2(...) stuff_i(__VA_ARGS__)
On both I receive this message:
在这两个我收到这条消息:
main.cpp:3:16: warning: variadic macros were introduced in C99 [-Wvariadic-macros]
... Which makes me wonder if Variadic macros are actually part of the standard of C++ (and of course I know that the Preprocessor is interpreted independently)?
...这让我想知道 Variadic 宏是否实际上是 C++ 标准的一部分(当然我知道预处理器是独立解释的)?
回答by Thomas
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.
可变参数宏于 1999 年在 C 语言标准的 ISO/IEC 9899:1999 (C99) 修订版中引入,并于 2011 年在 C++ 语言标准的 ISO/IEC 14882:2011 (C++11) 修订版中引入。
So it's standard from C99 and C++11 onwards, but a GNU extension in C++03.
所以它是 C99 和 C++11 以后的标准,但它是 C++03 中的 GNU 扩展。
回答by Dan Moulding
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).
从 C++11 开始,可变参数宏现在包含在标准 C++ 中。C++11 标准的第 16.3 节指定了可变参数宏,以便它们与来自 C99 的可变参数宏(问题中的第二种形式)兼容。
Here is an example of a standard-conforming variadic macro definition in C++:
以下是 C++ 中符合标准的可变参数宏定义的示例:
#define foo(x, y, ...) bar(x, y, __VA_ARGS__)
回答by Clifford
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.
以您的示例“数字 2”的形式,它们是 C99 中的标准,并且通常 C++ 编译器的预处理器对于 C 和 C++ 编译是相同的。
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.
尽管 Microsoft VC++ 顽固地抵制 C99 合规性,但它们也得到了 Microsoft VC++ 的支持。因此,在 GCC 和 GCC 之间,几乎没有理由避免使用它们。即使在我使用的大多数嵌入式系统编译器上,它们也受支持。
Avoid the "Number 1" form however, that is firmly GCC specific, and no doubt deprecated.
但是,请避免使用“Number 1”形式,它是 GCC 特定的,毫无疑问已被弃用。
回答by Sebastian Mach
Standard says in 16.3 Macro replacement:
标准在16.3 宏替换中说:
The identifier _ _ VA_ARGS _ _ shall occur only in the replacement-list of a function-like macro that uses the ellipsis notation in the parameters.
标识符_ _ VA_ARGS _ _ 应仅出现在参数中使用省略号表示法的类似函数的宏的替换列表中。