<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2055: std::move in std::accumulate and other algorithms</title>
<meta property="og:title" content="Issue 2055: std::move in std::accumulate and other algorithms">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2055.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2055"><a href="lwg-defects.html#2055">2055</a>. <code>std::move</code> in <code>std::accumulate</code> and other algorithms</h3>
<p><b>Section:</b> 26.10 <a href="https://wg21.link/numeric.ops">[numeric.ops]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Chris Jefferson <b>Opened:</b> 2011-01-01 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#numeric.ops">issues</a> in [numeric.ops].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The C++0x draft says <code>std::accumulate</code> uses: <code>acc = binary_op(acc, *i)</code>.
<p/>
Eelis van der Weegen has pointed out, on the libstdc++ mailing list, that using 
<code>acc = binary_op(std::move(acc), *i)</code> can lead to massive improvements (particularly, 
it means accumulating strings is linear rather than quadratic).
<p/>
Consider the simple case, accumulating a bunch of strings of length 1 (the same argument holds for other length buffers).
For strings <code>s</code> and <code>t</code>, <code>s+t</code> takes time <code>length(s)+length(t)</code>, as you have to copy 
both <code>s</code> and <code>t</code> into a new buffer.
<p/>
So in accumulating <code>n</code> strings, step <code>i</code> adds a string of length <code>i-1</code> to a string of length 
1, so takes time <code>i</code>.
<p/>
Therefore the total time taken is: <code>1+2+3+...+n</code> = O(<code>n<sup>2</sup></code>)
<p/>
<code>std::move(s)+t</code>, for a "good" implementation, is amortized time <code>length(t)</code>, like <code>vector</code>, 
just copy <code>t</code> onto the end of the buffer. So the total time taken is:
<p/>
<code>1+1+1+...+1</code> (<code>n</code> times) = O(<code>n</code>). This is the same as <code>push_back</code> on a <code>vector</code>.
<p/>
I'm trying to decide if this implementation might already be allowed. I suspect it might not 
be (although I can't imagine any sensible code it would break). There are other algorithms 
which could benefit similarly (<code>inner_product</code>, <code>partial_sum</code> and 
<code>adjacent_difference</code> are the most obvious).
<p/>
Is there any general wording for "you can use rvalues of temporaries"?
<p/>
The reflector discussion starting with message c++std-lib-29763 came to the conclusion
that above example is not covered by the "as-if" rules and that enabling this behaviour
would seem quite useful.
</p>

<p><i>[
2011 Bloomington
]</i></p>


<p>
Moved to NAD Future.  This would be a larger change than we would consider for a simple TC.

<p><i>[2017-02 in Kona, LEWG responds]</i></p>

<p>Like the goal.</p>
<p>What is broken by adding std::move() on the non-binary-op version?</p>
<p>A different overload might be selected, and that would be a breakage. Is it breakage that we should care about?</p>
<p>We need to encourage value semantics.</p>
<p>Need a paper. What guidance do we give?</p>
<p>Use std::reduce() (uses generalized sum) instead of accumulate which doesn&rsquo;t suffer it.</p>
<p>Inner_product and adjacent_difference also. adjacent_difference solves it half-way for &ldquo;val&rdquo; object, but misses the opportunity for passing acc as <code>std::move(acc)</code>.</p>

<p><i>[2017-06-02 Issues Telecon]</i></p>

<p>Ville to encourage Eelis to write a paper on the algorithms in &lt;numeric&gt;, not just for accumulate.</p>
<p>Howard pointed out that this has already been done for the algorithms in &lt;algorithm&gt;</p>
<p>Status to Open; Priority 3</p>
</p>

<p><i>[2017-11-11, Albuquerque]</i></p>

<p>This was resolved by the adoption of <a href="https://wg21.link/P0616">P0616r0</a>.</p>


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





</body>
</html>
