Boost C++ Libraries: Ticket #7680: Poor choice of difference_type for zip_iterator https://svn.boost.org/trac10/ticket/7680 <p> Hi, </p> <p> the following program results in an integer overflow on my system (Linux g++ 4.7.2): </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/iterator/zip_iterator.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/iterator/counting_iterator.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/tuple/tuple.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">counting_iterator</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">short</span><span class="o">&gt;</span> <span class="n">i1</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="n">boost</span><span class="o">::</span><span class="n">counting_iterator</span><span class="o">&lt;</span><span class="kt">unsigned</span> <span class="kt">long</span> <span class="kt">long</span><span class="o">&gt;</span> <span class="n">i2</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span> <span class="k">auto</span> <span class="n">i3</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">make_zip_iterator</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">make_tuple</span><span class="p">(</span><span class="n">i1</span><span class="p">,</span> <span class="n">i2</span><span class="p">));</span> <span class="n">i3</span> <span class="o">+=</span> <span class="mi">3221225472u</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">boost</span><span class="o">::</span><span class="n">get</span><span class="o">&lt;</span><span class="mi">1</span><span class="o">&gt;</span><span class="p">(</span><span class="o">*</span><span class="n">i3</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="sc">&#39;\n&#39;</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> It prints 18446744072635809792, but I'd expect it to print 3221225472. boost::zip_iterator seems to use the first underlying iterator's difference_type as its own difference_type, which isn't necessarily the right choice. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7680 Trac 1.4.3 anonymous Sun, 11 Nov 2012 05:59:16 GMT <link>https://svn.boost.org/trac10/ticket/7680#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7680#comment:1</guid> <description> <p> According to the <a href="http://www.boost.org/doc/libs/1_52_0/libs/iterator/doc/new-iter-concepts.html">iterator concepts</a>, the difference_type needs to be a signed integral type. It should thus easily be possible to choose the difference_type with the biggest range from the underlying iterator types. </p> <p> Also note that g++-4.7.2 emits a warning for the above code when compiled with -Wsign-conversion. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Wed, 21 Nov 2012 22:41:14 GMT</pubDate> <title>owner changed https://svn.boost.org/trac10/ticket/7680#comment:2 https://svn.boost.org/trac10/ticket/7680#comment:2 <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