BOOST_PP_CAT宏主要用来连接两个标识符。此宏被其它地方用到。
定义此宏的头文件boost\preprocessor\cat.hpp
# 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
为什么BOOST_PP_CAT宏要定义这么多辅助宏(BOOST_PP_CAT_I和BOOST_PP_CAT_II)呢?为什么不直接如下定义:
#define BOOST_PP_CAT_SIMPLE(a, b) a##b
原因是这两者之间是有细微的差别的。
请看下面的例子。
int AB = 512;
int A1 = 1024;
#define B 1
#define C1 BOOST_PP_CAT(A, B)
#define C2 BOOST_PP_CAT_SIMPLE(A, B)
这样定义以后,C1是A1,而C2是AB。也就是说:如果b不是宏,BOOST_PP_CAT和BOOST_PP_CAT_SIMPLE没有区别;如果b是宏,BOOST_PP_CAT_SIMPLE依然是直接连接,而BOOST_PP_CAT宏是先将b宏展开,然后连接。
本文介绍了BOOST_PP_CAT宏在C++中的作用,它用于连接两个标识符。通过解析BOOST_PP_CAT宏的定义,揭示了为何需要多个辅助宏的原因,并通过实例对比了BOOST_PP_CAT与直接使用连接操作符##的区别,强调了当第二个参数为宏时,BOOST_PP_CAT会先展开再连接的特性。
126

被折叠的 条评论
为什么被折叠?



