Boost C++ Libraries: Ticket #2640: Legal forward iterators cannot store their referents (was: counting_iterator::reference lifetime tied to iterator) https://svn.boost.org/trac10/ticket/2640 <p> The iterators library needs a redesign based on the facts noted in <a class="new ticket" href="https://svn.boost.org/trac10/ticket/2640#comment:2" title="#2640: Bugs: Legal forward iterators cannot store their referents (was: ... (new)">this comment</a> </p> <p> The original report is below. </p> <hr /> <p> Currently, counting_iterator&lt;T&gt;::reference is Incrementable const&amp;. This makes reverse_iterator&lt; counting_iterator&lt;T&gt; &gt;::dereference return a reference to a temporary variable: </p> <p> typename super_t::reference dereference() const { return *boost::prior(this-&gt;base()); } </p> <p> The problem is that iterator::reference is expected to stay valid even if the iterator it was obtained from is dead. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2640 Trac 1.4.3 schoedl Tue, 13 Apr 2010 08:41:29 GMT attachment set https://svn.boost.org/trac10/ticket/2640 https://svn.boost.org/trac10/ticket/2640 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">counting_iterator.hpp.patch</span> </li> </ul> <p> patch </p> Ticket Dean Michael Berris Mon, 29 Nov 2010 02:53:55 GMT <link>https://svn.boost.org/trac10/ticket/2640#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2640#comment:1</guid> <description> <p> I'm not sure I understand the consequences of this patch. Iterator references are meant to be valid only while the actual source of the data is still valid. This is the same reason why iterators typically don't own the data it refers to. </p> <p> I don't see why the semantics should change for counting iterator. Unless there is really a bug with the implementation (i.e., a reproducible crash or relying on undefined behavior) then I don't see why this patch solves anything. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Fri, 14 Jan 2011 16:50:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2640#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2640#comment:2</guid> <description> <p> The history of iterators that own the object they reference is somewhat complicated. <code>counting_iterator</code> does own its referent. To be a valid forward iterator, its reference type must be a true C++ reference. Otherwise, it would be limited to input iterator. Trying to accomodate forward iterators that store their referent caused innumerable contortions in the design of this library. </p> <p> However, recent re-readings of the C++03 standard make it clear to me that such iterators are not allowed to be forward iterators anyway. In particular, below table 74 it says: </p> <blockquote class="citation"> <p> — If a and b are both dereferenceable, then a == b if and only if *a and *b are the same object. </p> </blockquote> <p> So IMO, the library design needs to be revisited in terms of this new understanding. I don't want to address it piecemeal, because anything we do will break someone's code, and I want to make those changes all at once. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Fri, 14 Jan 2011 16:55:02 GMT</pubDate> <title>description, summary changed https://svn.boost.org/trac10/ticket/2640#comment:3 https://svn.boost.org/trac10/ticket/2640#comment:3 <ul> <li><strong>description</strong> modified (<a href="/trac10/ticket/2640?action=diff&amp;version=3">diff</a>) </li> <li><strong>summary</strong> <span class="trac-field-old">counting_iterator::reference lifetime tied to iterator</span> → <span class="trac-field-new">Legal forward iterators cannot store their referents (was: counting_iterator::reference lifetime tied to iterator)</span> </li> </ul> Ticket Dave Abrahams Wed, 21 Nov 2012 19:42:51 GMT owner changed https://svn.boost.org/trac10/ticket/2640#comment:4 https://svn.boost.org/trac10/ticket/2640#comment:4 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Dave Abrahams</span> to <span class="trac-author">jeffrey.hellrung</span> </li> </ul> Ticket Kohei Takahashi <flast@…> Tue, 30 Sep 2014 15:34:14 GMT cc set https://svn.boost.org/trac10/ticket/2640#comment:5 https://svn.boost.org/trac10/ticket/2640#comment:5 <ul> <li><strong>cc</strong> <span class="trac-author">flast@…</span> added </li> </ul> Ticket