boost::mpl::vector(MSVC)源码分析

代码看起来很直接,没有前面宏定义那么复杂。但头文件的相互包括比较复杂。
如果仅仅是利用VS助手来寻找代码,出错的可能性非常之大。
对于模板元的调试非常的特殊。正确的代码是不能调试的,而是故意把它写错:比如,
vector1只能存放一个元素,那么故意写成两个元素,超出了界限,编译器自然会报错。
这样可以知道编译器调用的是什么地方的代码了。否则调用的是什么地方的代码都不知道(花了大量的时间来寻找代码)。
有些模板仅仅是声明没有定义。这样只是起一个标记作用,用类型来标记。这个vector同那个vector是不同的。比如:vector_tag<1>和vector_tag<2>就是不同,他们代表不同的类型。它的差异仅仅用类型就可以做到。网上仅有一篇博客说是定义,这儿显然是不对。如果是定义可以生成对像,我试了aux::vector_tag不能生成对像。
// Copyright Aleksey Gurtovoy 2000-2004
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// Preprocessed version of "boost/mpl/vector/vector10.hpp" header
// -- DO NOT modify by hand!
#   define BOOST_MPL_AUX_NTTP_DECL(T, x) T x /**/
struct v_iter_tag;//仅仅是一个标志
struct integral_c_tag { BOOST_STATIC_CONSTANT(int, value = 0); };
template< int  N>
struct int_
{
    enum { value = N };
    typedef  int   type;
    typedef  int_  value_type;
    typedef integral_c_tag tag;
    typedef int_ next;
    typedef int_ prior;
    // enables uniform function call syntax for families of overloaded
    // functions that return objects of both arithmetic ('int', 'long',
    // 'double', etc.) and wrapped integral types (for an example, see
    // "mpl/example/power.cpp")
    operator int() const { return static_cast(this->value); }
};
namespace boost { namespace mpl {
//起标记作用。同STL的代码这点来说很相似。
    struct forward_iterator_tag       : int_<0> { typedef forward_iterator_tag type; };
    struct bidirectional_iterator_tag : int_<1> { typedef bidirectional_iterator_tag type; };
    struct random_access_iterator_tag : int_<2> { typedef random_access_iterator_tag type; };
}}
#   define BOOST_MPL_AUX_COMMON_NAME_WKND(name) /**/
#define BOOST_MPL_AUX_NA_PARAM(param) param = na
namespace boost { namespace mpl {
        BOOST_MPL_AUX_COMMON_NAME_WKND(next)
        BOOST_MPL_AUX_COMMON_NAME_WKND(prior)
        template<
        typename BOOST_MPL_AUX_NA_PARAM(T)// typename T = na
        >
        struct next
        {
            typedef typename T::next type;
            BOOST_MPL_AUX_LAMBDA_SUPPORT(1,next,(T))
        };
        template<
            typename BOOST_MPL_AUX_NA_PARAM(T)
        >
        struct prior
        {
            typedef typename T::prior type;
            BOOST_MPL_AUX_LAMBDA_SUPPORT(1,prior,(T))
        };
        BOOST_MPL_AUX_NA_SPEC(1, next)
        BOOST_MPL_AUX_NA_SPEC(1, prior)
}}
namespace boost { namespace mpl {
    //#   define BOOST_MPL_AUX_NTTP_DECL(T, x) T x /**/
    template<
        typename Vector
        ,long n_
    >
    struct v_iter
    {
        typedef aux::v_iter_tag tag; //标记
        typedef random_access_iterator_tag category;//迭代器类型
        typedef typename v_at::type type;//这是在vector中特化的版本。
        typedef Vector vector_;
        typedef mpl::long_ pos;
#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
        enum {
            next_ = n_ + 1
            , prior_ = n_ - 1
            , pos_ = n_
        };
        typedef v_iter next; //实际上重新生成一个v_iter
        typedef v_iter prior;
#endif
    };
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
    template<
        typename Vector
        , BOOST_MPL_AUX_NTTP_DECL(long, n_)//long n_
    >
    struct next< v_iter >//这儿来看,显然不主模版。因为这儿只有一个参数v_iter,是一个特化版本。
    {
        typedef v_iter type;//重新生成一个v_iter
    };
    template<
        typename Vector
        , BOOST_MPL_AUX_NTTP_DECL(long, n_)
    >
    struct prior< v_iter >//同上面一样。
    {
        typedef v_iter type;//重新生成一个v_iter
    };
#   define BOOST_MPL_AUX_NESTED_VALUE_WKND(T, C) /
    BOOST_MPL_AUX_VALUE_WKND(C)::value /
    /**/
    #   define BOOST_MPL_AUX_VALUE_WKND(C) C
#endif
}}
#endif // BOOST_MPL_AUX_VECTOR_ITERATOR_HPP_INCLUDED
namespace boost { namespace mpl {
    //特化版本,在其他地方声明
    template< typename V >
    struct v_at< V,0 >
    {
        typedef typename V::item0 type;//V代表的是vector,item0就是第一个元素值。
    };
    template<
        typename T0
    >
    struct vector1
    {  //这仅是一个标记,只对它进行了声明,没有对它进行定义。代表一个特定的类型。
        typedef aux::vector_tag<1> tag;
        typedef vector1 type;
        typedef T0 item0;//vector1存放的元素T0
        typedef void_ item1;//
        typedef T0 back;
        typedef v_iter< type,0 > begin; //
        typedef v_iter< type,1 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<0> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector1 type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<1> >
    {
        template< typename Vector > struct apply
        {
            typedef vector0<  //对于vector1来说,pop出一个元素之后,就没有任何元素了。
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<0> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector1< //同样重新生成一个vector1,模板元是不能改变其元素值。
                             //要改变只得重新生成一个新的vector。
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<1> >
    {
        template< typename Vector > struct apply
        {
            typedef vector0 type;
        };
    };
    template< typename V >
    struct v_at< V,1 >
    {
        typedef typename V::item1 type;//vector的第二个元素值,比较直接。
    };
//下面的特化版本同上面的非常类似。而代码写得很清晰,而又直接。
    template<
        typename T0, typename T1
    >
    struct vector2
    {
        typedef aux::vector_tag<2> tag;
        typedef vector2 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef void_ item2;
        typedef T1 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,2 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<1> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector2<
                T
                ,
                typename Vector::item0
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<2> >
    {
        template< typename Vector > struct apply
        {
            typedef vector1<
                typename Vector::item1
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<1> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector2<
                typename Vector::item0
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<2> >
    {
        template< typename Vector > struct apply
        {
            typedef vector1<
                typename Vector::item0
            > type;
        };
    };
    template< typename V >
    struct v_at< V,2 >
    {
        typedef typename V::item2 type;
    };
    template<
        typename T0, typename T1, typename T2
    >
    struct vector3
    {
        typedef aux::vector_tag<3> tag;
        typedef vector3 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef void_ item3;
        typedef T2 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,3 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<2> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector3<
                T
                ,
                typename Vector::item0, typename Vector::item1
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<3> >
    {
        template< typename Vector > struct apply
        {
            typedef vector2<
                typename Vector::item1, typename Vector::item2
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<2> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector3<
                typename Vector::item0, typename Vector::item1
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<3> >
    {
        template< typename Vector > struct apply
        {
            typedef vector2<
                typename Vector::item0, typename Vector::item1
            > type;
        };
    };
    template< typename V >
    struct v_at< V,3 >
    {
        typedef typename V::item3 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3
    >
    struct vector4
    {
        typedef aux::vector_tag<4> tag;
        typedef vector4 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef void_ item4;
        typedef T3 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,4 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<3> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector4<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<4> >
    {
        template< typename Vector > struct apply
        {
            typedef vector3<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<3> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector4<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<4> >
    {
        template< typename Vector > struct apply
        {
            typedef vector3<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2
            > type;
        };
    };
    template< typename V >
    struct v_at< V,4 >
    {
        typedef typename V::item4 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3, typename T4
    >
    struct vector5
    {
        typedef aux::vector_tag<5> tag;
        typedef vector5 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef T4 item4;
        typedef void_ item5;
        typedef T4 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,5 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<4> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector5<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<5> >
    {
        template< typename Vector > struct apply
        {
            typedef vector4<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3, typename Vector::item4
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<4> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector5<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<5> >
    {
        template< typename Vector > struct apply
        {
            typedef vector4<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
            > type;
        };
    };
    template< typename V >
    struct v_at< V,5 >
    {
        typedef typename V::item5 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3, typename T4
        , typename T5
    >
    struct vector6
    {
        typedef aux::vector_tag<6> tag;
        typedef vector6 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef T4 item4;
        typedef T5 item5;
        typedef void_ item6;
        typedef T5 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,6 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<5> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector6<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<6> >
    {
        template< typename Vector > struct apply
        {
            typedef vector5<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3, typename Vector::item4
                , typename Vector::item5
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<5> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector6<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<6> >
    {
        template< typename Vector > struct apply
        {
            typedef vector5<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4
            > type;
        };
    };
    template< typename V >
    struct v_at< V,6 >
    {
        typedef typename V::item6 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3, typename T4
        , typename T5, typename T6
    >
    struct vector7
    {
        typedef aux::vector_tag<7> tag;
        typedef vector7 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef T4 item4;
        typedef T5 item5;
        typedef T6 item6;
        typedef void_ item7;
        typedef T6 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,7 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<6> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector7<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<7> >
    {
        template< typename Vector > struct apply
        {
            typedef vector6<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3, typename Vector::item4
                , typename Vector::item5, typename Vector::item6
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<6> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector7<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<7> >
    {
        template< typename Vector > struct apply
        {
            typedef vector6<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
            > type;
        };
    };
    template< typename V >
    struct v_at< V,7 >
    {
        typedef typename V::item7 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3, typename T4
        , typename T5, typename T6, typename T7
    >
    struct vector8
    {
        typedef aux::vector_tag<8> tag;
        typedef vector8 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef T4 item4;
        typedef T5 item5;
        typedef T6 item6;
        typedef T7 item7;
        typedef void_ item8;
        typedef T7 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,8 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<7> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector8<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<8> >
    {
        template< typename Vector > struct apply
        {
            typedef vector7<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3, typename Vector::item4
                , typename Vector::item5, typename Vector::item6
                , typename Vector::item7
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<7> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector8<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<8> >
    {
        template< typename Vector > struct apply
        {
            typedef vector7<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6
            > type;
        };
    };
    template< typename V >
    struct v_at< V,8 >
    {
        typedef typename V::item8 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3, typename T4
        , typename T5, typename T6, typename T7, typename T8
    >
    struct vector9
    {
        typedef aux::vector_tag<9> tag;
        typedef vector9 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef T4 item4;
        typedef T5 item5;
        typedef T6 item6;
        typedef T7 item7;
        typedef T8 item8;
        typedef void_ item9;
        typedef T8 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,9 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<8> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector9<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6, typename Vector::item7
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<9> >
    {
        template< typename Vector > struct apply
        {
            typedef vector8<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3, typename Vector::item4
                , typename Vector::item5, typename Vector::item6
                , typename Vector::item7, typename Vector::item8
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<8> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector9<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6, typename Vector::item7
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<9> >
    {
        template< typename Vector > struct apply
        {
            typedef vector8<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6, typename Vector::item7
            > type;
        };
    };
    template< typename V >
    struct v_at< V,9 >
    {
        typedef typename V::item9 type;
    };
    template<
        typename T0, typename T1, typename T2, typename T3, typename T4
        , typename T5, typename T6, typename T7, typename T8, typename T9
    >
    struct vector10
    {   //vector_tag只有声明,没有定义。在这儿表示类型,不需要定义,只是一个标记。
        typedef aux::vector_tag<10> tag;
        typedef vector10 type;
        typedef T0 item0;
        typedef T1 item1;
        typedef T2 item2;
        typedef T3 item3;
        typedef T4 item4;
        typedef T5 item5;
        typedef T6 item6;
        typedef T7 item7;
        typedef T8 item8;
        typedef T9 item9;
        typedef void_ item10;
        typedef T9 back;
        typedef v_iter< type,0 > begin;
        typedef v_iter< type,10 > end;
    };
    template<>
    struct push_front_impl< aux::vector_tag<9> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector10<
                T
                ,
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6, typename Vector::item7
                , typename Vector::item8
            > type;
        };
    };
    template<>
    struct pop_front_impl< aux::vector_tag<10> >
    {
        template< typename Vector > struct apply
        {
            typedef vector9<
                typename Vector::item1, typename Vector::item2
                , typename Vector::item3, typename Vector::item4
                , typename Vector::item5, typename Vector::item6
                , typename Vector::item7, typename Vector::item8
                , typename Vector::item9
            > type;
        };
    };
    template<>
    struct push_back_impl< aux::vector_tag<9> >
    {
        template< typename Vector, typename T > struct apply
        {
            typedef vector10<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6, typename Vector::item7
                , typename Vector::item8
                ,
                T
            > type;
        };
    };
    template<>
    struct pop_back_impl< aux::vector_tag<10> >
    {
        template< typename Vector > struct apply
        {
            typedef vector9<
                typename Vector::item0, typename Vector::item1
                , typename Vector::item2, typename Vector::item3
                , typename Vector::item4, typename Vector::item5
                , typename Vector::item6, typename Vector::item7
                , typename Vector::item8
            > type;
        };
    };
    template< typename V >
    struct v_at< V,10 >
    {
        typedef typename V::item10 type;
    };
}}

 

你可能感兴趣的:(C++&&算法&&设计模式)