<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1324: Still too many implicit conversions for pair and  tuple</title>
<meta property="og:title" content="Issue 1324: Still too many implicit conversions for pair and  tuple">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1324.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#Resolved">Resolved</a> status.</em></p>
<h3 id="1324"><a href="lwg-defects.html#1324">1324</a>. Still too many implicit conversions for <code>pair</code> and  <code>tuple</code></h3>
<p><b>Section:</b> 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a>, 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2010-03-20 <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#pairs.pair">active issues</a> in [pairs.pair].</p>
<p><b>View all other</b> <a href="lwg-index.html#pairs.pair">issues</a> in [pairs.pair].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
In analogy to library defect <a href="lwg-defects.html#811" title="pair of pointers no longer works with literal 0 (Status: C++11)">811</a><sup><a href="https://cplusplus.github.io/LWG/issue811" title="Latest snapshot">(i)</a></sup>, <code>tuple</code>'s variadic constructor
</p>

<blockquote><pre>
template &lt;class... UTypes&gt;
explicit tuple(UTypes&amp;&amp;... u);
</pre></blockquote>

<p>
creates the same problem as pair:
</p>

<blockquote><pre>
#include &lt;tuple&gt;

int main()
{
  std::tuple&lt;char*&gt; p(0);
}
</pre></blockquote>

<p>
produces a similar compile error for a recent gcc implementation.
</p>

<p>
I suggest to follow the same resolution path as has been applied to
<code>pair</code>'s corresponding c'tor, that is require that these c'tors should
not participate in overload resolution, if the arguments are not implicitly
convertible to the element types.
</p>

<p>
Further-on both <code>pair</code> and <code>tuple</code> provide converting constructors
from different <code>pairs</code>/<code>tuples</code> that should be not available, if
the corresponding element types are not implicitly convertible. It seems
astonishing that in the following example
</p>

<blockquote><pre>
struct A {
  explicit A(int);
};

A  a = 1; <span style="color:#C80000">// Error</span>

std::tuple&lt;A&gt; ta = std::make_tuple(1); <span style="color:#C80000">// # OK?</span>
</pre></blockquote>

<p>
the initialization marked with # could be well-formed.
</p>

<p><i>[
Only constraints on constructors are suggested. Adding similar constraints on
assignment operators is considered as QoI, because the assigments wouldn't be
well-formed anyway.
]</i></p>


<ol>

<li>
<p>
Following 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a>/5 add a new Remarks element:
</p>

<blockquote><pre>
template&lt;class U, class V&gt; pair(const pair&lt;U, V&gt;&amp; p);
</pre>

<blockquote>
<p>
5 <i>Effects:</i> Initializes members from the corresponding members of the
argument<del>, performing implicit conversions as needed</del>.
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless <code>U</code> is implicitly convertible to <code>first_type</code>
and <code>V</code> is implicitly convertible to <code>second_type</code>.</ins>
</p>
</blockquote>
</blockquote>

</li>

<li>
<p>
Following 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a>/6 add a new Remarks element:
</p>

<blockquote><pre>
template&lt;class U, class V&gt; pair(pair&lt;U, V&gt;&amp;&amp; p);
</pre>

<blockquote>
<p>
6 <i>Effects:</i> The constructor initializes <code>first</code> with
<code>std::move(p.first)</code> and second with <code>std::move(p.second)</code>.
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless <code>U</code> is implicitly convertible to <code>first_type</code>
and <code>V</code> is implicitly convertible to <code>second_type</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Following 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/7 add a new Remarks element:
</p>

<blockquote><pre>
template &lt;class... UTypes&gt;
explicit tuple(UTypes&amp;&amp;... u);
</pre>

<blockquote>
<p>
6 <i>Requires:</i> Each type in <code>Types</code> shall satisfy the requirements of
<code>MoveConstructible</code> (Table 33) from the corresponding type in
<code>UTypes</code>. <code>sizeof...(Types) == sizeof...(UTypes)</code>.
</p>

<p>
7 <i>Effects:</i> Initializes the elements in the <code>tuple</code> with the
corresponding value in <code>std::forward&lt;UTypes&gt;(u)</code>.
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless each type in <code>UTypes</code> is implicitly convertible to its
corresponding type in <code>Types</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Following 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/13 add a new Remarks element:
</p>

<blockquote><pre>
template &lt;class... UTypes&gt; tuple(const tuple&lt;UTypes...&gt;&amp; u);
</pre>

<blockquote>
<p>
12 <i>Requires:</i> Each type in <code>Types</code> shall be constructible from the
corresponding type in <code>UTypes</code>. <code>sizeof...(Types) ==
sizeof...(UTypes)</code>.
</p>

<p>
13 <i>Effects:</i> Constructs each element of <code>*this</code> with the
corresponding element of <code>u</code>.
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless each type in <code>UTypes</code> is implicitly convertible to its
corresponding type in <code>Types</code>.</ins>
</p>

<p>
14 [<i>Note:</i> <code>enable_if</code> can be used to make the converting
constructor and assignment operator exist only in the cases where the source and
target have the same number of elements. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Following 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/16 add a new Remarks element:
</p>

<blockquote><pre>
template &lt;class... UTypes&gt; tuple(tuple&lt;UTypes...&gt;&amp;&amp; u);
</pre>

<blockquote>
<p>
15 <i>Requires:</i> Each type in <code>Types</code> shall shall satisfy the
requirements of <code>MoveConstructible</code> (Table 33) from the corresponding
type in <code>UTypes</code>. <code>sizeof...(Types) == sizeof...(UTypes)</code>.
</p>

<p>
16 <i>Effects:</i> Move-constructs each element of <code>*this</code> with the
corresponding element of <code>u</code>.
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless each type in <code>UTypes</code> is implicitly convertible to its
corresponding type in <code>Types</code>.</ins>
</p>

<p>
[<i>Note:</i> <code>enable_if</code> can be used to make the converting constructor
and assignment operator exist only in the cases where the source and target have
the same number of elements. &mdash; <i>end note</i>]
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Following 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/18 add a new Remarks element:
</p>

<blockquote><pre>
template &lt;class U1, class U2&gt; tuple(const pair&lt;U1, U2&gt;&amp; u);
</pre>

<blockquote>
<p>
17 <i>Requires:</i> The first type in <code>Types</code> shall be constructible from
<code>U1</code> and the second type in <code>Types</code> shall be constructible from
<code>U2</code>. <code>sizeof...(Types) == 2</code>.
</p>

<p>
18 <i>Effects:</i> Constructs the first element with <code>u.first</code> and the
second element with <code>u.second</code>.
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless <code>U1</code> is implicitly convertible to the first type in
<code>Types</code> and <code>U2</code> is implicitly convertible to the second type in
<code>Types</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>

<li>
<p>
Following 22.4.4.2 <a href="https://wg21.link/tuple.cnstr">[tuple.cnstr]</a>/20 add a new Remarks element:
</p>

<blockquote><pre>
template &lt;class U1, class U2&gt; tuple(pair&lt;U1, U2&gt;&amp;&amp; u);
</pre>

<blockquote>
<p>
19 <i>Requires:</i> The first type in <code>Types</code> shall shall satisfy the
requirements of <code>MoveConstructible</code>(Table 33) from <code>U1</code> and the
second type in <code>Types</code> shall be move-constructible from <code>U2</code>.
<code>sizeof...(Types) == 2</code>.
</p>

<p>
20 <i>Effects:</i> Constructs the first element with <code>std::move(u.first)</code>
and the second element with <code>std::move(u.second)</code>
</p>

<p>
<ins><i>Remarks:</i> This constructor shall not participate in overload
resolution unless <code>U1</code> is implicitly convertible to the first type in
<code>Types</code> and <code>U2</code> is implicitly convertible to the second type in
<code>Types</code>.</ins>
</p>
</blockquote>
</blockquote>
</li>

</ol>
<p><i>[
2010-10-24 Daniel adds:
]</i></p>


<blockquote><p>
Accepting 
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3140.html">n3140</a> 
would solve this issue.
</p></blockquote>


<p id="res-1324"><b>Proposed resolution:</b></p><p>
See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3140.html">n3140</a>.
</p>




</body>
</html>
