MSVC 未正确扩展 __VA_ARGS__
考虑这段代码:
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) F(__VA_ARGS__)
F(1, 2, 3)
G(1, 2, 3)
两个宏的预期输出都是 X = 1 和 VA_ARGS = 2, 3
,这就是我使用 GCC 得到的结果,但是,MSVC 将其扩展为
X = 1 and VA_ARGS = 2, 3
X = 1, 2, 3 and VA_ARGS =
: >__VA_ARGS__ 扩展为单个参数,而不是分解为多个参数。
有什么办法解决这个问题吗?
Consider this code:
#define F(x, ...) X = x and VA_ARGS = __VA_ARGS__
#define G(...) F(__VA_ARGS__)
F(1, 2, 3)
G(1, 2, 3)
The expected output is X = 1 and VA_ARGS = 2, 3
for both macros, and that's what I'm getting with GCC, however, MSVC expands this as:
X = 1 and VA_ARGS = 2, 3
X = 1, 2, 3 and VA_ARGS =
That is, __VA_ARGS__
is expanded as a single argument, instead of being broken down to multiple ones.
Any way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的 MSVC 版本是什么?您将需要 Visual C++ 2010。
__VA_ARGS__
最初由 C99 引入。 MSVC 从未尝试支持 C99,因此未添加支持。然而,现在,
__VA_ARGS__
已包含在新的 C++ 标准 C++2011(以前称为 C++0x)中,微软显然计划支持该标准,因此最近版本的 C++ 已支持它。 MSVC。顺便说一句,您需要在源文件中使用
.cpp
后缀才能获得此支持。 MSVC 已经很长时间没有更新其 C 前端了。What version of MSVC are you using? You will need Visual C++ 2010.
__VA_ARGS__
was first introduced by C99. MSVC never attempted to support C99, so the support was not added.Now, however,
__VA_ARGS__
is included in the new C++ standard, C++2011 (previously known as C++0x), which Microsoft apparently plans to support, so it has been supported in recent versions of MSVC.BTW, you will need to use a
.cpp
suffix to your source file to get this support. MSVC hasn't updated its C frontend for a long time.编辑:
此问题可以通过使用来解决
最近的 MSVC 中的
/Zc:preprocessor
或/experimental:preprocessor
选项。详情请参阅
此处。
MSVC 的预处理器的行为似乎与标准有很大不同
规格。
以下解决方法可能会有所帮助:
Edit:
This issue might be resolved by using
/Zc:preprocessor
or/experimental:preprocessor
option in recent MSVC.For the details, please see
here.
MSVC's preprocessor seems to behave quite differently from the standard
specification.
Probably the following workaround will help:
我发布了 以下 Microsoft 支持问题:
我从 Microsoft 编译器团队开发人员那里收到了以下令人不满意的答案:
I posted the following Microsoft support issue:
I received the following unsatisfying answer from a Microsoft compiler team developer: