Boost logo

Boost :

From: Thorsten Ottosen (tottosen_at_[hidden])
Date: 2006-02-18 18:04:31


Dear All,

I've changed the inteface of ptr_map's iterators due to some user
requests. The old interface supported

typedef ptr_map<string,int> map_t;
map_t m;
m[ "foo" ] = 4; // insert pair
m[ "bar" ] = 5; // ditto
...
for( map_t::iterator i = m.begin(); i != m.end(); ++i )
{
          *i += 42; // add 42 to each value
          cout << "value=" << *i << ", key=" << i.key() << "n";
}

The same code can now be written (#1)

for( map_t::iterator i = m.begin(); i != m.end(); ++i )
{
          *i->second += 42; // add 42 to each value
          cout << "value=" << *i->second << ", key=" << i->first << "n";
}

or (#2)

for( map_t::iterator i = m.begin(); i != m.end(); ++i )
{
          i.value() += 42; // add 42 to each value
          cout << "value=" << i.value() << ", key=" << i.key() << "n";
}

or (#3)

for( map_t::iterator i = m.begin(); i != m.end(); ++i )
{
          *(*i).second += 42; // add 42 to each value
          cout << "value=" << *(*i).second << ", key=" << (*i).first << "n";
}

There are two issues here:

1. AFIICT it is not possible to let operator->() return a tuple of
references s.t. ->second would yeild T& instead of T*. Reason: the
pointer might be null.

2. Operator*() now returns a tuple [key,T*]; this is necessary because
you may need access to both the key inside e.g predicates for
algorithms. Operator->() also returns the same tuple, so that you can
make the familiar loop in #1. The question is #1 is necessary/wanted
given that it removes the current behavior where operator->() returns
a T* s.t. you can more easily access the mapped object. Would it
be preferable to keep operator-<() with a T* return type?

Thanks for your feedback

-Thorsten


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