Section 5.3.  Iterators
Team LiB
Previous Section Next Section

5.3. Iterators

As with STL iterators, the most fundamental service provided by MPL iterators is access to the sequence element to which they refer. To dereference a compile-time iterator, we can't simply apply the prefix * operator: runtime operator overloading is unavailable at compile time. Instead, the MPL provides us with an aptly named deref metafunction that takes an iterator and returns the referenced element.



    typedef mpl::vector<char,short,int,long,float,double> types;





    // locate the position of long in types


    typedef mpl::find<types,long>::type long_pos;





    // dereference the iterator


    typedef mpl::deref<long_pos>::type x;





    // check that we have the expected result


    BOOST_STATIC_ASSERT((boost::is_same<x,long>::value));



An iterator can also provide access to adjacent positions in a sequence, or traversal. In Chapter 4 we described the mpl::next and mpl::prior metafunctions, which produce an incremented or decremented copy of their integral argument. These primitives apply equally well to iterators:



    typedef mpl::next<long_pos>::type float_pos;


    BOOST_STATIC_ASSERT((


        boost::is_same<


            mpl::deref<float_pos>::type


          , float


        >::value


    ));



    Team LiB
    Previous Section Next Section