Boost C++ Libraries: Ticket #5674: assertion failed in boost::range::copy_n() https://svn.boost.org/trac10/ticket/5674 <p> The program below fails on assert: </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/range/iterator_range.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/range/algorithm_ext/copy_n.hpp&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">argv</span><span class="p">[])</span> <span class="p">{</span> <span class="kt">int</span> <span class="n">src</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span> <span class="p">};</span> <span class="kt">int</span> <span class="n">dst</span><span class="p">[</span><span class="mi">5</span><span class="p">];</span> <span class="n">boost</span><span class="o">::</span><span class="n">copy_n</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">make_iterator_range</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">src</span> <span class="o">+</span> <span class="mi">5</span><span class="p">),</span> <span class="mi">5</span><span class="p">,</span> <span class="n">dst</span><span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><pre class="wiki">Assertion failed: n &lt; static_cast&lt;Size&gt;(boost::distance(rng)), file boost_1_46_1/boost/range/algorithm_ext/copy_n.hpp, line 38 </pre><p> Proposed patch: </p> <pre class="wiki">--- boost/range/algorithm_ext/copy_n.hpp +++ boost/range/algorithm_ext/copy_n.hpp @@ -30,12 +30,12 @@ /// /// \pre SinglePassRange is a model of the SinglePassRangeConcept /// \pre OutputIterator is a model of the OutputIteratorConcept -/// \pre 0 &lt;= n &lt; distance(rng) +/// \pre 0 &lt;= n &lt;= distance(rng) template&lt; class SinglePassRange, class Size, class OutputIterator &gt; inline OutputIterator copy_n(const SinglePassRange&amp; rng, Size n, OutputIterator out) { BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept&lt;const SinglePassRange&gt; )); - BOOST_ASSERT( n &lt; static_cast&lt;Size&gt;(boost::distance(rng)) ); + BOOST_ASSERT( n &lt;= static_cast&lt;Size&gt;(boost::distance(rng)) ); BOOST_ASSERT( n &gt;= static_cast&lt;Size&gt;(0) ); BOOST_DEDUCED_TYPENAME range_iterator&lt;const SinglePassRange&gt;::type source = boost::begin(rng); </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5674 Trac 1.4.3 Neil Groves Sun, 15 Apr 2012 11:57:15 GMT status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/5674#comment:1 https://svn.boost.org/trac10/ticket/5674#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.50.0</span> </li> </ul> <p> Resolved at last on trunk. </p> Ticket