Boost logo

Boost :

Subject: Re: [boost] [iterator adaptor] requires default constructor?
From: Neal Becker (ndbecker2_at_[hidden])
Date: 2010-06-11 21:14:53


IIUC, in typical usage, the default constructor will be called.
Here is my example:

template<typename BaseIterator, typename offset_t>
  class cycle_iterator : public
boost::iterator_adaptor<cycle_iterator<BaseIterator, offset_t>,
                                                        BaseIterator
>
 {...

typedef typename boost::iterator_adaptor<cycle_iterator<BaseIterator,
offset_t>,
                                             BaseIterator
> super_t;

    explicit cycle_iterator (BaseIterator const& _b, BaseIterator const& _e,
offset_t offset=0) :
      base(_b),
      size (std::distance (_b, _e)) {
      SetPos (offset);
    }

...
    BaseIterator base;
};

This is a bit confusing. IIUC, the issue is that we did not explicitly
invoke the super_t constructor, so we got the default constructor, which in
turn calls (iterator_adaptor.hpp:280)

      iterator_adaptor() {}

Adding an explicity super_t constructor call fixes it:
    explicit cycle_iterator (BaseIterator const& _b, BaseIterator const& _e,
offset_t offset=0) :
        super_t (_b),
      base(_b),
      size (std::distance (_b, _e)) {
      SetPos (offset);
    }

But is not very obvious.


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