<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 539: partial_sum and adjacent_difference should mention requirements</title>
<meta property="og:title" content="Issue 539: partial_sum and adjacent_difference should mention requirements">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue539.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++11">C++11</a> status.</em></p>
<h3 id="539"><a href="lwg-defects.html#539">539</a>. <code>partial_sum</code> and <code>adjacent_difference</code> should mention requirements</h3>
<p><b>Section:</b> 26.10.7 <a href="https://wg21.link/partial.sum">[partial.sum]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Marc Schoolderman <b>Opened:</b> 2006-02-06 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
There are some problems in the definition of <code>partial_sum</code> and
<code>adjacent_difference</code> in 26.4 [lib.numeric.ops]
</p>

<p>
Unlike <code>accumulate</code> and <code>inner_product</code>, these functions are not
parametrized on a "type T", instead, 26.4.3 [lib.partial.sum] simply
specifies the effects clause as;
</p>

<blockquote><p>
Assigns to every element referred to by iterator <code>i</code> in the range
<code>[result,result + (last - first))</code> a value correspondingly equal to
</p>
<blockquote><pre>
((...(* first + *( first + 1)) + ...) + *( first + ( i - result )))
</pre></blockquote>
</blockquote>

<p>
And similarly for BinaryOperation. Using just this definition, it seems
logical to expect that:
</p>


<blockquote><pre>
char i_array[4] = { 100, 100, 100, 100 };
int  o_array[4];

std::partial_sum(i_array, i_array+4, o_array);
</pre></blockquote>

<p>
Is equivalent to
</p>

<blockquote><pre>
int o_array[4] = { 100, 100+100, 100+100+100, 100+100+100+100 };
</pre></blockquote>

<p>
i.e. 100, 200, 300, 400, with addition happening in the <code>result type</code>,
<code>int</code>.
</p>

<p>
Yet all implementations I have tested produce 100, -56, 44, -112,
because they are using an accumulator of the <code>InputIterator</code>'s
<code>value_type</code>, which in this case is <code>char</code>, not <code>int</code>.
</p>

<p>
The issue becomes more noticeable when the result of the expression <code>*i +
*(i+1)</code> or <code>binary_op(*i, *i-1)</code> can't be converted to the
<code>value_type</code>. In a contrived example:
</p>

<blockquote><pre>
enum not_int { x = 1, y = 2 };
...
not_int e_array[4] = { x, x, y, y };
std::partial_sum(e_array, e_array+4, o_array);
</pre></blockquote>

<p>
Is it the intent that the operations happen in the <code>input type</code>, or in
the <code>result type</code>?
</p>

<p>
If the intent is that operations happen in the <code>result type</code>, something
like this should be added to the "Requires" clause of 26.4.3/4
[lib.partial.sum]:
</p>

<blockquote><p>
The type of <code>*i + *(i+1)</code> or <code>binary_op(*i, *(i+1))</code> shall meet the
requirements of <code>CopyConstructible</code> (20.1.3) and <code>Assignable</code>
(23.1) types.
</p></blockquote>

<p>
(As also required for <code>T</code> in 26.4.1 [lib.accumulate] and 26.4.2
[lib.inner.product].)
</p>

<p>
The "auto initializer" feature proposed in
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1894.pdf">N1894</a>
is not required to
implement <code>partial_sum</code> this way. The 'narrowing' behaviour can still be
obtained by using the <code>std::plus&lt;&gt;</code> function object.
</p>

<p>
If the intent is that operations happen in the <code>input type</code>, then
something like this should be added instead;
</p>

<blockquote><p>
The type of *first shall meet the requirements of
<code>CopyConstructible</code> (20.1.3) and <code>Assignable</code> (23.1) types.
The result of <code>*i + *(i+1)</code> or <code>binary_op(*i, *(i+1))</code> shall be
convertible to this type.
</p></blockquote>

<p>
The 'widening' behaviour can then be obtained by writing a custom proxy
iterator, which is somewhat involved.
</p>

<p>
In both cases, the semantics should probably be clarified.
</p>

<p>
26.4.4 [lib.adjacent.difference] is similarly underspecified, although
all implementations seem to perform operations in the 'result' type:
</p>

<blockquote><pre>
unsigned char i_array[4] = { 4, 3, 2, 1 };
int o_array[4];

std::adjacent_difference(i_array, i_array+4, o_array);
</pre></blockquote>

<p>
o_array is 4, -1, -1, -1 as expected, not 4, 255, 255, 255.
</p>

<p>
In any case, <code>adjacent_difference</code> doesn't mention the requirements on the
<code>value_type</code>; it can be brought in line with the rest of 26.4
[lib.numeric.ops] by adding the following to 26.4.4/2
[lib.adjacent.difference]:
</p>

<blockquote><p>
The type of <code>*first</code> shall meet the requirements of
<code>CopyConstructible</code> (20.1.3) and <code>Assignable</code> (23.1) types."
</p></blockquote>
<p><i>[
Berlin: Giving output iterator's value_types very controversial. Suggestion of
adding signatures to allow user to specify "accumulator".
]</i></p>


<p><i>[
Bellevue:
]</i></p>


<blockquote><p>
The intent of the algorithms is to perform their calculations using the type of the input iterator.
Proposed wording provided.
</p></blockquote>

<p><i>[
Sophia Antipolis:
]</i></p>


<blockquote><p>
We did not agree that the proposed resolution was correct. For example,
when the arguments are types <code>(float*, float*, double*)</code>, the
highest-quality solution would use double as the type of the
accumulator. If the intent of the wording is to require that the type of
the accumulator must be the <code>input_iterator</code>'s <code>value_type</code>, the wording
should specify it.
</p></blockquote>

<p><i>[
2009-05-09 Alisdair adds:
]</i></p>


<blockquote>
<p>
Now that we have the facility, the 'best' accumulator type could probably be
deduced as:
</p>
<blockquote><pre>
std::common_type&lt;InIter::value_type, OutIter::reference&gt;::type
</pre></blockquote>
<p>
This type would then have additional requirements of constructability and
incrementability/assignability.
</p>
<p>
If this extracting an accumulator type from a pair/set of iterators (with
additional requirements on that type) is a problem for multiple functions,
it might be worth extracting into a SharedAccumulator concept or similar.
</p>
<p>
I'll go no further in writing up wording now, until the group gives a
clearer indication of preferred direction.
</p>
</blockquote>

<p><i>[
2009-07 Frankfurt
]</i></p>


<blockquote><p>
The proposed resolution isn't quite right. For example, "the type of
<code>*first</code>" should be changed to "<code>iterator::value_type</code>" or similar. Daniel
volunteered to correct the wording.
</p></blockquote>

<p><i>[
2009-07-29 Daniel corrected wording.
]</i></p>


<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
Move to Ready.
</p></blockquote>



<p id="res-539"><b>Proposed resolution:</b></p>



<ol>
<li>
<p>
Change 26.10.7 <a href="https://wg21.link/partial.sum">[partial.sum]</a>/1 as indicated:
</p>

<blockquote>
<p>
<i>Effects:</i> <ins>Let <code>VT</code> be <code>InputIterator</code>'s value type. For a nonempty range,
initializes an accumulator <code>acc</code> of type <code>VT</code> with <code>*first</code> and performs
<code>*result = acc</code>. For every iterator <code>i</code> in <code>[first + 1, last)</code> in order, <code>acc</code> is then
modified by <code>acc = acc + *i</code> or <code>acc = binary_op(acc, *i)</code> and is assigned
to <code>*(result + (i - first))</code>.</ins> <del>Assigns to every element referred to by
iterator <code>i</code> in the range <code>[result,result + (last - first))</code> a value
correspondingly
equal to</del>
</p>

<blockquote><pre><del>
((...(*first + *(first + 1)) + ...) + *(first + (i - result)))
</del></pre></blockquote>

<p><del>
or
</del></p>

<blockquote><pre><del>
binary_op(binary_op(...,
   binary_op(*first, *(first + 1)),...), *(first + (i - result)))
</del></pre></blockquote>
</blockquote>
</li>

<li>
<p>
Change 26.10.7 <a href="https://wg21.link/partial.sum">[partial.sum]</a>/3 as indicated:
</p>

<blockquote><p>
<i>Complexity:</i> Exactly <code><ins>max(</ins>(last - first) - 1<ins>, 0)</ins></code>
applications
of <code><del>binary_op</del></code><ins>the binary operation</ins>.
</p></blockquote>
</li>

<li>
<p>
Change 26.10.7 <a href="https://wg21.link/partial.sum">[partial.sum]</a>/4 as indicated:
</p>

<blockquote><p>
<i>Requires:</i> <ins><code>VT</code> shall be constructible from the type of <code>*first</code>, the result of
<code>acc + *i</code> or <code>binary_op(acc, *i)</code> shall be implicitly convertible to <code>VT</code>, and
the result of the expression <code>acc</code> shall be writable to the <code>result</code>
output iterator.</ins> In the ranges <code>[first,last]</code> and
<code>[result,result + (last - first)]</code> [..]
</p></blockquote>
</li>

<li>
<p>
Change 26.10.12 <a href="https://wg21.link/adjacent.difference">[adjacent.difference]</a>/1 as indicated:
</p>

<blockquote>
<p>
<i>Effects:</i> <ins>Let <code>VT</code> be <code>InputIterator</code>'s value type. For a nonempty range,
initializes an accumulator <code>acc</code> of type <code>VT</code> with <code>*first</code> and performs
<code>*result = acc</code>. For every iterator <code>i</code> in <code>[first + 1, last)</code> in order,
initializes a
value <code>val</code> of type <code>VT</code> with <code>*i</code>, assigns the result of <code>val - acc</code> or
<code>binary_op(val, acc)</code>
to <code>*(result + (i - first))</code> and modifies <code>acc = std::move(val)</code>.</ins>
<del>Assigns to every element referred to by iterator <code>i</code> in the range
<code>[result + 1,
result + (last - first))</code> a value correspondingly equal to</del>
</p>

<blockquote><pre><del>
*(first + (i - result)) - *(first + (i - result) - 1)
</del></pre></blockquote>

<p><del>
or
</del></p>

<blockquote><pre><del>
binary_op(*(first + (i - result)), *(first + (i - result) - 1)).
</del></pre></blockquote>

<p><del>
result gets the value of *first.</del>
</p>
</blockquote>
</li>

<li>
<p>
Change 26.10.12 <a href="https://wg21.link/adjacent.difference">[adjacent.difference]</a>/2 as indicated:
</p>

<blockquote><p>
<i>Requires:</i> <ins><code>VT</code> shall be <code>MoveAssignable</code> ([moveassignable])
and shall be
constructible from the type of <code>*first</code>. The result
of the expression <code>acc</code> and the result of the expression <code>val - acc</code> or
<code>binary_op(val, acc)</code>
shall be writable to the <code>result</code> output iterator.</ins> In the ranges
<code>[first,last]</code> [..]
</p></blockquote>
</li>

<li>
<p>
Change 26.10.12 <a href="https://wg21.link/adjacent.difference">[adjacent.difference]</a>/5 as indicated:
</p>

<blockquote><p>
<i>Complexity:</i> Exactly <code><ins>max(</ins>(last - first) - 1<ins>, 0)</ins></code>
applications
of <del><code>binary_op</code></del><ins>the binary operation</ins>.
</p></blockquote>
</li>
</ol>








</body>
</html>
