这个C宏的含义是什么? [英] what is the meaning of this C macro?

查看:89
本文介绍了这个C宏的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个可以计算参数数量的宏.和类似的代码:

#define Y_TUPLE_SIZE_II(__args) Y_TUPLE_SIZE_I __args
#define Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0

#define Y_TUPLE_SIZE_I(__p0,__p1,__p2,__p3,__p4,__p5,__p6,__p7,__p8,__p9,__p10,__p11,__p12,__p13,__p14,__p15,__p16,__p17,__p18,__p19,__p20,__p21,__p22,__p23,__p24,__p25,__p26,__p27,__p28,__p29,__p30,__p31,__n,...) __n

#define MPL_ARGS_SIZE(...) Y_TUPLE_SIZE_II((Y_TUPLE_SIZE_PREFIX_ ## __VA_ARGS__ ## _Y_TUPLE_SIZE_POSTFIX,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))

// the running result ---------------------------------------

MPL_ARGS_SIZE(a,b,c,d,e,f,g)==7

MPL_ARGS_SIZE(a,b,c,d)==4

如何理解

#define Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0

Y_TUPLE_SIZE_PREFIX_ ## __VA_ARGS__ ## _Y_TUPLE_SIZE_POSTFIX

?

顺便说一句,我知道##(磅,磅)的用法以及#define Y_TUPLE_SIZE_I的机制

解决方案

PREFIX和POSTFIX宏用于在未提供任何参数(即MPL_ARGS_SIZE())时将其设置为0.在这种情况下,将Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX连接起来生成Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX,这将结果强制为0.

在一般情况下,__VA_ARGS__是非空的,因此串联仅扩展为给定的相同数量的参数.其次是32,... 0.

在两种情况下,参数都用括号括起来. Y_TUPLE_SIZE_II除去这些多余的括号,并将参数传递给Y_TUPLE_SIZE_I. Y_TUPLE_SIZE_I只是扩展到其第33个参数,而丢弃其余的参数.

因此,如果给它提供32个参数,则将跳过这32个参数,并根据需要将其后的数字32作为结果.如果您给它提供31个参数,它将跳过这31个参数,并跳过后面的第一个数字,即32,结果将再次根据需要成为下一个数字31.

如果只给它一个参数,它将跳过该参数及其后的31个,结果将为1.

如果不提供任何参数,将使用Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX的特殊情况,即32个空参数后跟0.32个空参数将被跳过,结果将为0.

不带参数的特殊情况的原因是,如果没有它,它的行为将与单参数情况相同.以下内容可能有助于更好地理解它:

#define Y_TUPLE_SIZE_II(__args) Y_TUPLE_SIZE_I __args
#define Y_TUPLE_SIZE_I(__p0,__p1,__p2,__p3,__p4,__p5,__p6,__p7,__p8,__p9,__p10,__p11,__p12,__p13,__p14,__p15,__p16,__p17,__p18,__p19,__p20,__p21,__p22,__p23,__p24,__p25,__p26,__p27,__p28,__p29,__p30,__p31,__n,...) __n

#define MPL_ARGS_SIZE(...) Y_TUPLE_SIZE_II((__VA_ARGS__,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))

这是原始的宏集,但是删除了零参数的所有特殊情况处理.它适用于除零参数情况以外的所有情况,该情况返回1而不是0.

为了处理零个参数,它将前缀列表夹在前缀和后缀宏之间.如果结果扩展为Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX,则参数列表为空,并且特殊情况起作用.

Here is a macro can calculate the count of arguments. and the code like:

#define Y_TUPLE_SIZE_II(__args) Y_TUPLE_SIZE_I __args
#define Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0

#define Y_TUPLE_SIZE_I(__p0,__p1,__p2,__p3,__p4,__p5,__p6,__p7,__p8,__p9,__p10,__p11,__p12,__p13,__p14,__p15,__p16,__p17,__p18,__p19,__p20,__p21,__p22,__p23,__p24,__p25,__p26,__p27,__p28,__p29,__p30,__p31,__n,...) __n

#define MPL_ARGS_SIZE(...) Y_TUPLE_SIZE_II((Y_TUPLE_SIZE_PREFIX_ ## __VA_ARGS__ ## _Y_TUPLE_SIZE_POSTFIX,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))

// the running result ---------------------------------------

MPL_ARGS_SIZE(a,b,c,d,e,f,g)==7

MPL_ARGS_SIZE(a,b,c,d)==4

how to understand

#define Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0

and

Y_TUPLE_SIZE_PREFIX_ ## __VA_ARGS__ ## _Y_TUPLE_SIZE_POSTFIX

?

BTW, I know about ##(pound, pound) usage and the mechanism of #define Y_TUPLE_SIZE_I

解决方案

The PREFIX and POSTFIX macros are intended to make it give 0 when no arguments are given, i.e. MPL_ARGS_SIZE(). In this case, Y_TUPLE_SIZE_PREFIX_ and _Y_TUPLE_SIZE_POSTFIX are concatenated to produce Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX, which forces the result to 0.

In the general case, __VA_ARGS__ is non-empty, so the concatenation just expands to the same number of arguments that were given. This is followed by 32, ... 0.

In both cases, the arguments are wrapped in parentheses. Y_TUPLE_SIZE_II strips off these extra parentheses and passes the arguments on to Y_TUPLE_SIZE_I. Y_TUPLE_SIZE_I just expands to its 33rd argument, discarding the rest.

So if you give it 32 arguments, those 32 will be skipped and the number following them, 32, will be the result, as desired. If you give it 31 arguments, it will skip those 31, and the first number that follows, i.e. 32, and the result will be the next number, 31, again as desired.

If you give it a single argument, it will skip that argument and the 31 that follow it, and the result will be 1.

If you give it no arguments, the special case of Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX will come into play, which is 32 empty arguments followed by 0. The 32 empty arguments will be skipped, and the result will be 0.

The reason for the special case for no arguments is that without it, it would behave the same as the one-argument case. The following might help understand it better:

#define Y_TUPLE_SIZE_II(__args) Y_TUPLE_SIZE_I __args
#define Y_TUPLE_SIZE_I(__p0,__p1,__p2,__p3,__p4,__p5,__p6,__p7,__p8,__p9,__p10,__p11,__p12,__p13,__p14,__p15,__p16,__p17,__p18,__p19,__p20,__p21,__p22,__p23,__p24,__p25,__p26,__p27,__p28,__p29,__p30,__p31,__n,...) __n

#define MPL_ARGS_SIZE(...) Y_TUPLE_SIZE_II((__VA_ARGS__,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0))

This is the original set of macros, but with all of the special-case handling for zero arguments removed. It works for everything but the zero-argument case, which returns 1 instead of 0.

In order to handle zero arguments, it sandwiches the argumement list between the prefix and postfix macros. If the result expands to Y_TUPLE_SIZE_PREFIX__Y_TUPLE_SIZE_POSTFIX, then the argument list was empty, and the special case comes into play.

这篇关于这个C宏的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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