<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2549: Tuple EXPLICIT constructor templates that take tuple parameters end up taking references 
to temporaries and will create dangling references</title>
<meta property="og:title" content="Issue 2549: Tuple EXPLICIT constructor templates that take tuple parameters end up taking references 
to temporaries and will create dangling references">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2549.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="2549"><a href="lwg-defects.html#2549">2549</a>. Tuple <i>EXPLICIT</i> constructor templates that take <code>tuple</code> parameters end up taking references 
to temporaries and will create dangling references</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> Ville Voutilainen <b>Opened:</b> 2015-10-11 <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 this example:
</p>
<blockquote><pre>
#include &lt;utility&gt;
#include &lt;tuple&gt;

struct X {
  int state; // this has to be here to not allow tuple
             // to inherit from an empty X.
  X() {
  }

  X(X const&amp;) {
  }

  X(X&amp;&amp;) {
  }

  ~X() {
  }
};

int main()
{
  X v;
  std::tuple&lt;X&gt; t1{v};
  std::tuple&lt;std::tuple&lt;X&gt;&amp;&amp;&gt; t2{std::move(t1)}; // #1
  std::tuple&lt;std::tuple&lt;X&gt;&gt; t3{std::move(t2)}; // #2
}
</pre></blockquote>
<p>
The line marked with <code>#1</code> will use the constructor
<code>template &lt;class... UTypes&gt; <i>EXPLICIT</i> constexpr tuple(tuple&lt;UTypes...&gt;&amp;&amp; u);</code>
and will construct a temporary and bind an rvalue reference to it.
The line marked with <code>#2</code> will move from a dangling reference.
<p/>
In order to solve the problem, the constructor templates taking tuples
as parameters need additional SFINAE conditions that SFINAE
those constructor templates away when <code>Types...</code> is constructible
or convertible from the incoming tuple type and <code>sizeof...(Types)</code> equals
one. libstdc++ already has this fix applied.
<p/>
There is an additional check that needs to be done, in order to avoid
infinite meta-recursion during overload resolution, for a case where
the element type is itself constructible from the target tuple. An example
illustrating that problem is as follows:
</p>
<blockquote><pre>
#include &lt;tuple&gt;

struct A
{
  template &lt;typename T&gt;
  A(T)
  {
  }

  A(const A&amp;) = default;
  A(A&amp;&amp;) = default;
  A&amp; operator=(const A&amp;) = default;
  A&amp; operator=(A&amp;&amp;) = default;
  ~A() = default;
};

int main()
{
  auto x = A{7};
  std::make_tuple(x);
}
</pre></blockquote>
<p>
I provide two proposed resolutions, one that merely has a note encouraging
trait-based implementations to avoid infinite meta-recursion, and a second
one that avoids it normatively (implementations can still do things differently
under the as-if rule, so we are not necessarily overspecifying how to do it.)
</p>

<p><i>[2016-02-17, Ville comments]</i></p>

<p>
It was pointed out at <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69853">gcc bug 69853</a>
that the fix for LWG 2549 is a breaking change. That is, it breaks code
that expects constructors inherited from <code>tuple</code> to provide an implicit
base-to-derived conversion. I think that's just additional motivation
to apply the fix; that conversion is very much undesirable. The example
I wrote into the bug report is just one example of a very subtle temporary
being created.
</p>

<p><strong>Previous resolution from Ville [SUPERSEDED]:</strong></p>
<blockquote class="note">
<ol style="list-style-type:upper-alpha">
<li><p><b>Alternative 1:</b></p>
<ol>
<li><p>In 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/17, edit as follows:</p>
<blockquote>
<pre>
template &lt;class... UTypes> <i>EXPLICIT</i> constexpr tuple(const tuple&lt;UTypes...&gt;&amp; u);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-17- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p><code>is_constructible&lt;T<sub><i>i</i></sub>, const Ui&amp;&gt;::value</code> is true for all 
<code><i>i</i></code><del>.</del><ins>, and</ins></p></li>
<li><p><ins><code>sizeof...(Types) != 1</code>, or <code>is_convertible&lt;const tuple&lt;UTypes...&gt;&amp;,
Types...&gt;::value</code> is false and <code>is_constructible&lt;Types..., const tuple&lt;UTypes...&gt;&amp;&gt;::value</code>
is false.</ins></p></li>
</ul>
<p>
<ins>
[<i>Note</i>: to avoid infinite template recursion in a trait-based implementation
for the case where <code>UTypes...</code> is a single type that has a constructor template that accepts
<code>tuple&lt;Types...&gt;</code>, implementations need to additionally check that
<code>remove_cv_t&lt;remove_reference_t&lt;const tuple&lt;UTypes...&gt;&amp;&gt;&gt;</code> and
<code>tuple&lt;Types...&gt;</code> are not the same type. &mdash; <i>end note</i>]
</ins>
<p/>
The constructor is explicit if and only if <code>is_convertible&lt;const U<sub><i>i</i></sub>&amp;, 
T<sub><i>i</i></sub>&gt;::value</code> is false for at least one <code><i>i</i></code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>In 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/20, edit as follows:</p>

<blockquote>
<pre>
template &lt;class... UTypes&gt; <i>EXPLICIT</i> constexpr tuple(tuple&lt;UTypes...&gt;&amp;&amp; u);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-20- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p><code>is_constructible&lt;T<sub><i>i</i></sub>, U<sub><i>i</i></sub>&amp;&amp;&gt;::value</code> is true for all 
<code><i>i</i></code><del>.</del><ins>, and</ins></p></li>
<li><p><ins><code>sizeof...(Types) != 1</code>, or <code>is_convertible&lt;tuple&lt;UTypes...&gt;&amp;&amp;,
Types...&gt;::value</code> is false and <code>is_constructible&lt;Types..., tuple&lt;UTypes...&gt;&amp;&amp;&gt;::value</code>
is false.</ins></p></li>
</ul>
<p>
<ins>[<i>Note</i>: to avoid infinite template recursion in a trait-based implementation
for the case where <code>UTypes...</code> is a single type that has a constructor template that accepts
<code>tuple&lt;Types...&gt;</code>, implementations need to additionally check that
<code>remove_cv_t&lt;remove_reference_t&lt;tuple&lt;UTypes...&gt;&amp;&amp;&gt;&gt;</code> and
<code>tuple&lt;Types...&gt;</code> are not the same type. &mdash; <i>end note</i>]</ins>
<p/>
The constructor is explicit if and only if <code>is_convertible&lt;U<sub><i>i</i></sub>&amp;&amp;, 
T<sub><i>i</i></sub>&gt;::value</code> is false for at least one <code><i>i</i></code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>

</li>

<li><p><b>Alternative 2 (do the sameness-check normatively):</b></p>

<ol>
<li><p>In 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/17, edit as follows:</p>
<blockquote>
<pre>
template &lt;class... UTypes> <i>EXPLICIT</i> constexpr tuple(const tuple&lt;UTypes...&gt;&amp; u);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-17- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p><code>is_constructible&lt;T<sub><i>i</i></sub>, const Ui&amp;&gt;::value</code> is true for all 
<code><i>i</i></code><del>.</del><ins>, and</ins></p></li>
<li><p><ins><code>sizeof...(Types) != 1</code>, or <code>is_convertible&lt;const tuple&lt;UTypes...&gt;&amp;,
Types...&gt;::value</code> is false and <code>is_constructible&lt;Types..., const tuple&lt;UTypes...&gt;&amp;&gt;::value</code>
is false and <code>is_same&lt;tuple&lt;Types...&gt;, remove_cv_t&lt;remove_reference_t&lt;const
tuple&lt;UTypes...&gt;&amp;&gt;&gt;&gt;::value</code> is false.</ins></p></li>
</ul>
<p>
The constructor is explicit if and only if <code>is_convertible&lt;const U<sub><i>i</i></sub>&amp;, 
T<sub><i>i</i></sub>&gt;::value</code> is false for at least one <code><i>i</i></code>.
</p>
</blockquote>
</blockquote>
</li>

<li><p>In 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/20, edit as follows:</p>

<blockquote>
<pre>
template &lt;class... UTypes&gt; <i>EXPLICIT</i> constexpr tuple(tuple&lt;UTypes...&gt;&amp;&amp; u);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-20- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p><code>is_constructible&lt;T<sub><i>i</i></sub>, U<sub><i>i</i></sub>&amp;&amp;&gt;::value</code> is true for all 
<code><i>i</i></code><del>.</del><ins>, and</ins></p></li>
<li><p><ins><code>sizeof...(Types) != 1</code>, or <code>is_convertible&lt;tuple&lt;UTypes...&gt;&amp;&amp;,
Types...&gt;::value</code> is false and <code>is_constructible&lt;Types..., tuple&lt;UTypes...&gt;&amp;&amp;&gt;::value</code>
is false and <code>is_same&lt;tuple&lt;Types...&gt;, 
remove_cv_t&lt;remove_reference_t&lt;tuple&lt;UTypes...&gt;&amp;&amp;&gt;&gt;&gt;::value</code> is
false.</ins></p></li>
</ul>
<p>
The constructor is explicit if and only if <code>is_convertible&lt;U<sub><i>i</i></sub>&amp;&amp;, 
T<sub><i>i</i></sub>&gt;::value</code> is false for at least one <code><i>i</i></code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>

</li>
</ol>
</blockquote>

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

<p>
STL provides a simplification of Ville's alternative #2 (with no semantic changes), and it's shipping in VS 2015 Update 2.
</p>


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

<ol style="list-style-type:upper-alpha">
<li>
<p>
This approach is orthogonal to the Proposed Resolution for LWG <a href="lwg-defects.html#2312" title="tuple's constructor constraints need to be phrased more precisely (Status: C++17)">2312</a><sup><a href="https://cplusplus.github.io/LWG/issue2312" title="Latest snapshot">(i)</a></sup>:
</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(const tuple&lt;UTypes...&gt;&amp; u);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-17- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p>
<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><ins>, and</ins>
</p></li>
<li><p>
<ins><code>sizeof...(Types) != 1</code>, or (when <code>Types...</code> expands to <code>T</code> and
<code>UTypes...</code> expands to <code>U</code>) <code>!is_convertible_v&lt;const tuple&lt;U&gt;&amp;, T&gt; &amp;&amp;
!is_constructible_v&lt;T, const tuple&lt;U&gt;&amp;&gt; &amp;&amp; !is_same_v&lt;T, U&gt;</code> is <code>true</code></ins>.
</p></li>
</ul>
<p>
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>
[&hellip;]
<p/>
-20- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p>
<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><ins>, and</ins>
</p></li>
<li><p>
<ins><code>sizeof...(Types) != 1</code>, or (when <code>Types...</code> expands to <code>T</code> and
<code>UTypes...</code> expands to <code>U</code>) <code>!is_convertible_v&lt;tuple&lt;U&gt;, T&gt; &amp;&amp;
!is_constructible_v&lt;T, tuple&lt;U&gt;&gt; &amp;&amp; !is_same_v&lt;T, U&gt;</code> is <code>true</code></ins>.
</p></li>
</ul>
<p> 
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>
</blockquote>
</li>
</ol>
</li>

<li><p>This approach is presented as a merge with the parts of the Proposed Resolution for LWG <a href="lwg-defects.html#2312" title="tuple's constructor constraints need to be phrased more precisely (Status: C++17)">2312</a><sup><a href="https://cplusplus.github.io/LWG/issue2312" title="Latest snapshot">(i)</a></sup>
with overlapping modifications in the same paragraph, to provide editorial guidance if <a href="lwg-defects.html#2312" title="tuple's constructor constraints need to be phrased more precisely (Status: C++17)">2312</a><sup><a href="https://cplusplus.github.io/LWG/issue2312" title="Latest snapshot">(i)</a></sup> would be 
accepted.</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(const tuple&lt;UTypes...&gt;&amp; u);
</pre>
<blockquote>
<p>
[&hellip;]
<p/>
-17- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p>
<ins><code>sizeof...(Types) == sizeof...(UTypes)</code>, and</ins>
</p></li>
<li><p>
<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><ins>, and</ins>
</p></li>
<li><p>
<ins><code>sizeof...(Types) != 1</code>, or (when <code>Types...</code> expands to <code>T</code> and
<code>UTypes...</code> expands to <code>U</code>) <code>!is_convertible_v&lt;const tuple&lt;U&gt;&amp;, T&gt; &amp;&amp;
!is_constructible_v&lt;T, const tuple&lt;U&gt;&amp;&gt; &amp;&amp; !is_same_v&lt;T, U&gt;</code> is <code>true</code></ins>.
</p></li>
</ul>
<p>
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>
[&hellip;]
<p/>
-20- <i>Remarks</i>: This constructor shall not participate in overload resolution unless
</p>
<ul>
<li><p>
<ins><code>sizeof...(Types) == sizeof...(UTypes)</code>, and</ins>
</p></li>
<li><p>
<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><ins>, and</ins>
</p></li>
<li><p>
<ins><code>sizeof...(Types) != 1</code>, or (when <code>Types...</code> expands to <code>T</code> and
<code>UTypes...</code> expands to <code>U</code>) <code>!is_convertible_v&lt;tuple&lt;U&gt;, T&gt; &amp;&amp;
!is_constructible_v&lt;T, tuple&lt;U&gt;&gt; &amp;&amp; !is_same_v&lt;T, U&gt;</code> is <code>true</code></ins>.
</p></li>
</ul>
<p>
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>
</blockquote>
</li>
</ol>

</li>
</ol>





</body>
</html>
