可变参数宏是非标准的吗?
对于调试构建,我通常使用 Clang,因为它可以更好地格式化警告和错误,并且可以更轻松地跟踪它们并修复它们。
但最近在添加带有可变参数的宏后,Clang 告诉我以下内容(来自一个虚拟项目):
main.cpp:5:20: warning: named variadic macros are a GNU extension [-Wvariadic-macros]
#define stuff3(args...) stuff_i(args)
我知道 macroname(args...)
在各种编译器中都能很好地编译,包括 Visualstudio 、Sunstudio,当然还有 GCC。但为了确保 clang 是正确的,我尝试了另外两种扩展可变参数的方法:
Number 1:
#define stuff1(...) stuff_i(...)
Number 2:
#define stuff2(...) stuff_i(__VA_ARGS__)
在两者上我都收到此消息:
main.cpp:3:16: warning: variadic macros were introduced in C99 [-Wvariadic-macros]
...这让我想知道可变参数宏是否实际上是标准的一部分C++ 的(当然我知道预处理器是独立解释的)?
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.
But recently after adding a Macro with variadic arguments, Clang told me the following (from a dummy project):
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:
Number 1:
#define stuff1(...) stuff_i(...)
Number 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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
引用维基百科:
所以它是 C99 和 C++11 以后的标准,但也是 C++03 中的 GNU 扩展。
Quote Wikipedia:
So it's standard from C99 and C++11 onwards, but a GNU extension in C++03.
从 C++11 开始,可变参数宏现已包含在标准 C++ 中。 C++11 标准的第 16.3 节指定了可变参数宏,以便它们与 C99 中的可变参数宏(问题中的第二种形式)兼容。
以下是 C++ 中符合标准的可变参数宏定义的示例:
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++:
以您的示例“Number 2”的形式,它们是 C99 中的标准,并且通常 C++ 编译器的预处理器对于 C 和 C++ 编译是相同的。
它们还支持 Microsoft VC++,尽管 Microsoft VC++ 顽固地抵制 C99 合规性。因此,在 GCC 和 GCC 之间,没有什么理由避免使用它们。即使在我使用的大多数嵌入式系统编译器上,它们也受到支持。
然而,请避免使用“Number 1”形式,这种形式是 GCC 特有的,毫无疑问已被弃用。
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.
标准在16.3宏替换中说:
Standard says in 16.3 Macro replacement: