boost iterator facade and dereference() function

boost, boost-iterators, iterator-facade

Solution

Yes, thing you want is possible. Please, take a look at `boost/iterator_facade.hpp` (example is for Boost lib of version 1.49.0 but it is ok for its new distributions also):

  template <
    class Derived
  , class Value
  , class CategoryOrTraversal
  , class Reference   = Value&
  , class Difference  = std::ptrdiff_t
>
class iterator_facade

Template argument `Reference` is the key. You should just specify `Reference` when deriving from `boost::iterator_facade`. For example, your code can look like as the following:

template<typename value_type>
class custom_iterator
    :    public boost::iterator_facade<
             custom_iterator<value_type>,
             value_type,
             boost::forward_traversal_tag,
             value_type
         >
{
    ...
    value_type dereference() const{ return value_type(...); }
    ...
};

Problem

I'm trying to create an iterator which only can dereference to real value types, not to references. Is this possible using `boost::iterator_facade`, or does it require me to have values that can be returned by adress\reference. To be more specfic, my iterator returns a `std::pair` of references, which means my iterators `value_type` is not stored anywhere, but created on the fly on dereferencing (like `std::map::iterator`).

Original source