<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2687: {inclusive,exclusive}_scan misspecified</title>
<meta property="og:title" content="Issue 2687: {inclusive,exclusive}_scan misspecified">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2687.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#C++17">C++17</a> status.</em></p>
<h3 id="2687"><a href="lwg-defects.html#2687">2687</a>. <code>{inclusive,exclusive}_scan</code> misspecified</h3>
<p><b>Section:</b> 26.10.8 <a href="https://wg21.link/exclusive.scan">[exclusive.scan]</a>, 26.10.9 <a href="https://wg21.link/inclusive.scan">[inclusive.scan]</a>, 26.10.10 <a href="https://wg21.link/transform.exclusive.scan">[transform.exclusive.scan]</a>, 26.10.11 <a href="https://wg21.link/transform.inclusive.scan">[transform.inclusive.scan]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Tim Song <b>Opened:</b> 2016-03-21 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>1
</p>
<p><b>View all other</b> <a href="lwg-index.html#exclusive.scan">issues</a> in [exclusive.scan].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
When P0024R2 changed the description of <code>{inclusive,exclusive}_scan</code> (and the <code>transform_</code> version), 
it seems to have introduced an off-by-one error. Consider what N4582 26.10.9 <a href="https://wg21.link/inclusive.scan">[inclusive.scan]</a>/2 says of 
<code>inclusive_scan</code>:
</p>
<blockquote>
<p>
Effects: Assigns through each iterator <code>i</code> in <code>[result, result + (last - first))</code> the value of
</p>
<ul>
<li><p>
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, *j, ...)</code> for every <code>j</code> in <code>[first, first
+ (i - result))</code> if <code>init</code> is provided, or
</p></li>
<li><p>
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, *j, ...)</code> for every <code>j</code> in <code>[first, first + (i
- result))</code> otherwise.
</p></li>
</ul>
</blockquote>
<p>
When <code>i == result</code>, <code>i - result = 0</code>, so the range <code>[first, first + (i - result))</code> is 
<code>[first, first)</code> &mdash; which is empty. Thus according to the specification, we should assign 
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init)</code> if <code>init</code> is provided, or 
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op)</code> otherwise, which doesn't seem "inclusive" &mdash; and isn't 
even defined in the second case.
<p/>
The parallelism TS's version uses <code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, *first, ..., *(first + (i - result)))</code> &mdash; 
which is a closed range, not a half-open one.
<p/>
Similar issues affect <code>exclusive_scan</code>, <code>transform_inclusive_scan</code>, and <code>transform_exclusive_scan</code>. 
</p>
<p><i>[2016-06 Oulu]</i></p>

<p>Voted to Ready 11-0 Tuesday evening in Oulu</p>


<p id="res-2687"><b>Proposed resolution:</b></p>
<p>
This wording is relative to N4582.
</p>
<ol>
<li><p>Edit 26.10.8 <a href="https://wg21.link/exclusive.scan">[exclusive.scan]</a>/2 as indicated:</p>
<blockquote class="note">
<p>
[<i>Drafting note</i>: when <code>i</code> is <code>result</code>, <code>[first, first + (i - result))</code> is an empty range, 
so the value to be assigned reduces to <code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init)</code>, which is 
<code>init</code>, so there's no need to special case this.]
</p>
</blockquote>

<blockquote><pre>
template&lt;class InputIterator, class OutputIterator, class T, class BinaryOperation&gt;
  OutputIterator exclusive_scan(InputIterator first, InputIterator last,
                                OutputIterator result,
                                T init, BinaryOperation binary_op);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Assigns through each iterator <code>i</code> in <code>[result, result + (last - first))</code> the value of
<ins><code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, *j, ...)</code> for every <code>j</code> in 
<code>[first, first + (i - result))</code>.</ins></p>
<ul>
<li><p>
<del><code>init</code>, if <code>i</code> is <code>result</code>, otherwise</del>
</p></li>
<li><p>
<del><code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, *j, ...)</code> for every <code>j</code> in <code>[first, first
+ (i - result) - 1)</code>.</del>
</p></li>
</ul>
</blockquote>
</blockquote>
</li>

<li><p>Edit 26.10.9 <a href="https://wg21.link/inclusive.scan">[inclusive.scan]</a>/2 as indicated:</p>

<blockquote><pre>
template&lt;class InputIterator, class OutputIterator, class BinaryOperation&gt;
  OutputIterator inclusive_scan(InputIterator first, InputIterator last,
                                OutputIterator result,
                                BinaryOperation binary_op);
template&lt;class InputIterator, class OutputIterator, class BinaryOperation&gt;
  OutputIterator inclusive_scan(InputIterator first, InputIterator last,
                                OutputIterator result,
                                BinaryOperation binary_op, T init);
</pre>
<blockquote>
<p>
-2- <i>Effects</i>: Assigns through each iterator <code>i</code> in <code>[result, result + (last - first))</code> the value of
</p>
<ul>
<li><p>
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, *j, ...)</code> for every <code>j</code> in <code>[first, first
+ (i - result <ins>+ 1</ins>))</code> if <code>init</code> is provided, or
</p></li>
<li><p>
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, *j, ...)</code> for every <code>j</code> in <code>[first, first
+ (i - result <ins>+ 1</ins>))</code> otherwise.
</p></li>
</ul>
</blockquote>
</blockquote>
</li>

<li><p>Edit 26.10.10 <a href="https://wg21.link/transform.exclusive.scan">[transform.exclusive.scan]</a>/1 as indicated:</p>

<blockquote><pre>
template&lt;class InputIterator, class OutputIterator,
         class UnaryOperation,
         class T, class BinaryOperation&gt;
  OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          T init, BinaryOperation binary_op);
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: Assigns through each iterator <code>i</code> in <code>[result, result + (last - first))</code> the value of
<ins><code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, unary_op(*j), ...)</code> for every <code>j</code> in 
<code>[first, first + (i - result))</code>.</ins></p>
<ul>
<li><p>
<del><code>init</code>, if <code>i</code> is <code>result</code>, otherwise</del>
</p></li>
<li><p>
<del><code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, unary_op(*j), ...)</code> for every <code>j</code> in 
<code>[first, first + (i - result) - 1)</code>.</del>
</p></li>
</ul>
</blockquote>
</blockquote>
</li>

<li><p>Edit 26.10.11 <a href="https://wg21.link/transform.inclusive.scan">[transform.inclusive.scan]</a>/1 as indicated:</p>

<blockquote><pre>
template&lt;class InputIterator, class OutputIterator,
         class UnaryOperation,
         class BinaryOperation&gt;
  OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          BinaryOperation binary_op);
template&lt;class InputIterator, class OutputIterator,
         class UnaryOperation,
         class BinaryOperation, class T&gt;
  OutputIterator transform_inclusive_scan(InputIterator first, InputIterator last,
                                          OutputIterator result,
                                          UnaryOperation unary_op,
                                          BinaryOperation binary_op, T init);
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: Assigns through each iterator <code>i</code> in <code>[result, result + (last - first))</code> the value of
</p>
<ul>
<li><p>
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, init, unary_op(*j), ...)</code> for every <code>j</code> in <code>[first, first
+ (i - result <ins>+ 1</ins>))</code> if <code>init</code> is provided, or
</p></li>
<li><p>
<code><i>GENERALIZED_NONCOMMUTATIVE_SUM</i>(binary_op, unary_op(*j), ...)</code> for every <code>j</code> in <code>[first, first
+ (i - result <ins>+ 1</ins>))</code> otherwise.
</p></li>
</ul>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
