<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4032: Possibly invalid types in the constraints of constructors of std::shared_ptr</title>
<meta property="og:title" content="Issue 4032: Possibly invalid types in the constraints of constructors of std::shared_ptr">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4032.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#New">New</a> status.</em></p>
<h3 id="4032"><a href="lwg-active.html#4032">4032</a>. Possibly invalid types in the constraints of constructors of <code>std::shared_ptr</code></h3>
<p><b>Section:</b> 20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2023-12-25 <b>Last modified:</b> 2024-03-15</p>
<p><b>Priority: </b>4
</p>
<p><b>View other</b> <a href="lwg-index-open.html#util.smartptr.shared.const">active issues</a> in [util.smartptr.shared.const].</p>
<p><b>View all other</b> <a href="lwg-index.html#util.smartptr.shared.const">issues</a> in [util.smartptr.shared.const].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently, 20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a>/3 and /9.1 says <code>Y(*)[N]</code> and <code>Y(*)[]</code>, 
however, they may be invalid types when <code>Y</code> is an array type of unknown bound or a function type. 
Presumably, the constraints should be satisfied only when the mentioned <code>Y(*)[N]</code> or <code>Y(*)[]</code> is valid.
</p>

<p><i>[2024-03-15; Reflector poll]</i></p>

<p>
Set priority to 4 after reflector poll.
</p>
<p>
Jens pointed out that "convertible", as a core language concept,
goes from "expression" to "type", not from "type" to "type".
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>

<li><p>Modify 20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class Y&gt; explicit shared_ptr(Y* p);
</pre>
<blockquote>
<p>
-3- <i>Constraints</i>: When <code>T</code> is an array type, the expression <code>delete[] p</code> is well-formed 
and either <code>T</code> is <code>U[N]</code> and <code>Y(*)[N]</code> is <ins>a valid type and</ins> convertible to 
<code>T*</code>, or <code>T</code> is <code>U[]</code> and <code>Y(*)[]</code> is <ins>a valid type and</ins> convertible to 
<code>T*</code>. When <code>T</code> is not an array type, the expression <code>delete p</code> is well-formed and 
<code>Y*</code> is convertible to <code>T*</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class Y, class D&gt; shared_ptr(Y* p, D d);
template&lt;class Y, class D, class A&gt; shared_ptr(Y* p, D d, A a);
template&lt;class D&gt; shared_ptr(nullptr_t p, D d);
template&lt;class D, class A&gt; shared_ptr(nullptr_t p, D d, A a);
</pre>
<blockquote>
<p>
-9- <i>Constraints</i>: <code>is_move_constructible_v&lt;D&gt;</code> is <code>true</code>, and <code>d(p)</code> 
is a well-formed expression. For the first two overloads:
</p>
<ol style="list-style-type: none">
<li><p>(9.1) &mdash; If <code>T</code> is an array type, then either <code>T</code> is <code>U[N]</code> and 
<code>Y(*)[N]</code> is <ins>a valid type and</ins> convertible to <code>T*</code>, or <code>T</code> is <code>U[]</code> and
<code>Y(*)[]</code> is <ins>a valid type and</ins> convertible to <code>T*</code>.</p></li>
<li><p>(9.2) &mdash; If <code>T</code> is not an array type, then <code>Y*</code> is convertible to <code>T*</code>.</p></li>
</ol>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>

</li>

</ol>


</blockquote>

<p><i>[2024-03-15; Jonathan provides alternative wording]</i></p>

<p>
Can we just use <code>is_convertible_v&lt;Y(*)[N], T*&gt;</code>?
With <code class='backtick'>enable_if</code>-style SFINAE an invalid type will cause substitution failure
and with a <em>requires-clause</em> the constraints won't be satisfied.
Either way we get the desired outcome.
Also, the delete expression is already required to be well-formed, which
rules out function types, so that part of the issue is NAD.
</p>



<p id="res-4032"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4971" title=" Working Draft, Programming Languages — C++">N4971</a>.
</p>

<ol>

<li><p>Modify 20.3.2.2.2 <a href="https://wg21.link/util.smartptr.shared.const">[util.smartptr.shared.const]</a> as indicated:</p>

<blockquote>
<pre>
template&lt;class Y&gt; explicit shared_ptr(Y* p);
</pre>
<blockquote>
<p>
-3- <i>Constraints</i>: When <code>T</code> is an array type, the expression <code>delete[] p</code> is well-formed 
and either<ins>:</ins>
<del><code>T</code> is <code>U[N]</code> and <code>Y(*)[N]</code> is convertible to 
<code>T*</code>, or <code>T</code> is <code>U[]</code> and <code>Y(*)[]</code> is convertible to 
<code>T*</code>.</del>
</p>
<ol style="list-style-type: none">
<li>&mdash;
<ins>
<code>is_bounded_array_v&lt;T&gt; &amp;&amp;
is_convertible_v&lt;Y(*)[rank_v&lt;T&gt;], T*&gt;</code> is <code>true</code>,
or
</ins>
</li>
<li>&mdash;
<ins>
<code>is_unbounded_array_v&lt;T&gt; &amp;&amp; is_convertible_v&lt;Y(*)[], T*&gt;</code> is <code>true</code>.
</ins>
</li>
</ol>
<p>
When <code>T</code> is not an array type, the expression <code>delete p</code> is well-formed and
<del><code>Y*</code> is convertible to <code>T*</code></del>
<ins><code>is_convertible_v&lt;Y*, T*&gt;</code> is <code>true</code></ins>.
</p>
<p>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class Y, class D&gt; shared_ptr(Y* p, D d);
template&lt;class Y, class D, class A&gt; shared_ptr(Y* p, D d, A a);
template&lt;class D&gt; shared_ptr(nullptr_t p, D d);
template&lt;class D, class A&gt; shared_ptr(nullptr_t p, D d, A a);
</pre>
<blockquote>
<p>
-9- <i>Constraints</i>: <code>is_move_constructible_v&lt;D&gt;</code> is <code>true</code>, and <code>d(p)</code>
is a well-formed expression. For the first two overloads:
</p>
<ol style="list-style-type: none">
<li><p>(9.1) &mdash; If <code>T</code> is an array type, then either<ins>:</ins>
<del>
<code>T</code> is <code>U[N]</code> and
<code>Y(*)[N]</code> is convertible to <code>T*</code>, or <code>T</code> is <code>U[]</code> and
<code>Y(*)[]</code> is convertible to <code>T*</code>.
</del>
</p>
<ol style="list-style-type: none">
<li>&mdash;
<ins>
<code>is_bounded_array_v&lt;T&gt; &amp;&amp;
is_convertible_v&lt;Y(*)[rank_v&lt;T&gt;], T*&gt;</code> is <code>true</code>,
or
</ins>
</li>
<li>&mdash;
<ins>
<code>is_unbounded_array_v&lt;T&gt; &amp;&amp;
is_convertible_v&lt;Y(*)[], T*&gt;</code> is <code>true</code>.
</ins>
</li>
</ol>
</li>
<li><p>(9.2) &mdash; If <code>T</code> is not an array type, then
<del><code>Y*</code> is convertible to <code>T*</code></del>
<ins><code>is_convertible_v&lt;Y*, T*&gt;</code> is <code>true</code></ins>.
</p></li>
</ol>
<p>
[&hellip;]
</p>
</blockquote>
</blockquote>

</li>

</ol>







</body>
</html>
