<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2312: tuple's constructor constraints need to be phrased more precisely</title>
<meta property="og:title" content="Issue 2312: tuple's constructor constraints need to be phrased more precisely">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2312.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#C++17">C++17</a> status.</em></p>
<h3 id="2312"><a href="lwg-defects.html#2312">2312</a>. <code>tuple</code>'s constructor constraints need to be phrased more precisely</h3>
<p><b>Section:</b> 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#tuple.cnstr">active issues</a> in [tuple.cnstr].</p>
<p><b>View all other</b> <a href="lwg-index.html#tuple.cnstr">issues</a> in [tuple.cnstr].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Consider the following code:
</p>
<blockquote><pre>
void meow(tuple&lt;long, long&gt;) { puts("Two"); }

void meow(tuple&lt;long, long, long&gt;) { puts("Three"); }

tuple&lt;int, int, int&gt; t(0, 0, 0);

meow(t);
</pre></blockquote>
<p>
This should compile and print "Three" because <code>tuple&lt;long, long&gt;</code>'s constructor from 
<code>const tuple&lt;int, int, int&gt;&amp;</code> should remove itself from overload resolution.  
Implementations sensibly do this, but the Standard doesn't actually say it!
<p/>
In this case, <code>Types</code> is "<code>long, long</code>" and <code>UTypes</code> is "<code>int, int, int</code>". 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/3 
says "let <code>i</code> be in the range <code>[0,sizeof...(Types))</code> in order", which is <code>[0, 2)</code>.  Then /17 says 
"<i>Remark:</i> This constructor shall not participate in overload resolution unless <code>const Ui&amp;</code> is implicitly 
convertible to <code>Ti</code> for all <code>i</code>." Interpreted literally, this is true!  /15 says 
"<i>Requires:</i> <code>sizeof...(Types) == sizeof...(UTypes)</code>." but requiring the sizes to be identical doesn't help.  
Only the special phrase "shall not participate in overload resolution unless" mandates SFINAE/<code>enable_if</code> machinery.
<p/>
The wording that we need is almost available in the <i>Requires</i> paragraphs, except that the <i>Requires</i> paragraphs say 
"<code>is_constructible</code>" while the Remark paragraphs say "is implicitly convertible", which is the correct thing for the SFINAE 
constraints to check. My proposed resolution is to unify the <i>Requires</i> and <i>Remark</i> paragraphs, after which there 
will be no need for <i>Requires</i> (when a constructor participates in overload resolution if and only if <code>X</code> is true, 
then there's no need for it to <i>Require</i> that <code>X</code> is true).
<p/>
Note: 21.3.6.4 <a href="https://wg21.link/meta.unary.prop">[meta.unary.prop]</a>/6 specifies <code>is_constructible&lt;To, From&gt;</code> and 21.3.8 <a href="https://wg21.link/meta.rel">[meta.rel]</a>/4 specifies 
<code>is_convertible&lt;From, To&gt;</code>. Both are specified in terms of 
"<code>template &lt;class T&gt; typename add_rvalue_reference&lt;T&gt;::type create();</code>".  
Therefore, passing <code>From</code> and <code>From&amp;&amp;</code> is equivalent, regardless of whether <code>From</code> is an object type, 
an lvalue reference, or an rvalue reference.
<p/>
Also note that 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/3 defines <code>T0</code> and <code>T1</code> so we don't need to repeat their definitions.
</p>

<p><i>[2014-10-05, Daniel comments]</i></p>

<p>
This issue is closely related to LWG <a href="lwg-defects.html#2419" title="Clang's libc++ extension to std::tuple (Status: Resolved)">2419</a><sup><a href="https://cplusplus.github.io/LWG/issue2419" title="Latest snapshot">(i)</a></sup>.
</p>

<p><i>[2015-02, Cologne]</i></p>

<p>
AM: Howard wants to do something in this space and I want to wait for him to get a paper in.
<p/>
Postponed.
</p>

<p><i>[2015-05, Lenexa]</i></p>

<p>
MC: handled by Daniel's tuple paper <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4387.html">N4387</a><br/>
STL: look at status after N4387 applied.
</p>
<p><i>[2015-05-05, Daniel comments]</i></p>

<p>
N4387 doesn't touch these area intentionally. I agree with Howard that a different option exists that would introduce
a TupleLike concept. Some implementations currently take advantage of this choice and this P/R would forbid them, which seems
unfortunate to me.
</p>

<p><i>[2015-09, Telecon]</i></p>

<p>
Proposed resolution is obsolete. <br/>
Howard has considered writing a paper.<br/>
Status quo gives more implementation freedom.<br/>
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<p/>
<blockquote class="note">
<p>This wording is relative to N3691.</p>

<ol>
<li><p>Edit 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a> as indicated:</p>

<blockquote><pre>
template &lt;class... UTypes&gt;
  explicit constexpr tuple(UTypes&amp;&amp;... u);
</pre><blockquote>
<p>
<del>-8- <i>Requires:</i> <code>sizeof...(Types) == sizeof...(UTypes)</code>. <code>is_constructible&lt;Ti, Ui&amp;&amp;&gt;::value</code> is true
for all <i>i</i>.</del>
<p/>
[&hellip;]
<p/>
-10- <i>Remark:</i> This constructor shall not participate in overload resolution unless <del>each type in <code>UTypes</code> is
implicitly convertible to its corresponding type in <code>Types</code></del><ins><code>sizeof...(Types) == sizeof...(UTypes)</code> and 
both <code>is_constructible&lt;Ti, Ui&gt;::value</code> and <code>is_convertible&lt;Ui, Ti&gt;::value</code> are true for all <i>i</i></ins>.
</p>
</blockquote>
<p>
[&hellip;]
</p>
</blockquote>
<blockquote><pre>
template &lt;class... UTypes&gt;
  constexpr tuple(const tuple&lt;UTypes...&gt;&amp; u);
</pre><blockquote>
<p>
<del>-15- <i>Requires:</i> <code>sizeof...(Types) == sizeof...(UTypes)</code>. <code>is_constructible&lt;Ti, const Ui&amp;&gt;::value</code> is true
for all <i>i</i>.</del>
<p/>
[&hellip;]
<p/>
-17- <i>Remark:</i> This constructor shall not participate in overload resolution unless <del><code>const Ui&amp;</code> is implicitly
convertible to <code>Ti</code></del><ins><code>sizeof...(Types) == sizeof...(UTypes)</code> and 
both <code>is_constructible&lt;Ti, const Ui&amp;&gt;::value</code> and <code>is_convertible&lt;const Ui&amp;, Ti&gt;::value</code> are 
true</ins> for all <i>i</i>.
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class... UTypes&gt;
  constexpr tuple(tuple&lt;UTypes...&gt;&amp;&amp; u);
</pre><blockquote>
<p>
<del>-18- <i>Requires:</i> <code>sizeof...(Types) == sizeof...(UTypes)</code>. <code>is_constructible&lt;Ti, Ui&amp;&amp;&gt;::value</code> is true
for all <i>i</i>.</del>
<p/>
[&hellip;]
<p/>
-20- <i>Remark:</i> This constructor shall not participate in overload resolution unless <del>each type in <code>UTypes</code> is
implicitly convertible to its corresponding type in <code>Types</code></del><ins><code>sizeof...(Types) == sizeof...(UTypes)</code> and 
both <code>is_constructible&lt;Ti, Ui&gt;::value</code> and <code>is_convertible&lt;Ui, Ti&gt;::value</code> are true for all <i>i</i></ins>.
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class U1, class U2&gt; constexpr tuple(const pair&lt;U1, U2&gt;&amp; u);
</pre><blockquote>
<p>
<del>-21- <i>Requires:</i> <code>sizeof...(Types) == 2</code>. <code>is_constructible&lt;T0, const U1&amp;&gt;::value</code> is true for the first
type <code>T0</code> in Types and <code>is_constructible&lt;T1, const U2&amp;&gt;::value</code> is true for the second type <code>T1</code> in
<code>Types</code>.</del>
<p/>
[&hellip;]
<p/>
-23- <i>Remark:</i> This constructor shall not participate in overload resolution unless <del><code>const U1&amp;</code> is implicitly
convertible to <code>T0</code> and <code>const U2&amp;</code> is implicitly convertible to <code>T1</code></del><ins><code>sizeof...(Types) == 2 
&amp;&amp; is_constructible&lt;T0, const U1&amp;&gt;::value &amp;&amp; is_constructible&lt;T1, const U2&amp;&gt;::value &amp;&amp; 
is_convertible&lt;const U1&amp;, T0&gt;::value &amp;&amp; is_convertible&lt;const U2&amp;, T1&gt;::value</code> is true</ins>.
</p>
</blockquote>
</blockquote>
<blockquote><pre>
template &lt;class U1, class U2&gt; constexpr tuple(pair&lt;U1, U2&gt;&amp;&amp; u);
</pre><blockquote>
<p>
<del>-24- <i>Requires:</i> <code>sizeof...(Types) == 2</code>. <code>is_constructible&lt;T0, U1&amp;&amp;&gt;::value</code> is true for the first
type <code>T0</code> in Types and <code>is_constructible&lt;T1, U2&amp;&amp;&gt;::value</code> is true for the second type <code>T1</code> in
<code>Types</code>.</del>
<p/>
[&hellip;]
<p/>
-26- <i>Remark:</i> This constructor shall not participate in overload resolution unless <del><code>U1</code> is implicitly convertible
to <code>T0</code> and <code>U2</code> is implicitly convertible to <code>T1</code></del><ins><code>sizeof...(Types) == 2
&amp;&amp; is_constructible&lt;T0, U1&gt;::value &amp;&amp; is_constructible&lt;T1, U2&gt;::value &amp;&amp; 
is_convertible&lt;U1, T0&gt;::value &amp;&amp; is_convertible&lt;U2, T1&gt;::value</code> is true</ins>.
</p>
</blockquote>
</blockquote>
</li>

</ol>
</blockquote>

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

<p>
STL provides improved wording.
</p>
<p><i>[2016-06 Oulu]</i></p>

<p>Tuesday: Adopt option 1, drop option B of <a href="lwg-defects.html#2549" title="Tuple EXPLICIT constructor templates that take tuple parameters end up taking references 
to temporaries and will create dangling references (Status: C++17)">2549</a><sup><a href="https://cplusplus.github.io/LWG/issue2549" title="Latest snapshot">(i)</a></sup>, and move to Ready.</p>
<p>Friday: status to Immediate</p>


<p id="res-2312"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4567.</p>

<ol>
<li><p>Edit 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a> as indicated:</p>

<blockquote>
<pre>
template &lt;class... UTypes&gt;
  <i>EXPLICIT</i> constexpr tuple(UTypes&amp;&amp;... u);
</pre>
<blockquote>
<p>
<del>-8- <i>Requires</i>: <code>sizeof...(Types) == sizeof...(UTypes)</code>.</del>
<p/>
[&hellip;]
<p/>
-10- <i>Remarks</i>: This constructor shall not participate in overload resolution unless <code>sizeof...(Types) &gt;= 1</code> 
and <ins><code>sizeof...(Types) == sizeof...(UTypes)</code> and</ins> <code>is_constructible&lt;<i>T<sub>i</sub></i>, 
<i>U<sub>i</sub></i>&amp;&amp;&gt;::value</code> is <code>true</code> for all <code><i>i</i></code>. The constructor is explicit 
if and only if <code>is_convertible&lt;<i>U<sub>i</sub></i>&amp;&amp;, <i>T<sub>i</sub></i>&gt;::value</code> is <code>false</code> 
for at least one <code><i>i</i></code>.
</p>
</blockquote>
<p>
[&hellip;]
</p>
<pre>
template &lt;class... UTypes&gt; <i>EXPLICIT</i> constexpr tuple(const tuple&lt;UTypes...&gt;&amp; u);
</pre>
<blockquote>
<p>
<del>-15- <i>Requires</i>: <code>sizeof...(Types) == sizeof...(UTypes)</code>.</del>
<p/>
[&hellip;]
<p/>
-17- <i>Remarks</i>: This constructor shall not participate in overload resolution unless 
<ins><code>sizeof...(Types) == sizeof...(UTypes)</code> and</ins> <code>is_constructible&lt;<i>T<sub>i</sub></i>, 
const <i>U<sub>i</sub></i>&amp;&gt;::value</code> is <code>true</code> for all <code><i>i</i></code>. The constructor is explicit 
if and only if <code>is_convertible&lt;const <i>U<sub>i</sub></i>&amp;, <i>T<sub>i</sub></i>&gt;::value</code> is <code>false</code> 
for at least one <code><i>i</i></code>.
</p>
</blockquote>
<pre>
template &lt;class... UTypes&gt; <i>EXPLICIT</i> constexpr tuple(tuple&lt;UTypes...&gt;&amp;&amp; u);
</pre>
<blockquote>
<p>
<del>-18- <i>Requires</i>: <code>sizeof...(Types) == sizeof...(UTypes)</code>.</del>
<p/>
[&hellip;]
<p/>
-20- <i>Remarks</i>: This constructor shall not participate in overload resolution unless 
<ins><code>sizeof...(Types) == sizeof...(UTypes)</code> and</ins> <code>is_constructible&lt;<i>T<sub>i</sub></i>, 
<i>U<sub>i</sub></i>&amp;&amp;&gt;::value</code> is <code>true</code> for all <code><i>i</i></code>. The constructor is explicit 
if and only if <code>is_convertible&lt;<i>U<sub>i</sub></i>&amp;&amp;, <i>T<sub>i</sub></i>&gt;::value</code> is <code>false</code> 
for at least one <code><i>i</i></code>.
</p>
</blockquote>
<pre>
template &lt;class U1, class U2&gt; <i>EXPLICIT</i> constexpr tuple(const pair&lt;U1, U2&gt;&amp; u);
</pre>
<blockquote>
<p>
<del>-21- <i>Requires</i>: <code>sizeof...(Types) == 2</code>.</del>
<p/>
[&hellip;]
<p/>
-23- <i>Remarks</i>: This constructor shall not participate in overload resolution unless 
<ins><code>sizeof...(Types) == 2</code> and</ins> <code>is_constructible&lt;<i>T<sub>0</sub></i>, 
const U1&amp;&gt;::value</code> is <code>true</code> and <code>is_constructible&lt;<i>T<sub>1</sub></i>, 
const U2&amp;&gt;::value</code> is <code>true</code>. The constructor is explicit 
if and only if <code>is_convertible&lt;const U1&amp;, <i>T<sub>0</sub></i>&gt;::value</code> is <code>false</code> 
or <code>is_convertible&lt;const U2&amp;, <i>T<sub>1</sub></i>&gt;::value</code> is <code>false</code>.
</p>
</blockquote>
<pre>
template &lt;class U1, class U2&gt; <i>EXPLICIT</i> constexpr tuple(pair&lt;U1, U2&gt;&amp;&amp; u);
</pre>
<blockquote>
<p>
<del>-24- <i>Requires</i>: <code>sizeof...(Types) == 2</code>.</del>
<p/>
[&hellip;]
<p/>
-26- <i>Remarks</i>: This constructor shall not participate in overload resolution unless 
<ins><code>sizeof...(Types) == 2</code> and</ins> <code>is_constructible&lt;<i>T<sub>0</sub></i>, 
U1&amp;&amp;&gt;::value</code> is <code>true</code> and <code>is_constructible&lt;<i>T<sub>1</sub></i>, 
U2&amp;&amp;&gt;::value</code> is <code>true</code>. The constructor is explicit 
if and only if <code>is_convertible&lt;U1&amp;&amp;, <i>T<sub>0</sub></i>&gt;::value</code> is <code>false</code> 
or <code>is_convertible&lt;U2&amp;&amp;, <i>T<sub>1</sub></i>&gt;::value</code> is <code>false</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
