
Stewart, Robert wrote:
Arno Schödl wrote:
"Stewart, Robert" <Robert.Stewart@sig.com 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