##__VA_ARGS__ not swallowing comma when zero args under C99

后端 未结 3 490
囚心锁ツ
囚心锁ツ 2021-01-24 09:13

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)         


        
3条回答
  •  鱼传尺愫
    2021-01-24 09:30

    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) .

提交回复
热议问题