该宏位于目录
boost/preprocessor/iteration/local.hpp
文档说它的作用是:
该宏将用户自定义宏 BOOST_PP_LOCAL_MACRO 以 BOOST_PP_LOCAL_LIMITS
所指定的区间中的值垂直展开。
还给出了一个例子:
#include <boost/preprocessor/iteration/local.hpp> template<int> struct sample; #define BOOST_PP_LOCAL_MACRO(n) / template<> struct sample<n> { / enum { value = n }; / }; / /**/ #define BOOST_PP_LOCAL_LIMITS (1, 5) #include BOOST_PP_LOCAL_ITERATE()
它将展开为:
template<> struct sample<1> { enum { value = 1 }; template<> struct sample<2> { enum { value = 2 }; template<> struct sample<3> { enum { value = 3 }; template<> struct sample<4> { enum { value = 4 }; template<> struct sample<5> { enum { value = 5 };
上面的代码在VC2008不能通过编译,但是这只需要一点修改,上面的代码修改为:
#include <boost/preprocessor/iteration/local.hpp> template<int n> struct sample; #define BOOST_PP_LOCAL_MACRO(n) template<> struct sample<n> { enum { value = n }; }; //需要成为一行。 #define BOOST_PP_LOCAL_LIMITS (1, 5) #include BOOST_PP_LOCAL_ITERATE()
BOOST_PP_LOCAL_MACRO宏和BOOST_PP_LOCAL_LIMITS宏都是用户自定定义代码。
其中BOOST_PP_LOCAL_LIMITS的定义后面需要一个空格(至少)。像上面的
#define BOOST_PP_LOCAL_LIMITS (1,5)。这意味着(1,5)并不是宏的参数,或是一个整体。
而BOOST_PP_LOCAL_LIMITS本身就代表 (1,5)
BOOST_PP_LOCAL_ITERATE宏代码定义如下:
# define BOOST_PP_LOCAL_ITERATE() <boost/preprocessor/iteration/detail/local.hpp> //这显然代表包括一个头文件,使用当然需要#include BOOST_PP_LOCAL_ITERATE() //扩展为: //#include <boost/preprocessor/iteration/detail/local.hpp>
local.hpp 的代码如下:
# if !defined(BOOST_PP_LOCAL_LIMITS)//用户自定义的范围,形如:(3,9) # error BOOST_PP_ERROR: local iteration boundaries are not defined //没定义出错 # elif !defined(BOOST_PP_LOCAL_MACRO)//用户自定义的生成代码,像上面的举例 # error BOOST_PP_ERROR: local iteration target macro is not defined //没定义就要报错 # else # if ~BOOST_PP_CONFIG_FLAGS() & BOOST_PP_CONFIG_EDG() //BOOST_PP_TUPLE_ELEM(size, i, tuple) 宏参数第一个是tuple的大小 //假如tuple为(2,3),这样size =2, i是索引,从0开始。如果 i =0结果就是2, //如果是i = 1,结果就是3 # define BOOST_PP_LOCAL_S BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_LOCAL_LIMITS) # define BOOST_PP_LOCAL_F BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_LOCAL_LIMITS) # else # define BOOST_PP_VALUE BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_LOCAL_LIMITS) # include <boost/preprocessor/iteration/detail/start.hpp> # define BOOST_PP_VALUE BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_LOCAL_LIMITS) # include <boost/preprocessor/iteration/detail/finish.hpp> # define BOOST_PP_LOCAL_S BOOST_PP_LOCAL_SE() # define BOOST_PP_LOCAL_F BOOST_PP_LOCAL_FE() # endif # endif # # if (BOOST_PP_LOCAL_S) > (BOOST_PP_LOCAL_F)//如果第一个元素只小于第二个 # include <boost/preprocessor/iteration/detail/rlocal.hpp> # else//否则执行 # if BOOST_PP_LOCAL_C(0) //# define BOOST_PP_LOCAL_C(n) (BOOST_PP_LOCAL_S) <= n && (BOOST_PP_LOCAL_F) >= n,这是判断是否在这个范围内 BOOST_PP_LOCAL_MACRO(0) //用户自己写的代码。 # endif # if BOOST_PP_LOCAL_C(1) BOOST_PP_LOCAL_MACRO(1) # endif # if BOOST_PP_LOCAL_C(2) BOOST_PP_LOCAL_MACRO(2) # endif # if BOOST_PP_LOCAL_C(3) BOOST_PP_LOCAL_MACRO(3) # endif # if BOOST_PP_LOCAL_C(4) BOOST_PP_LOCAL_MACRO(4) # endif # if BOOST_PP_LOCAL_C(5) BOOST_PP_LOCAL_MACRO(5) # endif # if BOOST_PP_LOCAL_C(6) BOOST_PP_LOCAL_MACRO(6) # endif # if BOOST_PP_LOCAL_C(7) BOOST_PP_LOCAL_MACRO(7) # endif # if BOOST_PP_LOCAL_C(8) BOOST_PP_LOCAL_MACRO(8) # endif # if BOOST_PP_LOCAL_C(9) BOOST_PP_LOCAL_MACRO(9) # endif # if BOOST_PP_LOCAL_C(10) BOOST_PP_LOCAL_MACRO(10) # endif # if BOOST_PP_LOCAL_C(11) BOOST_PP_LOCAL_MACRO(11) # endif # if BOOST_PP_LOCAL_C(12) BOOST_PP_LOCAL_MACRO(12) # endif # if BOOST_PP_LOCAL_C(13) BOOST_PP_LOCAL_MACRO(13) # endif # if BOOST_PP_LOCAL_C(14) BOOST_PP_LOCAL_MACRO(14) # endif # if BOOST_PP_LOCAL_C(15) BOOST_PP_LOCAL_MACRO(15) # endif # if BOOST_PP_LOCAL_C(16) BOOST_PP_LOCAL_MACRO(16) # endif # if BOOST_PP_LOCAL_C(17) BOOST_PP_LOCAL_MACRO(17) # endif # if BOOST_PP_LOCAL_C(18) BOOST_PP_LOCAL_MACRO(18) # endif # if BOOST_PP_LOCAL_C(19) BOOST_PP_LOCAL_MACRO(19) # endif # if BOOST_PP_LOCAL_C(20) BOOST_PP_LOCAL_MACRO(20) # endif # if BOOST_PP_LOCAL_C(21) BOOST_PP_LOCAL_MACRO(21) # endif # if BOOST_PP_LOCAL_C(22) BOOST_PP_LOCAL_MACRO(22) # endif # if BOOST_PP_LOCAL_C(23) BOOST_PP_LOCAL_MACRO(23) # endif # if BOOST_PP_LOCAL_C(24) BOOST_PP_LOCAL_MACRO(24) # endif # if BOOST_PP_LOCAL_C(25) BOOST_PP_LOCAL_MACRO(25) # endif # if BOOST_PP_LOCAL_C(26) BOOST_PP_LOCAL_MACRO(26) # endif # if BOOST_PP_LOCAL_C(27) BOOST_PP_LOCAL_MACRO(27) # endif ....... 内容省略掉 ....... # if BOOST_PP_LOCAL_C(252) BOOST_PP_LOCAL_MACRO(252) # endif # if BOOST_PP_LOCAL_C(253) BOOST_PP_LOCAL_MACRO(253) # endif # if BOOST_PP_LOCAL_C(254) BOOST_PP_LOCAL_MACRO(254) # endif # if BOOST_PP_LOCAL_C(255) BOOST_PP_LOCAL_MACRO(255) # endif # if BOOST_PP_LOCAL_C(256) BOOST_PP_LOCAL_MACRO(256)//从0开始一直到256 # endif # endif # # undef BOOST_PP_LOCAL_LIMITS # # undef BOOST_PP_LOCAL_S # undef BOOST_PP_LOCAL_F # # undef BOOST_PP_LOCAL_MACRO
上面的索引范围还可以倒着写:
如(3,9)写成(9,3)
其它剩下的代码也是倒着写:
# if BOOST_PP_LOCAL_R(256) BOOST_PP_LOCAL_MACRO(256) # endif # if BOOST_PP_LOCAL_R(255) BOOST_PP_LOCAL_MACRO(255) # endif # if BOOST_PP_LOCAL_R(254) BOOST_PP_LOCAL_MACRO(254) # endif # if BOOST_PP_LOCAL_R(253) BOOST_PP_LOCAL_MACRO(253) # endif ............. .............
其中BOOST_PP_LOCAL_R代码:
# define BOOST_PP_LOCAL_R(n) (BOOST_PP_LOCAL_F) <= n && (BOOST_PP_LOCAL_S) >= n
这就是BOOST_PP_LOCAL_ITERATE() 宏的功能。