Boost logo

Boost :

Subject: Re: [boost] [iterator] reverse_iterator<counting_iterator<T>>
From: Jeffrey Hellrung (jhellrung_at_[hidden])
Date: 2010-04-13 10:37:15


Stewart, Robert wrote:
> Arno Schödl wrote:
>> "Stewart, Robert" <Robert.Stewart_at_[hidden]
>> boost::prior(...) internally makes a copy of its iterator
>> argument, decrements it and returns it by value, so far so
>> good. counting_iterator::operator* then returns a reference
>> into this (temporary) iterator. Returning from
>> reverse_iterator::dereference() destroys the temporary
>> iterator, but still returns the reference pointing into it.
>
> boost::prior's T is deduced as a reference type. The copy is of a reference. There's no temporary.

You can think of the code as doing the equivalent of the following:

reference dereference() const
{
     counting_iterator<T> temp_it = this->base(); // holds a T value
     --temp_it;
     return *temp_it; // return a reference to a member object of
temp_it --> BAD
}

(Note: I changed "constant_iterator" to "counting_iterator", but the bug
exists regardless.)

The change suggested by Arno (counting_iterator should return by value
upon dereferencing unless the T member object is held by reference)
seems like a good one.

- Jeff


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