<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2068: std::pair not C++03-compatible with defaulted copy c'tor</title>
<meta property="og:title" content="Issue 2068: std::pair not C++03-compatible with defaulted copy c'tor">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2068.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#NAD">NAD</a> status.</em></p>
<h3 id="2068"><a href="lwg-closed.html#2068">2068</a>. <code>std::pair</code> not C++03-compatible with defaulted copy c'tor</h3>
<p><b>Section:</b> 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2011-06-18 <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#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The specification of the copy semantics of the C++03 version of <code>std::pair</code>
is defined by the class synopsis in [lib.pairs]:
</p>
<blockquote><pre>
template &lt;class T1, class T2&gt;
struct pair {
  typedef T1 first_type;
  typedef T2 second_type;

  T1 first;
  T2 second;
  pair();
  pair(const T1&amp; x, const T2&amp; y);
  template&lt;class U, class V&gt; pair(const pair&lt;U, V&gt; &amp;p);
};
</pre></blockquote>
<p>
The effect of this specification is, that the copy constructor is compiler-declared
with the proper form depending on the contained member types. In particular, the
instantiation of <code>pair</code> is well-formed with an element type that has a
copy constructor with non-const first parameter type like specialzations of <code>auto_ptr</code>
or any user-defined type like the following one:
</p>
<blockquote><pre>
struct A {
  A(A&amp;){}
};
</pre></blockquote>
<p>
In contrast to container types which require <code>CopyConstructible</code> value types, the C++03 <code>pair</code> 
does support these, albeit unusual, element types.
<p/>
The FDIS version of the <code>std::pair</code> specification does specify the same semantics by 
defaulting the copy and move constructor in 22.3.2 <a href="https://wg21.link/pairs.pair">[pairs.pair]</a>:
</p>
<blockquote><pre>
template &lt;class T1, class T2&gt;
struct pair {
  typedef T1 first_type;
  typedef T2 second_type;

  T1 first;
  T2 second;
  <span style="color:#C80000">pair(const pair&amp;) = default;</span>
  <span style="color:#C80000">pair(pair&amp;&amp;) = default;</span>
  pair();
  [&hellip;]
};
</pre></blockquote>
<p>
But according to the current core rules this makes the instantiation of e.g. <code>std::pair&lt;A, int&gt;</code>
ill-formed, because of the <code>const</code> mismatch of the compiler-declared form of the copy constructor
with that of the defaulted declaration.
<p/>
Unfortunately there seems to be no simple library solution for this problem. If the defaulted declarations
were removed, both copy c'tor and move c'tor would be <b>deleted</b>, because there exist user-declared
copy assignment and move assignment operators in the FDIS. But these operations need to be user-defined 
to realize the wanted semantics of these operations for element types that are reference types. If core
rules would not be changed to fix that, I see the following options:
</p>
<ol>
<li>Intentionally decide to break the support for element types with non-const copy c'tors in <code>pair</code>.</li>
<li>User-declare both copy and move ctor to at least support the instantiation of the <code>pair</code> specializations, 
but this would still not allow to copy them by the copy constructor.</li>
<li>User-declare both the const and non-const copy ctors, the move ctor, and additionally the non-const copy assignment
operator to support the instantiation of the <code>pair</code> specializations and of these members. This would 
support all element types as it did in C++03, but all copy&#47;move members would be non-trivial.</li>
<li>Intentionally decide to give up support for element types that are references for <code>pair</code>, but
still keep the allocator support with the effect of removing all declarations of the special
copy&#47;move members. User code that needs to use <code>tuple</code> instead. But this would be a rather
drastic step requiring further corrections of the draft, e.g. a change of the signature of the algorithm
<code>minmax</code> (not the overload with the <code>initializer_list</code>) with a different return type.</li>
</ol>
<p>
This problem does <b>not</b> extend as backward-compatibility problem to <code>tuple</code>, because the TR1 
specification did explicitly declare copy constructor and copy assignment operator via the &quot;normal&quot; 
form:
</p>
<blockquote><pre>
tuple(const tuple&amp;);
tuple&amp; operator=(const tuple&amp;);
</pre></blockquote>
<p>
</p>

<p><i>[Bloomington, 2011]</i></p>

<p>
Closed as NAD.
</p>

<p>
This is an unfortunate change of behavior between C++03 and C++11, but is consistent with <code>tuple</code>.  There is no desire to go to lengths supporting types like <code>auto_ptr</code> now that rvalue references are in the language.
</p>

<p>
There may be an issue for Core/EWG to look at, so that some simple <code>=default</code> syntax could be used that would do the right thing.  If such a facility became availabile, LWG might revisit this issue.
</p>



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





</body>
</html>
