是否可以在 Visual C++ 中防止删除带有空 __VA_ARGS__ 的逗号?
在 Visual Studio 2005 上,我有一个如下所示的宏(示例!!):
#define MY_CALL(FUN, ...) \
if(prepare(x, y)) { \
FUN(__VA_ARGS__); \
}
/**/
只要该函数至少接受一个参数,就可以了。
当函数采用零参数时,预处理器“有效地”删除“尾随逗号”,扩展如下内容:
if(prepare(x y)) { funct(); }
太棒了,不是吗?
如何修复此宏,以便它可以在 Visual C++ (VS 2005) 上使用零 __VA_ARGS__
?
On Visual Studio 2005 I have a macro that looks like this (examplified!!):
#define MY_CALL(FUN, ...) \
if(prepare(x, y)) { \
FUN(__VA_ARGS__); \
}
/**/
As long as the function takes at least one argument, I'm fine.
When the function takes zero arguments, the preprocessor "helpfully" removes the "trailing comma", expanding something like this:
if(prepare(x y)) { funct(); }
Great, isn't it?
How can I fix this macro, so that it'll work with zero __VA_ARGS__
on Visual C++ (VS 2005)?
Apparently this is a bug in VS2005.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,我不再使用 Visual C++(因此无法验证它是否有效),但是你能尝试一下吗?
使用 gcc 4.2,在该上下文中允许
{0,}
和{0}
,因此逗号是否被删除并不重要。然而,我不确定这是否在规范中被普遍接受,是一个普遍实现的扩展,还是特定于 gcc 的东西。如果 Visual C++ 允许
{0,}
语法,那么这将有望解决您的问题(假设我正确理解__VA_ARGS__< 之前的最新逗号) /code> 是被错误删除的内容,无论它出现在语法中的哪个位置)。
Unfortunately, I do not use Visual C++ anymore (and as so cannot verify this works), but can you try this?
Using gcc 4.2, both
{0,}
and{0}
are allowed in that context, so if the comma gets deleted or not it would not matter. However, I am not certain whether that is generally accepted in the spec, a commonly implemented extension, or something specific to gcc.If the
{0,}
syntax is allowed by Visual C++, then this would hopefully solve your problem (assuming I understand correctly that the most recent comma before__VA_ARGS__
is what is being incorrectly deleted, regardless of where it is appearing in the syntax).