Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-07-28 16:16:57


Helmut Zeisel wrote:
> > As related issue, it is possible to write functions that return an
> > object that has a container-like interface (has begin() and end()),
> > but refers to the elements of the container differently.
>
> Do you mean something like
>
> template<typename Iterator> struct range
> {
> Iterator m_begin;
> Iterator m_end;
> range(Iterator begin, Iterator end): m_begin(begin), m_end(end) {}
> Iterator begin() const {return m_begin;}
> Iterator end() const {return m_end;}
> };

Basically, that's what we have here at work too, only it's called
'iterator_range' :). Here are some relevant parts (just to show the
differences):

template<typename Iterator>
struct iterator_range
{
 private:
    typedef std::iterator_traits<Iterator> traits;

 public:
    typedef typename traits::value_type value_type;
    typedef Iterator iterator;
    typedef Iterator const_iterator;
    typedef typename traits::pointer pointer;
    typedef typename traits::reference reference;
    typedef value_type const& const_reference;
    typedef typename traits::difference_type difference_type;

 public:
    template<class Sequence> iterator_range(Sequence const& s)
        : m_begin(mtn::begin(s))
        , m_end(mtn::end(s))
    {
    }

    // borrowed from boost::half_open_range:
    // Implicit conversion from std::pair<Iterator,Iterator> allows us
    // to accept the results of std::equal_range(), for example.
    iterator_range(std::pair<Iterator, Iterator> const& other);
    self& operator=(std::pair<Iterator, Iterator> const& other);

    // the rest is the same as in your class
};

Aleksey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk