<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2146: Are reference types CopyConstructible&#47;MoveConstructible&#47;CopyAssignable&#47;MoveAssignable&#47;Destructible?</title>
<meta property="og:title" content="Issue 2146: Are reference types CopyConstructible&#47;MoveConstructible&#47;CopyAssignable&#47;MoveAssignable&#47;Destructible?">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2146.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#Open">Open</a> status.</em></p>
<h3 id="2146"><a href="lwg-active.html#2146">2146</a>. Are reference types <code>CopyConstructible</code>&#47;<code>MoveConstructible</code>&#47;<code>CopyAssignable</code>&#47;<code>MoveAssignable</code>&#47;<code>Destructible</code>?</h3>
<p><b>Section:</b> 16.4.4.2 <a href="https://wg21.link/utility.arg.requirements">[utility.arg.requirements]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Nikolay Ivchenkov <b>Opened:</b> 2012-03-23 <b>Last modified:</b> 2024-12-04</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#utility.arg.requirements">active issues</a> in [utility.arg.requirements].</p>
<p><b>View all other</b> <a href="lwg-index.html#utility.arg.requirements">issues</a> in [utility.arg.requirements].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>

<p>
According to 16.4.4.2 <a href="https://wg21.link/utility.arg.requirements">[utility.arg.requirements]</a> p1
</p>
<blockquote><p>
The template definitions in the C++ standard library refer to various named requirements whose details are set out in 
tables 17-24. In these tables, <code>T</code> is an object or reference type to be supplied by a C++ program instantiating 
a template; <code>a</code>, <code>b</code>, and <code>c</code> are values of type (possibly <code>const</code>) <code>T</code>; <code>s</code> 
and <code>t</code> are modifiable lvalues of type <code>T</code>; <code>u</code> denotes an identifier; <code>rv</code> is an rvalue of 
type <code>T</code>; and <code>v</code> is an lvalue of type (possibly <code>const</code>) <code>T</code> or an rvalue of type <code>const T</code>.
</p></blockquote>
<p>
Is it really intended that <code>T</code> may be a reference type? If so, what should <code>a</code>, <code>b</code>, <code>c</code>, 
<code>s</code>, <code>t</code>, <code>u</code>, <code>rv</code>, and <code>v</code> mean? For example, are "<code>int &amp;</code>" and 
"<code>int &amp;&amp;</code>" <code>MoveConstructible</code>?
<p/>
As far as I understand, we can explicitly specify template arguments for <code>std::swap</code> and <code>std::for_each</code>. 
Can we use reference types there?
</p>
<ol>
<li>
<blockquote><pre>
#include &lt;iostream&gt;
#include &lt;utility&gt;

int main()
{
  int x = 1;
  int y = 2;
  std::swap&lt;<span style="color:#C80000;font-weight:bold">int &amp;&amp;</span>&gt;(x, y); // <em>undefined?</em>
  std::cout &lt;&lt; x &lt;&lt; " " &lt;&lt; y &lt;&lt; std::endl;
}
</pre></blockquote>
</li>
<li>
<blockquote><pre>
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;utility&gt;

struct F
{
  void operator()(int n)
  {
    std::cout &lt;&lt; n &lt;&lt; std::endl;
    ++count;
  }
  int count;
} f;

int main()
{
  int arr[] = { 1, 2, 3 };
  auto&amp;&amp; result = std::for_each&lt;int *, <span style="color:#C80000;font-weight:bold">F &amp;&amp;</span>&gt;( // <em>undefined?</em>
    std::begin(arr),
    std::end(arr),
    std::move(f));
  std::cout &lt;&lt; "count: " &lt;&lt; result.count &lt;&lt; std::endl;
}
</pre></blockquote>
</li>
</ol>
<p>
Are these forms of usage well-defined?
<p/>
Let's also consider the following constructor of <code>std::thread</code>:
</p>
<blockquote><pre>
template &lt;class F, class ...Args&gt;
explicit thread(F&amp;&amp; f, Args&amp;&amp;... args);
</pre><blockquote>
<p>
<i>Requires</i>: <code>F</code> and each <code>Ti</code> in <code>Args</code> shall satisfy the <code>MoveConstructible</code> requirements.
</p>
</blockquote></blockquote>
<p>
When the first argument of this constructor is an lvalue (e.g. a name of a global function), template argument for <code>F</code> 
is deduced to be lvalue reference type. What should "<code>MoveConstructible</code>" mean with regard to an lvalue reference 
type? Maybe the wording should say that <code>std::decay&lt;F&gt;::type</code> and each <code>std::decay&lt;Ti&gt;::type</code> (where 
<code>Ti</code> is an arbitrary item in <code>Args</code>) shall satisfy the <code>MoveConstructible</code> requirements?
</p>

<p><i>[2013-03-15 Issues Teleconference]</i></p>

<p>Moved to Open.</p>
<p>The questions raised by the issue are real, and should have a clear answer.</p>

<p><i>[2015-10, Kona Saturday afternoon]</i></p>

<p>STL: std::thread needs to be fixed, and anything behaving like it needs to be fixed, rather than reference types. std::bind gets this right. We need to survey this. GR: That doesn't sound small to me. STL: Seach for CopyConstructible etc. It may be a long change, but not a hard one.</p>
<p>MC: It seems that we don't have a PR. Does anyone have one? Is anyone interested in doing a survey?</p>

<p><i>[2016-03, Jacksonville]</i></p>

<p>Casey volunteers to make a survey</p>

<p><i>[2016-06, Oulu]</i></p>

<p>
During an independent survey performed by Daniel as part of the analysis of LWG <a href="lwg-defects.html#2716" title="Specification of shuffle and sample disallows lvalue URNGs (Status: C++17)">2716</a><sup><a href="https://cplusplus.github.io/LWG/issue2716" title="Latest snapshot">(i)</a></sup>,
some overlap was found between these two issues. Daniel suggested to take responsibility for surveying
LWG <a href="lwg-active.html#2146" title="Are reference types CopyConstructible&#47;MoveConstructible&#47;CopyAssignable&#47;MoveAssignable&#47;Destructible? (Status: Open)">2146</a><sup><a href="https://cplusplus.github.io/LWG/issue2146" title="Latest snapshot">(i)</a></sup> and opined that the P/R of LWG <a href="lwg-defects.html#2716" title="Specification of shuffle and sample disallows lvalue URNGs (Status: C++17)">2716</a><sup><a href="https://cplusplus.github.io/LWG/issue2716" title="Latest snapshot">(i)</a></sup> should restrict to forwarding 
references, where the deduction to lvalue references can happen without providing an explicit template
argument just by providing an lvalue function argument.
</p>

<p><i>[2018-06, Rapperwsil]</i></p>

<p>Jonathan says that this will be covered by his Omnibus requirements paper.</p>

<p><i>[2019 Cologne Wednesday night]</i></p>

<p>Daniel will start working on this again; Marshall to provide rationale why some of the examples are not well-formed.</p>

<p><i>[2020-10-02; Issue processing telecon: change from P2 to P3]</i></p>

<p>
For the examples given in the original report, the <code>for_each</code>
case is now banned, because 26.2 <a href="https://wg21.link/algorithms.requirements">[algorithms.requirements]</a> p15
forbids explicit template argument lists. <code>std::thread</code>'s constructor
has also been fixed to describe the requirements on <code>decay_t&lt;T&gt;</code>
instead of <code>T</code>.
</p>
<p>
We believe we're more careful these days about using <code>remove_cvref</code>
or <code>decay</code> as needed, but there are still places where we incorrectly
state requirements in terms of types that might be references.
The <code>swap</code> case still needs solving. Still need a survey.
</p>

<p><i>[2024-03-15; LWG <a href="lwg-active.html#4047" title="Explicitly specifying template arguments for std::swap should not be supported (Status: New)">4047</a><sup><a href="https://cplusplus.github.io/LWG/issue4047" title="Latest snapshot">(i)</a></sup> addresses the <code>swap</code> part]</i></p>


<p><i>[2024-12-04; Daniel comments]</i></p>

<p>
The mentioned requirement sets have been renamed a while ago to:
</p>
<ul>
<li><p><i>Cpp17CopyConstructible</i></p></li>
<li><p><i>Cpp17MoveConstructible</i></p></li>
<li><p><i>Cpp17CopyAssignable</i></p></li>
<li><p><i>Cpp17MoveAssignable</i></p></li>
<li><p><i>Cpp17Destructible</i></p></li>
</ul>



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





</body>
</html>
