##__VA_ARGS__ not swallowing comma when zero args under C99

十年热恋 提交于 2019-12-20 04:13:30

问题


I'd like to use a macro like the following:

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

To expand like so:

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

With -std=gnu99, it works perfectly.

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.

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

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?


回答1:


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

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

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.




回答2:


Yes, ##__VA_ARGS__ is a GNU extension.




回答3:


As you describe, the following pattern works fines.

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

After I run the following code snip, I guess ##__VA_ARGS__ will always swallow comma when the prefix args exists(such as _ in the e_x)

#define x(...) y(a,c, ##__VA_ARGS__, b)
#define e_x(_, ...) y(a, c, ##__VA_ARGS__, b)

x();
x(a);
x(a, b);

e_x();
e_x(a);
e_x(a, b);

You can get the result:

y(a,c,, b);
y(a,c,a, b);
y(a,c,a, b, b);

y(a, c, b);
y(a, c, b);
y(a, c, b, b);

Above sample works fine in my computer(both macos and linux) .



来源:https://stackoverflow.com/questions/37206118/va-args-not-swallowing-comma-when-zero-args-under-c99

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!