<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2173: The meaning of operator + in the description of the algorithms</title>
<meta property="og:title" content="Issue 2173: The meaning of operator + in the description of the algorithms">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2173.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#Open">Open</a> status.</em></p>
<h3 id="2173"><a href="lwg-active.html#2173">2173</a>. The meaning of <code>operator +</code> in the description of the algorithms</h3>
<p><b>Section:</b> 26 <a href="https://wg21.link/algorithms">[algorithms]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Nikolay Ivchenkov <b>Opened:</b> 2012-08-01 <b>Last modified:</b> 2018-06-12</p>
<p><b>Priority: </b>4
</p>
<p><b>View other</b> <a href="lwg-index-open.html#algorithms">active issues</a> in [algorithms].</p>
<p><b>View all other</b> <a href="lwg-index.html#algorithms">issues</a> in [algorithms].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>

<p>
According to 26.1 <a href="https://wg21.link/algorithms.general">[algorithms.general]</a>/12,
</p>
<blockquote><p>
In the description of the algorithms operators <code>+</code> and <code>-</code> are used for some of the iterator categories 
for which they do not have to be defined. In these cases the semantics of <code>a+n</code> is the same as that of
</p>
<blockquote><pre>
X tmp = a;
advance(tmp, n);
return tmp;
</pre></blockquote>
</blockquote>
<p>
There are several places where such operator <code>+</code> is applied to an output iterator &mdash; for example, see the 
description of <code>std::copy</code>:
</p>
<blockquote><pre>
template&lt;class InputIterator, class OutputIterator&gt;
OutputIterator copy(InputIterator first, InputIterator last,
                    OutputIterator result);
</pre>
<blockquote><p>
-1- <i>Effects</i>: Copies elements in the range <code>[first,last)</code> into the range <code>[result,result + (last -
first))</code> starting from <code>first</code> and proceeding to <code>last</code>. For each non-negative integer 
<code>n &lt; (last - first)</code>, performs <code>*(result + n) = *(first + n)</code>.
</p></blockquote></blockquote>
<p>
<code>std::advance</code> is not supposed to be applicable to output iterators, so we need a different method of description.
<p/>
See also message <a href="http://listarchives.isocpp.org/cgi-bin/wg21/message?wg=lib&amp;msg=32908">c++std-lib-32908</a>.
</p>

<p><i>[2014-06-07 Daniel comments and provides wording]</i></p>

<p>
The specification for output iterators is somewhat tricky, because here a sequence of increments is required to
be combined with intervening <em>assignments</em> to the dereferenced iterator. I tried to respect this
fact by using a conceptual assignment operation as part of the specification.
<p/>
Another problem in the provided as-if-code is the question which requirements are imposed on <code>n</code>. Unfortunately,
the corresponding function <code>advance</code> is completely underspecified in this regard, so I couldn't borrow wording
from it. We cannot even assume here that <code>n</code> is the difference type of the iterator, because for output iterators there is
no requirements for this associated type to be defined. The presented wording attempts to minimize assumptions, but still
can be considered as controversial. 
</p>

<p><i>[2018-06 Rapperswil Wednesday issues processing]</i></p>

<p>Status to Open</p>


<p id="res-2173"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Change 26.1 <a href="https://wg21.link/algorithms.general">[algorithms.general]</a> around p12 as indicated:</p>

<blockquote>
<p>
-12- In the description of the algorithms operators <code>+</code> and <code>-</code> are used for some of the iterator categories for which
they do not have to be defined. In these cases the semantics of <code>a+n</code> is the same as that of
</p>
<blockquote>
<pre>
X tmp = a;
advance(tmp, n);
return tmp;
</pre>
</blockquote>
<p>
<ins>when <code>X</code> meets the input iterator requirements (24.3.5.3 <a href="https://wg21.link/input.iterators">[input.iterators]</a>), otherwise it is the same as that of</ins>
</p>
<blockquote>
<pre>
<ins>X tmp = a;
for (auto i = n; i; ++tmp, (void) --i) 
  *tmp = <i>Expr</i>(i); 
return tmp;</ins>
</pre>
</blockquote>
<p>
<ins>where <code><i>Expr</i>(i)</code> denotes the <code>(n-i)</code><sup>th</sup> expression that is assigned to for the corresponding algorithm;</ins> and that of <code>b-a</code> is the same as of
</p>
<blockquote>
<pre>
return distance(a, b);
</pre>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
