boost iterator facade and dereference() function

1.9k views Asked by At

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).

1

There are 1 answers

1
Piotr Semenov On BEST ANSWER

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(...); }
    ...
};