Boost logo

Boost :

From: Bill Buklis (boostuser_at_[hidden])
Date: 2006-02-20 14:12:42


If at all reasonably possible I would prefer to keep operator-> to return
T*. It certainly makes it easy to write something like "mapit->member". And
it looks natural as it treats the iterator like a smart object pointer. The
only problem was accessing the key from a predicate function.

>From #3 are you suggesting that operator-> and operator* return different
types. i.e. Instead of T*, and T& respectively, but T* and tuple<key,T*>?
That seems like a reasonable idea. It might look a little odd at first, but
would certainly get the job done and would still be easy to get used to.

The only point of #1 is so that the iterators behave exactly like std::map
iterators, right? One the one hand, that would be nice for consistency with
std::map, but then this is a different class with slightly different uses,
so I personally don't mind if it has slightly different characteristics. And
the usefulness of operator-> as T* outweighs the need for identical
behavior, IMO.

More and more as I think about it, I like this approach (i.e. operator-> as
T* and operator* as tuple<key,T*>). Method #1 is not supported, but it
doesn't seem that important to me anyway. You would still be able to
accomplish the same thing with i.key() and i.value().

-- Bill --

"Thorsten Ottosen" <tottosen_at_[hidden]> wrote in message
news:dt8968$cvg$1_at_sea.gmane.org...
> 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
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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