<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 679: resize parameter by value</title>
<meta property="og:title" content="Issue 679: resize parameter by value">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue679.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#CD1">CD1</a> status.</em></p>
<h3 id="679"><a href="lwg-defects.html#679">679</a>. resize parameter by value</h3>
<p><b>Section:</b> 23.3 <a href="https://wg21.link/sequences">[sequences]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2007-06-11 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#sequences">issues</a> in [sequences].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The C++98 standard specifies that one member function alone of the containers
passes its parameter (<code>T</code>) by value instead of by const reference:
</p>

<blockquote><pre>
void resize(size_type sz, T c = T());
</pre></blockquote>

<p>
This fact has been discussed / debated repeatedly over the years, the first time
being even before C++98 was ratified.  The rationale for passing this parameter by
value has been:
</p>

<blockquote>
<p>
So that self referencing statements are guaranteed to work, for example:
</p>
<blockquote><pre>
v.resize(v.size() + 1, v[0]);
</pre></blockquote>
</blockquote>

<p>
However this rationale is not convincing as the signature for <code>push_back</code> is:
</p>

<blockquote><pre>
void push_back(const T&amp; x);
</pre></blockquote>

<p>
And <code>push_back</code> has similar semantics to <code>resize</code> (append).
And <code>push_back</code> must also work in the self referencing case:
</p>

<blockquote><pre>
v.push_back(v[0]);  // must work
</pre></blockquote>

<p>
The problem with passing <code>T</code> by value is that it can be significantly more
expensive than passing by reference.  The converse is also true, however when it is
true it is usually far less dramatic (e.g. for scalar types).
</p>

<p>
Even with move semantics available, passing this parameter by value can be expensive.
Consider for example <code>vector&lt;vector&lt;int&gt;&gt;</code>:
</p>

<blockquote><pre>
std::vector&lt;int&gt; x(1000);
std::vector&lt;std::vector&lt;int&gt;&gt; v;
...
v.resize(v.size()+1, x);
</pre></blockquote>

<p>
In the pass-by-value case, <code>x</code> is copied once to the parameter of
<code>resize</code>.  And then internally, since the code can not know at compile
time by how much <code>resize</code> is growing the <code>vector</code>, <code>x</code> is
usually copied (not moved) a second time from <code>resize</code>'s parameter into its proper place
within the <code>vector</code>.
</p>

<p>
With pass-by-const-reference, the <code>x</code> in the above example need be copied
only once.  In this case, <code>x</code> has an expensive copy constructor and so any
copies that can be saved represents a significant savings.
</p>

<p>
If we can be efficient for <code>push_back</code>, we should be efficient for <code>resize</code>
as well.  The resize taking a reference parameter has been coded and shipped in the
CodeWarrior library with no reports of problems which I am aware of.
</p>



<p id="res-679"><b>Proposed resolution:</b></p>
<p>
Change 23.3.5 <a href="https://wg21.link/deque">[deque]</a>, p2:
</p>

<blockquote><pre>
class deque {
   ...
   void resize(size_type sz, <ins>const </ins>T<ins>&amp;</ins> c);
</pre></blockquote>

<p>
Change 23.3.5.3 <a href="https://wg21.link/deque.capacity">[deque.capacity]</a>, p3:
</p>

<blockquote><pre>
void resize(size_type sz, <ins>const </ins>T<ins>&amp;</ins> c);
</pre></blockquote>

<p>
Change 23.3.11 <a href="https://wg21.link/list">[list]</a>, p2:
</p>

<blockquote><pre>
class list {
   ...
   void resize(size_type sz, <ins>const </ins>T<ins>&amp;</ins> c);
</pre></blockquote>

<p>
Change 23.3.11.3 <a href="https://wg21.link/list.capacity">[list.capacity]</a>, p3:
</p>

<blockquote><pre>
void resize(size_type sz, <ins>const </ins>T<ins>&amp;</ins> c);
</pre></blockquote>

<p>
Change 23.3.13 <a href="https://wg21.link/vector">[vector]</a>, p2:
</p>

<blockquote><pre>
class vector {
   ...
   void resize(size_type sz, <ins>const </ins>T<ins>&amp;</ins> c);
</pre></blockquote>

<p>
Change 23.3.13.3 <a href="https://wg21.link/vector.capacity">[vector.capacity]</a>, p11:
</p>

<blockquote><pre>
void resize(size_type sz, <ins>const </ins>T<ins>&amp;</ins> c);
</pre></blockquote>






</body>
</html>
