Boost logo

Geometry :

Subject: [ggl] pre-/post-increment op on iterators
From: Mateusz Loskot (mateusz)
Date: 2009-04-30 09:56:20


Bruno Lalande wrote:
> Hi,
>
> As long as the returned reference is const (which is indeed
> preferable), wouldn't it be better to have the operator* returning a
> temporary rather than a member?

Iterators of input category may dereference returning element by-value.
However, it's unclear what to return from operator-> , pointer to
temporary? It looks like we would need to return such pointer
wrapped somehow and preserving pointer semantic.

This problem is mentioned in Abrahams' & Siek's paper here

http://boost.cppll.jp/HEAD/libs/utility/iterator_adaptors.ppt

Another issue is that returning by value causes number of allocations.
Each dereference of iterator/adapter = construction of new segment
object. But with cache, we just fill existing object.

> The C++ standard allows a const
> reference to be initialized from a temporary and guaranties that the
> temporary's lifetime will be the scope of the reference.

That's true.

> So:
>
> value_type operator*() const
> {
> value_type s;
> s.first = *m_prev;
> s.second = *m_it;
>
> return m_cache;
> }

Isn't the operator above returning by-value, not by-reference?

> [...]
>
> segment_iterator<my_iterator> i = some_iterator;
> const segment& s = *i;
> do_something_with(s.first); // OK, s is still alive.
>
> This way we don't carry segments uselessly.

True, if we assume permanent constness of such iterator/segments

> The problem is that I don't know is returning a value_type is
> something allowed for an iterator's operator*.

For input iterators, it is, but it has drawbacks and problems as
mentioned above.

To summary, we've two options. Let's discuss which one to use.

Best regards,

-- 
Mateusz Loskot, http://mateusz.loskot.net

Geometry list run by mateusz at loskot.net