## __ VA_ARGS__在C99下为零args时不吞下逗号 [英] ##__VA_ARGS__ not swallowing comma when zero args under C99

查看:175
本文介绍了## __ VA_ARGS__在C99下为零args时不吞下逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用宏如下所示:

#define x(...) y(a,##__VA_ARGS__,b)

要扩大像这样:

x();   ->   y(a,b);
x(1);  ->   y(a,1,b);

使用,它完美.

With -std=gnu99, it works perfectly.

但是,使用-std=c99时,它看起来像这样:

With -std=c99 however, it looks like this:

x();   ->   y(a,,b);
x(1);  ->   y(a,1,b);

是制备没有区别. - 它不吞咽逗号

The ## is making no difference – it's not swallowing the comma.

在C99,例如在其他用途#define x(a,...) y(a,##__VA_ARGS__),可以用逗号吞咽了.

In other usages under C99, e.g. #define x(a,...) y(a,##__VA_ARGS__), comma-swallowing works fine.

我能做些什么,如果有的话,要得到铛所需的逗号吞咽行为的,无论是与GNU扩展或通过其他方法?

What can I do, if anything, to get the desired comma-swallowing behaviour under clang's -std=c99, either with the GNU extension ## or by some other method?

推荐答案

这是预期的行为.还有就是要吞下逗号没有标准的方式.

That's expected behaviour. There is no standard way to swallow the comma.

幸运的是,支持 GNU扩展,和燕子逗号自动

Fortunately, gcc, clang and xlc support the ##__VA_ARGS__ gnu extension, and msvc swallows the comma automatically.

如果你不想依靠上述语言扩展,惯用ISO C90的方式来获得可变参数宏是这样的:

If you don't want to rely on above mentioned language extensions, the idiomatic ISO C90 way to get variable argument macros was like this:

#define x(args) y args

/* notice the extra parantheses */
x((a, b, c, d));

如果您不想使用这两种解决方案中的任何一种,则可以始终使用参数计数,如此处

If you don't want to use either of these solutions, you can always employ argument counting, as answered here.

这篇关于## __ VA_ARGS__在C99下为零args时不吞下逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆