Boost C++ Libraries: Ticket #2539: advance() and distance() for new iterator concepts https://svn.boost.org/trac10/ticket/2539 <p> I can't find a trace of a problem submitted by Sebastian Redl last year : <a class="ext-link" href="http://lists.boost.org/Archives/boost/2007/09/127785.php"><span class="icon">​</span>http://lists.boost.org/Archives/boost/2007/09/127785.php</a> </p> <p> In brief, the std version of advance() and distance() functions does not dispatch accordingly to the traversal_tag. For instance a transform_iterator is categorized as std::input_iterator_tag, boost::random_access_traversal_tag and the chosen distance() implementation is O(n) while O(1) is expected. </p> <p> The patch suggested by Sebastian works fine. The only thing is that names collides with Boost.Range (which already have boost::distance). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2539 Trac 1.4.3 viboes Sat, 06 Jun 2009 15:01:35 GMT component changed; owner set https://svn.boost.org/trac10/ticket/2539#comment:1 https://svn.boost.org/trac10/ticket/2539#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Dave Abrahams</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">iterator</span> </li> </ul> Ticket paul Wed, 30 Dec 2009 14:53:33 GMT <link>https://svn.boost.org/trac10/ticket/2539#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2539#comment:2</guid> <description> <p> Any update on this? I would like to add that this is not simply a effeciency issue, it leads to erroneous behavior if a boost iterator is used with std::advance and if nothing else there should be a strong warning in the documentation, i.e. the following test fails std::advance(some_boost_iterator.end(), -1) != some_boost_iterator.end(). </p> <p> Simple test, </p> <div class="wiki-code"><div class="code"><pre><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">test_iter</span> <span class="n">itr</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="n">itr</span><span class="o">++</span><span class="p">;</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">1</span><span class="p">);</span> <span class="o">++</span><span class="n">itr</span><span class="p">;</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">2</span><span class="p">);</span> <span class="n">itr</span><span class="o">+=</span><span class="mi">8</span><span class="p">;</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">10</span><span class="p">);</span> <span class="n">itr</span><span class="o">-=</span><span class="mi">8</span><span class="p">;</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">2</span><span class="p">);</span> <span class="o">--</span><span class="n">itr</span><span class="p">;</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">1</span><span class="p">);</span> <span class="n">itr</span><span class="o">--</span><span class="p">;</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">0</span><span class="p">);</span> <span class="n">std</span><span class="o">::</span><span class="n">advance</span><span class="p">(</span><span class="n">itr</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span> <span class="c1">// passes, but less efficient than expected</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">5</span><span class="p">);</span> <span class="n">std</span><span class="o">::</span><span class="n">advance</span><span class="p">(</span><span class="n">itr</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">);</span> <span class="c1">// iterator is not moved</span> <span class="n">assert</span><span class="p">(</span><span class="o">*</span><span class="n">itr</span> <span class="o">==</span> <span class="mi">0</span><span class="p">);</span> <span class="c1">// FAILURE, *itr == 5</span> <span class="p">}</span> <span class="k">struct</span> <span class="nl">test_iter</span> <span class="p">:</span> <span class="k">public</span> <span class="n">boost</span><span class="o">::</span><span class="n">iterator_facade</span><span class="o">&lt;</span> <span class="n">test_iter</span> <span class="p">,</span> <span class="kt">int</span> <span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">random_access_traversal_tag</span><span class="p">,</span> <span class="kt">int</span> <span class="o">&gt;</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">test_iter</span><span class="p">()</span> <span class="o">:</span> <span class="n">m_value</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="p">{}</span> <span class="k">explicit</span> <span class="n">test_iter</span><span class="p">(</span><span class="kt">int</span> <span class="n">v</span><span class="p">)</span> <span class="o">:</span> <span class="n">m_value</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="p">{}</span> <span class="n">test_iter</span><span class="p">(</span><span class="n">test_iter</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">other</span><span class="p">)</span> <span class="o">:</span> <span class="n">m_value</span><span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="n">m_value</span><span class="p">)</span> <span class="p">{}</span> <span class="kt">bool</span> <span class="n">equal</span><span class="p">(</span><span class="n">test_iter</span> <span class="k">const</span><span class="o">&amp;</span> <span class="n">other</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">m_value</span> <span class="o">==</span> <span class="n">other</span><span class="p">.</span><span class="n">m_value</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">increment</span><span class="p">()</span> <span class="p">{</span> <span class="o">++</span><span class="n">m_value</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">decrement</span><span class="p">()</span> <span class="p">{</span> <span class="o">--</span><span class="n">m_value</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">dereference</span><span class="p">()</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">m_value</span><span class="p">;</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">advance</span><span class="p">(</span><span class="n">difference_type</span> <span class="n">offset</span><span class="p">)</span> <span class="p">{</span> <span class="n">m_value</span><span class="o">+=</span><span class="n">offset</span><span class="p">;</span> <span class="p">}</span> <span class="n">difference_type</span> <span class="n">distance_to</span><span class="p">(</span><span class="k">const</span> <span class="n">test_iter</span><span class="o">&amp;</span> <span class="n">rhs</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">m_value</span><span class="o">-</span><span class="n">rhs</span><span class="p">.</span><span class="n">m_value</span><span class="p">;</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">m_value</span><span class="p">;</span> <span class="p">};</span> </pre></div></div> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Wed, 21 Nov 2012 19:42:25 GMT</pubDate> <title>owner changed https://svn.boost.org/trac10/ticket/2539#comment:3 https://svn.boost.org/trac10/ticket/2539#comment:3 <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 anonymous Mon, 22 Feb 2016 02:25:30 GMT <link>https://svn.boost.org/trac10/ticket/2539#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2539#comment:4</guid> <description> <p> My business partners wanted DA 31 earlier today and encountered a great service that hosts a searchable forms database . If you require DA 31 too , here's <a class="ext-link" href="http://goo.gl/3IWMJu"><span class="icon">​</span>http://goo.gl/3IWMJu</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>