c++ - 求宏可变参数个数

查看:1006
本文介绍了c++ - 求宏可变参数个数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

在看libco的源码。看到下面一段宏:

#define comac_arg_n( _0,_1,_2,_3,_4,_5,_6,_7,N,...) N
#define comac_get_args_cnt( ... ) comac_arg_n( __VA_ARGS__ )
#define comac_args_seqs() 7,6,5,4,3,2,1,0
#define comac_argc(...) comac_get_args_cnt( 0, ##__VA_ARGS__, comac_args_seqs())

有点搞不懂comac_argc是怎么得出宏参数的个数的

追问1:直接打印comac_argc(),不加-std=c++11是0,如果嫁了-std=c++11之后就是1了,请问这是为什么?
追问2:如果不加-std=c++11而是把##__VA_ARGS__改成__VA_ARGS__,也会打印1,这是为什么?

解决方案

comac_argc(arg1,arg2,arg3)

comac_get_args_cnt(0,arg1,arg2,arg3, 7,6,5,4,3,2,1,0)

comac_arg_n(0 ,arg1,arg2,arg3, 7 , 6, 5, 4,3,2,1,0)
            -0,  _1,   _2, _3, _4,_5,_6,_7,N
                                           ^
        

---上面是原答案----

题主评论第一个问题

The character string literal corresponding to an empty argument is "".
(since C++11)参考:http://www.open-std.org/jtc1/...

c++11之后会把空的参数的可变参数宏解释成"",这也是为什么加了-std=c++11后为1的情况,即

comac_argc()

comac_get_args_cnt(0,"", 7,6,5,4,3,2,1,0)

我们,再看gcc自己实现的特性,

Second, the ‘##’ token paste operator has a special meaning when placed between a comma and a variable argument. If you write

#define eprintf(format, …) fprintf (stderr, format, ##__VA_ARGS__) 

and the variable argument is left out when the eprintf macro is used,
then the comma before the ‘##’ will be deleted. This does not happen
if you pass an empty argument, nor does it happen if the token
preceding ‘##’ is anything other than a comma.

参考:https://gcc.gnu.org/onlinedoc...

##会干掉那个多余的,宏展开的过程变成了

comac_argc()

comac_get_args_cnt(0, 7,6,5,4,3,2,1,0)

comac_arg_n(0 , 7  ,   6,   5, 4,  3,  2, 1,0)
            -0,  _1,   _2, _3, _4,_5, _6,_7,N
                                            ^

题主评论第二个问题,


题主看红框里的展开内容,在开一下test的宏定义。不加##,宏展开的过程变成了

comac_argc()

comac_get_args_cnt(0, ,7,6,5,4,3,2,1,0)

comac_arg_n(0 ,    ,    7,  6,  5, 4,  3,  2, 1,0)
            -0,  _1,   _2, _3, _4,_5, _6, _7, N
                                              ^

这篇关于c++ - 求宏可变参数个数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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