BOOST_PP_SEQ_SIZE宏

本文详细介绍了Boost库中的PP_SEQ_SIZE宏,用于计算序列的长度。通过实例展示了该宏的使用及展开过程,解释了其内部工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义此宏的头文件boost\preprocessor\seq\size.hpp

此宏用来求一个序列的长度。

 

举个列子:

先定义一个序列,

#define BOOST_UNORDERED_PRIMES (17ul)(29ul)(37ul)(53ul)

(此序列可以在boost\unordered\detail\util.hpp中找到)

BOOST_PP_SEQ_SIZEBOOST_UNORDERED_PRIMES)即为序列BOOST_UNORDERED_PRIMES的长度4

 

宏定义如下:

#define BOOST_PP_SEQ_SIZE(seq) BOOST_PP_SEQ_SIZE_I(seq)

#define BOOST_PP_SEQ_SIZE_I(seq) BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_0 seq)

 

# define BOOST_PP_SEQ_SIZE_0(_) BOOST_PP_SEQ_SIZE_1

# define BOOST_PP_SEQ_SIZE_1(_) BOOST_PP_SEQ_SIZE_2

# define BOOST_PP_SEQ_SIZE_2(_) BOOST_PP_SEQ_SIZE_3

# define BOOST_PP_SEQ_SIZE_3(_) BOOST_PP_SEQ_SIZE_4

# define BOOST_PP_SEQ_SIZE_4(_) BOOST_PP_SEQ_SIZE_5

# define BOOST_PP_SEQ_SIZE_5(_) BOOST_PP_SEQ_SIZE_6

# define BOOST_PP_SEQ_SIZE_6(_) BOOST_PP_SEQ_SIZE_7

# define BOOST_PP_SEQ_SIZE_7(_) BOOST_PP_SEQ_SIZE_8

# define BOOST_PP_SEQ_SIZE_8(_) BOOST_PP_SEQ_SIZE_9

# define BOOST_PP_SEQ_SIZE_9(_) BOOST_PP_SEQ_SIZE_10

 

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_0 0

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_1 1

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_2 2

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_3 3

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_4 4

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_5 5

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_6 6

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_7 7

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_8 8

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_9 9

# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_10 10

 

下面来围观这个宏是如何展开的,以及如何实现求序列长度。

--->BOOST_PP_SEQ_SIZEBOOST_UNORDERED_PRIMES

--->BOOST_PP_SEQ_SIZE((17ul)(29ul)(37ul)(53ul))

--->BOOST_PP_SEQ_SIZE_I((17ul)(29ul)(37ul)(53ul)) 

--->BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_0 (17ul)(29ul)(37ul)(53ul))

--->BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_1(29ul)(37ul)(53ul))

此处为什么先展开BOOST_PP_SEQ_SIZE_0 (17ul)(29ul)(37ul)(53ul),请参考BOOST_PP_CAT宏 http://blog.csdn.net/freemannnn/article/details/24524919

--->BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_2(37ul)(53ul))

--->BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_3(53ul))

--->BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_4)

--->BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_4

--->4

自此,宏展开结束。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值