<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 385: Does call by value imply the CopyConstructible requirement?</title>
<meta property="og:title" content="Issue 385: Does call by value imply the CopyConstructible requirement?">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue385.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#NAD">NAD</a> status.</em></p>
<h3 id="385"><a href="lwg-closed.html#385">385</a>. Does call by value imply the CopyConstructible requirement?</h3>
<p><b>Section:</b> 16 <a href="https://wg21.link/library">[library]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Matt Austern <b>Opened:</b> 2002-10-23 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#library">active issues</a> in [library].</p>
<p><b>View all other</b> <a href="lwg-index.html#library">issues</a> in [library].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Many function templates have parameters that are passed by value;
a typical example is <code>find_if</code>'s <i>pred</i> parameter in
26.6.6 <a href="https://wg21.link/alg.find">[alg.find]</a>.  Are the corresponding template parameters
(<code>Predicate</code> in this case) implicitly required to be
CopyConstructible, or does that need to be spelled out explicitly?
</p>

<p>
This isn't quite as silly a question as it might seem to be at first
sight.  If you call <code>find_if</code> in such a way that template
argument deduction applies, then of course you'll get call by value
and you need to provide a copy constructor.  If you explicitly provide
the template arguments, however, you can force call by reference by
writing something like <code>find_if&lt;my_iterator,
my_predicate&amp;&gt;</code>.  The question is whether implementation
are required to accept this, or whether this is ill-formed because
my_predicate&amp; is not CopyConstructible.
</p>

<p>
The scope of this problem, if it is a problem, is unknown.  Function
object arguments to generic algorithms in clauses 26 <a href="https://wg21.link/algorithms">[algorithms]</a>
and 29 <a href="https://wg21.link/numerics">[numerics]</a> are obvious examples.  A review of the whole
library is necessary.
</p>
<p><i>[
This is really two issues.  First, predicates are typically passed by
value but we don't say they must be Copy Constructible.  They should
be. Second: is specialization allowed to transform value arguments
into references? References aren't copy constructible, so this should
not be allowed.
]</i></p>

<p><i>[
2007-01-12, Howard: First, despite the note above, references <b>are</b>
copy constructible. They just aren't assignable.  Second, this is very
closely related to <a href="lwg-defects.html#92" title="Incomplete Algorithm Requirements (Status: CD1)">92</a><sup><a href="https://cplusplus.github.io/LWG/issue92" title="Latest snapshot">(i)</a></sup> and should be consistent with that.
That issue already says that implementations are allowed to copy
function objects.  If one passes in a reference, it is copyable, but
susceptible to slicing if one passes in a reference to a base.  Third,
with rvalue reference in the language one only needs to satisfy
MoveConstructible to pass an rvalue "by value".  Though the function
might still copy the function object internally (requiring
CopyConstructible). Finally (and fwiw), if we wanted to, it is easy to
code all of the std::algorithms such that they do not copy function
objects internally.  One merely passes them by reference internally if
desired (this has been fully implemented and shipped for several years).
 If this were mandated, it would reverse <a href="lwg-defects.html#92" title="Incomplete Algorithm Requirements (Status: CD1)">92</a><sup><a href="https://cplusplus.github.io/LWG/issue92" title="Latest snapshot">(i)</a></sup>, allowing
function objects to reliably maintain state.  E.g. the example in <a href="lwg-defects.html#92" title="Incomplete Algorithm Requirements (Status: CD1)">92</a><sup><a href="https://cplusplus.github.io/LWG/issue92" title="Latest snapshot">(i)</a></sup> would reliably remove only the third element.
]</i></p>



<p id="res-385"><b>Proposed resolution:</b></p>
<p>
Recommend NAD.
</p>


<p><b>Rationale:</b></p>
<p>
Generic algorithms will be marked with concepts and these will imply a requirement
of MoveConstructible (not CopyConstructible).  The signature of the function will
then precisely describe and enforce the precise requirements.
</p>





</body>
</html>
