Boost logo

Geometry :

Subject: [ggl] pre-/post-increment op on iterators
From: Bruno Lalande (bruno.lalande)
Date: 2009-04-30 08:56:28


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? 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. So:

value_type operator*() const
{
   value_type s;
   s.first = *m_prev;
   s.second = *m_it;

   return m_cache;
}

[...]

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.

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

Bruno


Geometry list run by mateusz at loskot.net