mpl::vector有三种不同实现方式,一种能在GCC 下面使用,它是通过继承模板参数来实现的。
下面这些宏,不是实现的细节,而是不同实现方式的主模版。
# define BOOST_MPL_LIMIT_VECTOR_SIZE 20 # define AUX778076_SEQUENCE_NAME vector # define AUX778076_SEQUENCE_LIMIT BOOST_MPL_LIMIT_VECTOR_SIZE namespace boost { namespace mpl { #if !defined(AUX778076_SEQUENCE_BASE_NAME) # define AUX778076_SEQUENCE_BASE_NAME AUX778076_SEQUENCE_NAME // AUX778076_SEQUENCE_BASE_NAME vector #endif #if !defined(AUX778076_SEQUENCE_INTEGRAL_WRAPPER) # define AUX778076_SEQUENCE_PARAM_NAME T # define AUX778076_SEQUENCE_TEMPLATE_PARAM typename T # define AUX778076_SEQUENCE_DEFAULT na //BOOST_PP_CAT 起连接作用,如果传入的参数为0,1,2 //展开后的形式为: vector0, vecotr1, vector2 # define AUX778076_SEQUENCE_NAME_N(n) / BOOST_PP_CAT(AUX778076_SEQUENCE_BASE_NAME,n) / /**/ //下面这项展开的形式为: // typename T0, typename T1, typename T2, typename T3 ...... typename T19 共20项 # define AUX778076_SEQUENCE_PARAMS() / BOOST_PP_ENUM_PARAMS( / AUX778076_SEQUENCE_LIMIT / , AUX778076_SEQUENCE_TEMPLATE_PARAM / ) / /**/ //展开的形式为: //T0, T1, T2, T3 ...... T19 共20项 # define AUX778076_SEQUENCE_ARGS() / BOOST_PP_ENUM_PARAMS( / AUX778076_SEQUENCE_LIMIT / , T / ) / /**/ //展开的形式: AUX778076_SEQUENCE_DEFAULT 为 na ,na代表类型,起标识作用。 //typename T0 = na, typename T1 = na, typename T2 = na, typename T3 = na //typename T4 = na, typename T5 = na ...... typename T19 = na # define AUX778076_SEQUENCE_DEFAULT_PARAMS() / BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT( / AUX778076_SEQUENCE_LIMIT / , AUX778076_SEQUENCE_TEMPLATE_PARAM / , AUX778076_SEQUENCE_DEFAULT / ) / /**/ //这是一个指定的展开形式:假如传入的参数为3,则展开形式: //typename T0, typename T1, typename T2 # define AUX778076_SEQUENCE_N_PARAMS(n) / BOOST_PP_ENUM_PARAMS(n, AUX778076_SEQUENCE_TEMPLATE_PARAM) / /**/ //展开的形式为: //T0, T1, T2 ... Tn # define AUX778076_SEQUENCE_N_ARGS(n) / BOOST_PP_ENUM_PARAMS(n, T) / /**/ //# define BOOST_PP_TUPLE_ELEM_3_2(a, b, c) c //BOOST_PP_ENUM(18, BOOST_PP_TUPLE_ELEM_3_2, na) // # define AUX778076_SEQUENCE_N_PARTIAL_SPEC_ARGS(n) / BOOST_PP_ENUM_PARAMS(n, T) / BOOST_PP_COMMA_IF(n) / BOOST_PP_ENUM( / BOOST_PP_SUB_D(1,AUX778076_SEQUENCE_LIMIT,n) / , BOOST_PP_TUPLE_ELEM_3_2 / , AUX778076_SEQUENCE_DEFAULT / ) / /**/ #else // AUX778076_SEQUENCE_INTEGRAL_WRAPPER # define BOOST_MPL_AUX_NTTP_DECL(T, x) T x /**/ # define AUX778076_SEQUENCE_PARAM_NAME C # define AUX778076_SEQUENCE_TEMPLATE_PARAM BOOST_MPL_AUX_NTTP_DECL(long, C) # define AUX778076_SEQUENCE_DEFAULT LONG_MAX // typename T, long C0, longC1 ...... long C19 # define AUX778076_SEQUENCE_PARAMS() / typename T, BOOST_PP_ENUM_PARAMS( / AUX778076_SEQUENCE_LIMIT / , AUX778076_SEQUENCE_TEMPLATE_PARAM / ) / /**/ // T, C0, C1, C2 ...... C19 # define AUX778076_SEQUENCE_ARGS() / T, BOOST_PP_ENUM_PARAMS( / AUX778076_SEQUENCE_LIMIT / , C / ) / /**/ // typename T, long C0 = LONG_MAX, long C1 = LONG_MAX, long C2 = LONG_MAX ...... long C19 = LONG_MAX # define AUX778076_SEQUENCE_DEFAULT_PARAMS() / typename T, / BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT( / AUX778076_SEQUENCE_LIMIT / , AUX778076_SEQUENCE_TEMPLATE_PARAM / , AUX778076_SEQUENCE_DEFAULT / ) / /**/ // typename T, long C0, long C1, long C2, ...... long Cn # define AUX778076_SEQUENCE_N_PARAMS(n) / typename T BOOST_PP_COMMA_IF(n) / BOOST_PP_ENUM_PARAMS(n, AUX778076_SEQUENCE_TEMPLATE_PARAM) / /**/ # if !defined(AUX778076_SEQUENCE_CONVERT_CN_TO) # define AUX778076_SEQUENCE_CONVERT_CN_TO(z,n,TARGET) BOOST_PP_CAT(C,n) # endif // T, n, Cn, T # define AUX778076_SEQUENCE_N_ARGS(n) / T BOOST_PP_COMMA_IF(n) / BOOST_PP_ENUM(n,AUX778076_SEQUENCE_CONVERT_CN_TO,T) / /**/ # define AUX778076_SEQUENCE_N_PARTIAL_SPEC_ARGS(n) / T, BOOST_PP_ENUM_PARAMS(n, C) / BOOST_PP_COMMA_IF(n) / BOOST_PP_ENUM( / BOOST_PP_SUB_D(1,AUX778076_SEQUENCE_LIMIT,n) / , BOOST_PP_TUPLE_ELEM_3_2 / , AUX778076_SEQUENCE_DEFAULT / ) / /**/ #endif // AUX778076_SEQUENCE_INTEGRAL_WRAPPER #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) // forward declaration //一个推导的形式为: //template<typename T0 = na, typename T1 = na, typename T2 = na, typename T3 = na... typename T19 = na> //struct vector; template< AUX778076_SEQUENCE_DEFAULT_PARAMS() > struct AUX778076_SEQUENCE_NAME; #else // template <int N> // struct vector_chooser; namespace aux { template< BOOST_MPL_AUX_NTTP_DECL(int, N) > struct BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_chooser); } #endif #define BOOST_PP_ITERATION_PARAMS_1 / (3,(0, AUX778076_SEQUENCE_LIMIT, <boost/mpl/aux_/sequence_wrapper.hpp>)) #include BOOST_PP_ITERATE() // real C++ version is already taken care of #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) namespace aux { // ???_count_args #define AUX778076_COUNT_ARGS_PREFIX AUX778076_SEQUENCE_NAME #define AUX778076_COUNT_ARGS_DEFAULT AUX778076_SEQUENCE_DEFAULT #define AUX778076_COUNT_ARGS_PARAM_NAME AUX778076_SEQUENCE_PARAM_NAME #define AUX778076_COUNT_ARGS_TEMPLATE_PARAM AUX778076_SEQUENCE_TEMPLATE_PARAM #define AUX778076_COUNT_ARGS_ARITY AUX778076_SEQUENCE_LIMIT #define AUX778076_COUNT_ARGS_USE_STANDARD_PP_PRIMITIVES #include <boost/mpl/aux_/count_args.hpp> //template <typename T0, typename T1, typename T2, typename T3 ...... typename T19 > //struct vector_impl //{ // vector_count_args<T0, T1, T2, ...... T19> arg_num_; // typedef typename vector_chooser<arg_num_::vaule> // ::template result<T0, T1, T2, T3 ...... T19>::type type; //}; template< AUX778076_SEQUENCE_PARAMS() > struct BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_impl) { typedef aux::BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_count_args)< BOOST_PP_ENUM_PARAMS(AUX778076_SEQUENCE_LIMIT, AUX778076_SEQUENCE_PARAM_NAME) > arg_num_; typedef typename aux::BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_chooser)< arg_num_::value > ::template result_< AUX778076_SEQUENCE_ARGS() >::type type; }; } // namespace aux // template<typename T0, typename T1, typename T2, typename T3 ...... typename T19> // struct vector : vector_impl<T0, T1, T2, T3 ...... T19>::type //{ // typedef typename vector_impl<T0, T1, T2, T3 ...... T19>::type type; // //}; template< AUX778076_SEQUENCE_DEFAULT_PARAMS() > struct AUX778076_SEQUENCE_NAME : aux::BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_impl)< AUX778076_SEQUENCE_ARGS() >::type { typedef typename aux::BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_impl)< AUX778076_SEQUENCE_ARGS() >::type type; }; #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION # undef AUX778076_SEQUENCE_N_PARTIAL_SPEC_ARGS # undef AUX778076_SEQUENCE_N_ARGS # undef AUX778076_SEQUENCE_CONVERT_CN_TO # undef AUX778076_SEQUENCE_N_PARAMS # undef AUX778076_SEQUENCE_DEFAULT_PARAMS # undef AUX778076_SEQUENCE_ARGS # undef AUX778076_SEQUENCE_PARAMS # undef AUX778076_SEQUENCE_NAME_N # undef AUX778076_SEQUENCE_DEFAULT # undef AUX778076_SEQUENCE_TEMPLATE_PARAM # undef AUX778076_SEQUENCE_PARAM_NAME # undef AUX778076_SEQUENCE_LIMIT # undef AUX778076_SEQUENCE_BASE_NAME # undef AUX778076_SEQUENCE_NAME # undef AUX778076_SEQUENCE_INTEGRAL_WRAPPER }} ///// iteration #else #define i_ BOOST_PP_FRAME_ITERATION(1) # if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) #if i_ == AUX778076_SEQUENCE_LIMIT /// primary template (not a specialization!) //宏仅仅是展开,并不会进行模板特化。 假设传入的参数为: N // template <typename T0, typename T1, typename T2, typename T3 , ... , typename TN> // struct vector : vectorN<T0,T1,T2,T3,...,TN> // { // typedef typename vectorN<T0,T1,T2,T3,...,TN>::type type; // // }; template< AUX778076_SEQUENCE_N_PARAMS(i_) > struct AUX778076_SEQUENCE_NAME : AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) > { typedef typename AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) >::type type; }; #else //template<typename T0, typename T1, typename T2, typename T3 , ... , typename TN> //struct vector<T0,T1,T2,T3,...,TN>: vectorN<T0,T1,T2,T3,...,TN> //#if i_ > 0 || defined(AUX778076_SEQUENCE_INTEGRAL_WRAPPER) // typedef typename vectorN<T0,T1,T2,T3,...,TN>::type type; //#else // typedef vectorN<T0,T1,T2,T3,...,TN>::type type; //#endif //}; template< AUX778076_SEQUENCE_N_PARAMS(i_) > struct AUX778076_SEQUENCE_NAME< AUX778076_SEQUENCE_N_PARTIAL_SPEC_ARGS(i_) > : AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) > { #if i_ > 0 || defined(AUX778076_SEQUENCE_INTEGRAL_WRAPPER) typedef typename AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) >::type type; #else typedef AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) >::type type; #endif }; #endif // i_ == AUX778076_SEQUENCE_LIMIT # else namespace aux { // template<> // struct vector_chooser<i> // { // template <typename T0, typename T1, typename T2, typename T3 ...... typename T19> // struct result_ // { // #if i_ > 0 || defined(AUX778076_SEQUENCE_INTEGRAL_WRAPPER) // typedef typename vectori_<T0,T1,...,Ti_>::type type; // #else // typedef vectori_<T0,T1,...,Ti>::type type; // #endif // }; //}; struct BOOST_PP_CAT(AUX778076_SEQUENCE_NAME,_chooser)<i_> { template< AUX778076_SEQUENCE_PARAMS() > struct result_ { #if i_ > 0 || defined(AUX778076_SEQUENCE_INTEGRAL_WRAPPER) typedef typename AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) >::type type; #else typedef AUX778076_SEQUENCE_NAME_N(i_)< AUX778076_SEQUENCE_N_ARGS(i_) >::type type; #endif }; }; } // namespace aux # endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #undef i_ #endif // BOOST_PP_IS_ITERATING