<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=utf8">

<title>Resolving Core Issue #1331 (const mismatch with defaulted copy constructor)</title>

<style type="text/css">
  code {
    white-space:pre;
  }
  p {
    text-align:justify;
  }
  ins {
    background-color:#A0FFA0;
  }
  del {
    background-color:#FFA0A0;
  }
  blockquote.std {
    background-color:#F0F0F0;
  }
  blockquote.std ul {
    list-style-type: '— ';
  }
</style>
</head><body>
<address style="text-align: left;">
Document number: P0641R0<br/>
Date: 2017-04-17<br/>
Project: Programming Language C++<br/>
Audience: Evolution Working Group<br/>
Reply-to: <a href="mailto:daniel.kruegler@gmail.com">Daniel Kr&uuml;gler</a>, <a href="mailto:botond@mozilla.com">Botond Ballo</a>
</address>
<hr>
<h1 style="text-align: center;">Resolving Core Issue #1331 (<code>const</code> mismatch with defaulted copy constructor)</h1>

<ul>
<li><a href="#Motivation">Motivation</a></li>
<li><a href="#Analysis">Analysis</a></li>
<li><a href="#Proposed_resolution">Proposed Resolution</a></li>
<li><a href="#Proposed_wording">Proposed Wording</a></li>
<li><a href="#Issues_resolved">Issues Resolved</a></li>
</ul>

<h2><a name="Motivation"></a>Motivation</h2>
<p>
If you have a class with a copy constructor whose parameter type is reference-to-nonconst:</p>
<code>
  struct MyType {
      MyType(MyType&amp;);  // no 'const'
  };
</code>

<p>and you try to aggregate it in a class that has a defaulted copy constructor with the usual signature:</p>
<code>
  template &lt;typename T&gt;
  struct Wrapper {
      Wrapper(const Wrapper&amp;) = default;
      T t;
  };
  
  Wrapper&lt;MyType&gt; var;  // fails to instantiate
</code>

<p>the resulting type is ill-formed, even if it is never used in a context where the copy constructor is required.</p>

<p>The authors believe this is unnecessarily restrictive; one should be able to use <code>Wrapper&lt;MyType&gt;</code> as 
long as one does not try to copy it.</p>

<p><code>std::tuple</code> is an example of a wrapper type which suffers from this problem in popular implementations.</p>

<h2><a name="Analysis"></a>Analysis</h2>

The relevant standard wording can be found in 8.4.2 [dcl.fct.def.deault] paragraph 1 (emphasis ours):

<blockquote class="std">
  <ol>
    <li>[...] A function that is explicitly defaulted shall:
      <ul>
        <li>be a special member function,</li>
        <li>have the <strong>same declared function type</strong> (except for possibly differing ref-qualifiers and except that in
            the case of a copy constructor or copy assignment operator, the parameter type may be "reference to
            non-const T", where T is the name of the member function's class) <strong>as if it had been implicitly declared</strong>,
            and</li>
        <li>not have default arguments.</li>
      </ul>
    </li>
  </ol>
</blockquote>

<p>In the case of <code>Wrapper&lt;MyType&gt;</code>, the implicitly declared copy constructor would have the parameter type
<code>MyType&amp;</code> per 12.8.1 [class.copy.ctor] paragraph 7, so an explicitly defaulted copy constructor with the
parameter type <code>const MyType&amp;</code> is ill-formed.</p>

<h2><a name="Proposed_resolution"></a>Proposed Resolution</h2>

<p>This proposal suggests that if the declared type of an explicitly defaulted function is not the same as if it had been
implicitly declared, then it be defined as deleted, rather than being ill-formed.</p>

<h2><a name="Proposed_wording"></a>Proposed Wording</h2>

<blockquote class="std">
  <ol>
    <li>[...] A function that is explicitly defaulted shall:
      <ul>
        <li>be a special member function,</li>
        <li><del>have the same declared function type (except for possibly differing ref-qualifiers and except that in
            the case of a copy constructor or copy assignment operator, the parameter type may be "reference to
            non-const T", where T is the name of the member function's class) as if it had been implicitly declared,
            and</del></li>
        <li>not have default arguments.</li>
      </ul>
    </li>
    <li><ins>If an explicitly defaulted function does not have the same declared function type (except for possibly 
             differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, 
             the parameter type may be "reference to non-const T", where T is the name of the member function's class) 
             as if it had been implicitly declared, it is defined as deleted.</ins></li>
  </ol>
</blockquote>

<h2><a name="Issues_resolved"></a>Issues Resolved</h2>

<p>This proposal would resolve Core Issues <a href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1331">#1331</a> 
and <a href="http://open-std.org/JTC1/SC22/WG21/docs/cwg_closed.html#1426">#1426</a>, both of which are in Extension status,
and are tracked by corresponding Evolution Issues <a href="http://cplusplus.github.io/EWG/ewg-active.html#101">#101</a> and
<a href="http://cplusplus.github.io/EWG/ewg-closed.html#103">#103</a>, respectively.</p>

</body></html>