<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3089: copy_n should require non-overlapping ranges</title>
<meta property="og:title" content="Issue 3089: copy_n should require non-overlapping ranges">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3089.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#New">New</a> status.</em></p>
<h3 id="3089"><a href="lwg-active.html#3089">3089</a>. <code>copy_n</code> should require non-overlapping ranges</h3>
<p><b>Section:</b> 26.7.1 <a href="https://wg21.link/alg.copy">[alg.copy]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Marshall Clow <b>Opened:</b> 2018-03-21 <b>Last modified:</b> 2022-11-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#alg.copy">active issues</a> in [alg.copy].</p>
<p><b>View all other</b> <a href="lwg-index.html#alg.copy">issues</a> in [alg.copy].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
All the copy algorithms have some kind of prohibition on having the input and output ranges overlap. 
<p/>
The serial version of <code>copy</code> says:
</p>
<blockquote><p>
<i>Requires:</i> <code>result</code> shall not be in the range <code>[first, last)</code>.
</p></blockquote>
<p>
The parallel version of <code>copy</code> says:
</p>
<blockquote><p>
<i>Requires:</i> The ranges <code>[first, last)</code> and <code>[result, result + (last - first))</code> 
shall not overlap.
</p></blockquote>
<p>
<code>copy_if</code> says: 
</p>
<blockquote><p>
<i>Requires:</i> The ranges <code>[first, last)</code> and <code>[result, result + (last - first))</code> 
shall not overlap.
</p></blockquote>
<p>
<code>copy_backwards</code> says:
</p>
<blockquote><p>
<i>Requires:</i> <code>result</code> shall not be in the range <code>[first, last)</code>.
</p></blockquote>
<p>
But <code>copy_n</code> has no such requirement.
<p/>
I think it should. I checked the minutes of the LWG discussion from 2008 when this was added, and 
there was no discussion of overlapping ranges.
</p>

<p>What formulation do we want here? Is it sufficient to say "... shall not be in the range ..." or should we use the stronger "... shall not overlap ..."? Some <code>copy</code> variants use one, some use the other.  Should we be consistent? Issue <a href="lwg-defects.html#3085" title="char_traits::copy precondition too weak (Status: C++23)">3085</a><sup><a href="https://cplusplus.github.io/LWG/issue3085" title="Latest snapshot">(i)</a></sup> is a similar issue for <code>char_traits::copy</code>.</p>

<p><i>[2018-06-18 after reflector discussion]</i></p>

<p>Priority set to 3</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4727">N4727</a>.</p>

<ol>
<li><p>Edit 26.7.1 <a href="https://wg21.link/alg.copy">[alg.copy]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> I'm using the permission in 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a>/10 to 
do random-access arithmetic on (possibly) input iterators.]
</p>
</blockquote>

<blockquote>
<pre>
template&lt;class InputIterator, class Size, class OutputIterator&gt;
  constexpr OutputIterator copy_n(InputIterator first, Size n,
                                  OutputIterator result);
template&lt;class ExecutionPolicy, class ForwardIterator1, class Size, class ForwardIterator2&gt;
  ForwardIterator2 copy_n(ExecutionPolicy&amp;&amp; exec,
                          ForwardIterator1 first, Size n,
                          ForwardIterator2 result);
</pre>
<blockquote>
<p>
<ins>-?- <i>Requires:</i> <code>result</code> shall not be in the range <code>[first, first + n)</code>.</ins>
<p/>
-9- <i>Effects:</i> For each non-negative integer <code>i &lt; n</code>, performs <code>*(result + i) = *(first + i)</code>.
<p/>
-10- <i>Returns:</i> <code>result + n</code>.
<p/>
-11- <i>Complexity:</i> Exactly <code>n</code> assignments.
</p>
</blockquote>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2022-11-06; Daniel syncs wording with recent working draft]</i></p>




<p id="res-3089"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.
</p>

<ol>
<li><p>Edit 26.7.1 <a href="https://wg21.link/alg.copy">[alg.copy]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> I'm using the permission in 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a>/10 to 
do random-access arithmetic on (possibly) input iterators.]
</p>
</blockquote>

<blockquote>
<pre>
template&lt;class InputIterator, class Size, class OutputIterator&gt;
  constexpr OutputIterator copy_n(InputIterator first, Size n,
                                  OutputIterator result);
template&lt;class ExecutionPolicy, class ForwardIterator1, class Size, class ForwardIterator2&gt;
  ForwardIterator2 copy_n(ExecutionPolicy&amp;&amp; exec,
                          ForwardIterator1 first, Size n,
                          ForwardIterator2 result);
template&lt;input_iterator I, weakly_incrementable O&gt;
  requires indirectly_copyable&lt;I, O&gt;
  constexpr ranges::copy_n_result&lt;I, O&gt;
    ranges::copy_n(I first, iter_difference_t&lt;I&gt; n, O result);
</pre>
<blockquote>
<p>
-10- Let <code><i>N</i></code> be max<code>(0, n)</code>.
<p/>
-11- <i>Mandates</i>: The type <code>Size</code> is convertible to an integral type (7.3.9, 11.4.8).
<p/>
<ins>-?- <i>Preconditions:</i> <code>result</code> is not in the range <code>[first, first + n)</code>.</ins>
<p/>
-12- <i>Effects</i>: For each non-negative integer <code><i>i</i> &lt; N</code>, performs <code>*(result + <i>i</i>) = *(first + <i>i</i>)</code>.
<p/>
-13- <i>Returns</i>:
</p>
<ol style="list-style-type: none">
<li><p>(13.1) &mdash; <code>result + <i>N</i></code> for the overloads in namespace <code>std</code>.</p></li>
<li><p>(13.2) &mdash; <code>{first + <i>N</i>, result + <i>N</i>}</code> for the overload in namespace <code>ranges</code>.</p></li>
</ol>
<p>
-14- <i>Complexity:</i> Exactly <code><i>N</i></code> assignments.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
