BOOST_PP_CAT宏

来源:互联网 发布:免费开淘宝店流程视频 编辑:程序博客网 时间:2024/06/18 05:58

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_IBOOST_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)

这样定义以后,C1A1,而C2AB。也就是说:如果b不是宏,BOOST_PP_CATBOOST_PP_CAT_SIMPLE没有区别;如果b是宏,BOOST_PP_CAT_SIMPLE依然是直接连接,而BOOST_PP_CAT宏是先将b宏展开,然后连接。

0 0
原创粉丝点击