求宏可变参数个数

在看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

^

以上是 求宏可变参数个数 的全部内容, 来源链接: utcz.com/p/191127.html

回到顶部