| Home | Libraries | People | FAQ | More

##### Method 3.1: Implement a Range Adaptor without arguments

To implement a Range Adaptor without arguments (e.g. reversed) you need to:

1. Provide a range for your return type, for example:

       #include <boost/range/iterator_range.hpp>
       #include <boost/iterator/reverse_iterator.hpp>

       template< typename R >
       struct reverse_range :
           boost::iterator_range<
               boost::reverse_iterator<
                   typename boost::range_iterator<R>::type> >
       {
       private:
           typedef boost::iterator_range<
               boost::reverse_iterator<
                   typename boost::range_iterator<R>::type> > base;

       public:
           typedef boost::reverse_iterator<
               typename boost::range_iterator<R>::type > iterator;

           reverse_range(R& r)
               : base(iterator(boost::end(r)), iterator(boost::begin(r)))
           { }
       };

2. Provide a tag to uniquely identify your adaptor in the `operator|` function overload set

       namespace detail {
           struct reverse_forwarder {};
       }

3. Implement `operator|`

       template< class BidirectionalRng >
       inline reverse_range<BidirectionalRng>
       operator|( BidirectionalRng& r, detail::reverse_forwarder )
       {
       	return reverse_range<BidirectionalRng>( r );
       }

       template< class BidirectionalRng >
       inline reverse_range<const BidirectionalRng>
       operator|( const BidirectionalRng& r, detail::reverse_forwarder )
       {
       	return reverse_range<const BidirectionalRng>( r );
       }

4. Declare the adaptor itself (it is a variable of the tag type).

       namespace
       {
           const detail::reverse_forwarder reversed = detail::reverse_forwarder();
       }