Chinaunix

标题: 可变参数... args...有什么区别? [打印本页]

作者: blake326    时间: 2017-04-25 10:44
标题: 可变参数... args...有什么区别?
可变参数宏定义的时候,可以用 ... 格式 和 args... 格式。
... 使用时用 __VA_ARGS__   或  ##__VA_ARGS__
args... 使用时 用 args 或者 ##args


这两个方式有什么区别?



#define trace_printk(fmt, ...)                \
do {                            \
    char _______STR[] = __stringify((__VA_ARGS__));    \
    if (sizeof(_______STR) > 3)            \
        do_trace_printk(fmt, ##__VA_ARGS__);    \
    else                        \
        trace_puts(fmt);            \
} while (0)

#define do_trace_printk(fmt, args...)                    \
do {                                    \
    static const char *trace_printk_fmt                \
        __attribute__((section("__trace_printk_fmt"))) =    \
        __builtin_constant_p(fmt) ? fmt : NULL;            \
                                    \
    __trace_printk_check_format(fmt, ##args);            \
                                    \
    if (__builtin_constant_p(fmt))                    \
        __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);    \
    else                                \
        __trace_printk(_THIS_IP_, fmt, ##args);            \
} while (0)



作者: folklore    时间: 2017-04-25 13:29
__VA_ARGS__ 是以前的写法,
##args是新写法吧~
作者: bruceteen    时间: 2017-04-26 08:24
__VA_ARGS__ 和 ##__VA_ARGS__ 的区别
Note: some compilers offer an extension that allows ## to appear after a comma and before __VA_ARGS__, in which case the ## does nothing when __VA_ARGS__ is non-empty, but removes the comma when __VA_ARGS__ is empty: this makes it possible to define macros such as fprintf (stderr, format, ##__VA_ARGS__)

作者: blake326    时间: 2017-04-26 12:23
folklore 发表于 2017-04-25 13:29
__VA_ARGS__ 是以前的写法,
##args是新写法吧~

恩,我试了下编译出来汇编都一样
就是格式不同
Args...这种格式更容易阅读





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2