需求场景
有些时候,我们想要写一段有固定规律代码,如下
int a0 , a1 , a2 , a3 , a4 , a5 , a6 , a7 , a8 , a9 , a10 , a11 , a12 , a13 , a14 , a15 , a16 , a17 , a18 , a19;
解决方案
包含BOOST的PP库,使用BOOST_PP_ENUM_PARAMS宏
int BOOST_PP_ENUM_PARAMS(20, a);
背后原理
通过学习BOOST_PP_ENUM_PARAMS宏的实现,加深我们对宏的理解。
其实,通过阅读下面的代码,发现用到的思想就是枚举递推,通过枚举的方式将递推关系全列出来。
#define BOOST_PP_CAT(a, b) BOOST_PP_CAT_I(a, b)
#define BOOST_PP_CAT_I(a, b) BOOST_PP_CAT_II(~, a ## b)
#define BOOST_PP_CAT_II(p, res) res
#define BOOST_PP_NUM 1
#define BOOST_PP_ENUM_PARAMS(count, param) BOOST_PP_REPEAT(count, BOOST_PP_ENUM_PARAMS_M, param)
#define BOOST_PP_REPEAT BOOST_PP_CAT(BOOST_PP_REPEAT_,BOOST_PP_NUM)
#define BOOST_PP_REPEAT_1(c, m, d) BOOST_PP_REPEAT_1_I(c, m, d)
#define BOOST_PP_REPEAT_1_I(c, m, d) BOOST_PP_REPEAT_1_ ## c(m, d)
#define BOOST_PP_REPEAT_1_0(m, d)
#define BOOST_PP_REPEAT_1_1(m, d) m(2, 0, d)
#define BOOST_PP_REPEAT_1_2(m, d) BOOST_PP_REPEAT_1_1(m, d) m(2, 1, d)
#define BOOST_PP_REPEAT_1_3(m, d) BOOST_PP_REPEAT_1_2(m, d) m(2, 2, d)
#define BOOST_PP_REPEAT_1_4(m, d) BOOST_PP_REPEAT_1_3(m, d) m(2, 3, d)
#define BOOST_PP_REPEAT_1_5(m, d) BOOST_PP_REPEAT_1_4(m, d) m(2, 4, d)
#define BOOST_PP_REPEAT_1_6(m, d) BOOST_PP_REPEAT_1_5(m, d) m(2, 5, d)
#define BOOST_PP_REPEAT_1_7(m, d) BOOST_PP_REPEAT_1_6(m, d) m(2, 6, d)
#define BOOST_PP_REPEAT_1_8(m, d) BOOST_PP_REPEAT_1_7(m, d) m(2, 7, d)
#define BOOST_PP_REPEAT_1_9(m, d) BOOST_PP_REPEAT_1_8(m, d) m(2, 8, d)
#define BOOST_PP_REPEAT_1_10(m, d) BOOST_PP_REPEAT_1_9(m, d) m(2, 9, d)
#define BOOST_PP_REPEAT_1_11(m, d) BOOST_PP_REPEAT_1_10(m, d) m(2, 10, d)
#define BOOST_PP_REPEAT_1_12(m, d) BOOST_PP_REPEAT_1_11(m, d) m(2, 11, d)
#define BOOST_PP_REPEAT_1_13(m, d) BOOST_PP_REPEAT_1_12(m, d) m(2, 12, d)
#define BOOST_PP_REPEAT_1_14(m, d) BOOST_PP_REPEAT_1_13(m, d) m(2, 13, d)
#define BOOST_PP_REPEAT_1_15(m, d) BOOST_PP_REPEAT_1_14(m, d) m(2, 14, d)
#define BOOST_PP_REPEAT_1_16(m, d) BOOST_PP_REPEAT_1_15(m, d) m(2, 15, d)
#define BOOST_PP_REPEAT_1_17(m, d) BOOST_PP_REPEAT_1_16(m, d) m(2, 16, d)
#define BOOST_PP_REPEAT_1_18(m, d) BOOST_PP_REPEAT_1_17(m, d) m(2, 17, d)
#define BOOST_PP_REPEAT_1_19(m, d) BOOST_PP_REPEAT_1_18(m, d) m(2, 18, d)
#define BOOST_PP_REPEAT_1_20(m, d) BOOST_PP_REPEAT_1_19(m, d) m(2, 19, d)
#define BOOST_PP_REPEAT_1_21(m, d) BOOST_PP_REPEAT_1_20(m, d) m(2, 20, d)
#define BOOST_PP_REPEAT_1_22(m, d) BOOST_PP_REPEAT_1_21(m, d) m(2, 21, d)
#define BOOST_PP_REPEAT_1_23(m, d) BOOST_PP_REPEAT_1_22(m, d) m(2, 22, d)
#define BOOST_PP_REPEAT_1_24(m, d) BOOST_PP_REPEAT_1_23(m, d) m(2, 23, d)
#define BOOST_PP_REPEAT_1_25(m, d) BOOST_PP_REPEAT_1_24(m, d) m(2, 24, d)
#define BOOST_PP_ENUM_PARAMS_M(z, n, param) BOOST_PP_COMMA_IF(n) param ## n
#define BOOST_PP_COMMA() ,
#define BOOST_PP_EMPTY()
#define BOOST_PP_COMMA_IF(cond) BOOST_PP_IF(cond, BOOST_PP_COMMA, BOOST_PP_EMPTY)()
#define BOOST_PP_IF(cond, t, f) BOOST_PP_IIF(BOOST_PP_BOOL(cond), t, f)
#define BOOST_PP_IIF(bit, t, f) BOOST_PP_IIF_I(bit, t, f)
#define BOOST_PP_IIF_I(bit, t, f) BOOST_PP_IIF_ ## bit(t, f)
#define BOOST_PP_IIF_0(t, f) f
#define BOOST_PP_IIF_1(t, f) t
#define BOOST_PP_BOOL(x) BOOST_PP_BOOL_I(x)
#define BOOST_PP_BOOL_I(x) BOOST_PP_BOOL_ ## x
#define BOOST_PP_BOOL_0 0
#define BOOST_PP_BOOL_1 1
#define BOOST_PP_BOOL_2 1
#define BOOST_PP_BOOL_3 1
#define BOOST_PP_BOOL_4 1
#define BOOST_PP_BOOL_5 1
#define BOOST_PP_BOOL_6 1
#define BOOST_PP_BOOL_7 1
#define BOOST_PP_BOOL_8 1
#define BOOST_PP_BOOL_9 1
#define BOOST_PP_BOOL_10 1
#define BOOST_PP_BOOL_11 1
#define BOOST_PP_BOOL_12 1
#define BOOST_PP_BOOL_13 1
#define BOOST_PP_BOOL_14 1
#define BOOST_PP_BOOL_15 1
#define BOOST_PP_BOOL_16 1
#define BOOST_PP_BOOL_17 1
#define BOOST_PP_BOOL_18 1
#define BOOST_PP_BOOL_19 1
#define BOOST_PP_BOOL_20 1
#define BOOST_PP_BOOL_21 1
#define BOOST_PP_BOOL_22 1
#define BOOST_PP_BOOL_23 1
#define BOOST_PP_BOOL_24 1
#define BOOST_PP_BOOL_25 1
#define BOOST_PP_BOOL_26 1
#define BOOST_PP_BOOL_27 1
#define BOOST_PP_BOOL_28 1
#define BOOST_PP_BOOL_29 1
#define BOOST_PP_BOOL_30 1
#define BOOST_PP_BOOL_31 1
#define BOOST_PP_BOOL_32 1
#define BOOST_PP_BOOL_33 1
#define BOOST_PP_BOOL_34 1
#define BOOST_PP_BOOL_35 1
#define BOOST_PP_BOOL_36 1
#define BOOST_PP_BOOL_37 1
#define BOOST_PP_BOOL_38 1
#define BOOST_PP_BOOL_39 1
#define BOOST_PP_BOOL_40 1
#define BOOST_PP_BOOL_41 1
#define BOOST_PP_BOOL_42 1
#define BOOST_PP_BOOL_43 1
#define BOOST_PP_BOOL_44 1
#define BOOST_PP_BOOL_45 1
#define BOOST_PP_BOOL_46 1
#define BOOST_PP_BOOL_47 1
#define BOOST_PP_BOOL_48 1
#define BOOST_PP_BOOL_49 1
#define BOOST_PP_BOOL_50 1
#define BOOST_PP_BOOL_51 1
#define BOOST_PP_BOOL_52 1
#define BOOST_PP_BOOL_53 1
#define BOOST_PP_BOOL_54 1
#define BOOST_PP_BOOL_55 1
#define BOOST_PP_BOOL_56 1
#define BOOST_PP_BOOL_57 1
#define BOOST_PP_BOOL_58 1
#define BOOST_PP_BOOL_59 1
#define BOOST_PP_BOOL_60 1
#define BOOST_PP_BOOL_256 1
int main()
{
int BOOST_PP_ENUM_PARAMS(20, a);
return 0;
}
参考文献
- https://blog.csdn.net/xinqingwuji/article/details/89134274
- https://www.cnblogs.com/hjbf/p/11989585.html
- https://www.xuebuyuan.com/2198272.html