可变参数宏是非标准的吗?

发布于 2024-10-14 00:42:06 字数 747 浏览 3 评论 0原文

对于调试构建,我通常使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请别遗忘我 2024-10-21 00:42:06

引用维基百科

变量参数宏于 1999 年在 C 语言标准的 ISO/IEC 9899:1999 (C99) 修订版中引入,并于 2011 年在 C++ 语言的 ISO/IEC 14882:2011 (C++11) 修订版中引入标准。

所以它是 C99 和 C++11 以后的标准,但也是 C++03 中的 GNU 扩展。

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.

空袭的梦i 2024-10-21 00:42:06

从 C++11 开始,可变参数宏现已包含在标准 C++ 中。 C++11 标准的第 16.3 节指定了可变参数宏,以便它们与 C99 中的可变参数宏(问题中的第二种形式)兼容。

以下是 C++ 中符合标准的可变参数宏定义的示例:

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

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__)
如梦亦如幻 2024-10-21 00:42:06

以您的示例“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.

久而酒知 2024-10-21 00:42:06

标准在16.3宏替换中说:

标识符 _ _ VA_ARGS _ _ 只能出现在使用以下函数的类函数宏的替换列表中
参数中的省略号表示法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文