Boost logo

Boost :

Subject: [boost] [iterator] iterator_facade reference type problem
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2010-01-14 06:30:46


Dear All,

I have a problem with boost::iterator_facade. Here's an example: say
I'm implementing an associative container that stores a sorted vector
of keys and a separate sorted vector of values:

template <typename KEY, typename VALUE>
class vecmap {
   std::vector<KEY> keys;
   std::vector<VALUE> values;

I can make an iterator for this that stores one iterator for each of
the vectors:

   class iterator:
     public boost::iterator_facade<iterator,
                                   std::pair<KEY,VALUE>,
                                   boost::bidirectional_traversal_tag>
   {
     std::vector<KEY>::iterator ki;
     std::vector<VALUE>::iterator vi;
     void increment() { ++ki; ++vi; }
     void decrement() { --ki; --vi; }
     ....

But what do I do about the reference type? I can't use std::pair<const
KEY,VALUE>& because I'm not storing the keys and values in that way.
It's almost possible to use std::pair<KEY,VALUE> for a const_iterator,
but not if you want to allow the user to change the value through the
reference. What I think I want to use is std::pair<KEY,VALUE&> i.e.
the key is returned by value and only the value is returned by
reference. But when I try this I get various cryptic errors about
references to references and mpl things.

So, has anyone else tried to do something like this? I wonder if
boost::reference could be part of the solution.

Thanks for any suggestions,

Phil.


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