[Home]CPPTM Answers - Exercise 7-7

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

 // (by Ariel Badichi)
 #include <boost/static_assert.hpp>
 #include <boost/mpl/vector_c.hpp>
 #include <boost/mpl/begin.hpp>
 #include <boost/mpl/end.hpp>
 #include <boost/mpl/iterator_range.hpp>
 #include <boost/mpl/iterator_category.hpp>
 #include <boost/mpl/equal.hpp>
 #include <boost/mpl/reverse.hpp>
 #include <boost/mpl/deref.hpp>
 #include <boost/mpl/next.hpp>
 #include <boost/mpl/prior.hpp>

 namespace mpl = boost::mpl;

 template<typename Iter>
 struct reverse_iterator
 {
     typedef Iter base;
     typedef typename mpl::iterator_category<Iter>::type category;
     typedef reverse_iterator type;
 };

 namespace boost
 {
     namespace mpl
     {
         template<typename Iter>
         struct deref<reverse_iterator<Iter> >
             : mpl::deref<typename mpl::prior<Iter>::type> 
         {};

         template<typename Iter>
         struct next<reverse_iterator<Iter> >
             : reverse_iterator<typename mpl::prior<Iter>::type>
         {};

         template<typename Iter>
         struct prior<reverse_iterator<Iter> > 
             : reverse_iterator<typename mpl::next<Iter>::type>
         {};
     }
 }

 template<typename Seq>
 struct reverse_view
     : mpl::iterator_range<
           reverse_iterator<typename mpl::end<Seq>::type>,
           reverse_iterator<typename mpl::begin<Seq>::type>
       >
 {};

 int main()
 {
     typedef mpl::vector_c<int, 1, 2, 3, 4> v;
     typedef mpl::reverse<v>::type rv;

     typedef reverse_view<v> view;
     BOOST_STATIC_ASSERT((mpl::equal<view, rv>::value));
 }

BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited March 27, 2005 12:21 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers