<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3562: Superseding nullopt_t's requirement to not be DefaultConstructible</title>
<meta property="og:title" content="Issue 3562: Superseding nullopt_t's requirement to not be DefaultConstructible">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3562.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="3562"><a href="lwg-closed.html#3562">3562</a>. Superseding <code>nullopt_t</code>'s requirement to not be <i>DefaultConstructible</i></h3>
<p><b>Section:</b> 22.5.5 <a href="https://wg21.link/optional.nullopt">[optional.nullopt]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> David Friberg <b>Opened:</b> 2021-06-04 <b>Last modified:</b> 2021-06-14</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#optional.nullopt">issues</a> in [optional.nullopt].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
22.5.5 <a href="https://wg21.link/optional.nullopt">[optional.nullopt]</a> p2 requires that <code>nullopt_t</code> shall not have a default constructor 
([&hellip;] or an initializer-list constructor, and shall not be an aggregate). However, after the final 
resolution of LWG issue <a href="lwg-defects.html#2510" title="Tag types should not be DefaultConstructible (Status: C++17)">2510</a><sup><a href="https://cplusplus.github.io/LWG/issue2510" title="Latest snapshot">(i)</a></sup> and <a href="https://wg21.link/cwg1518">CWG 1518</a> it seems 
as if the following synopsis would suffice:
</p>
<blockquote><pre>
struct nullopt_t { explicit nullopt_t() = default; };
</pre></blockquote>
<p>
Which would also be consistent with other tag types.
<p/>
22.5.5 <a href="https://wg21.link/optional.nullopt">[optional.nullopt]</a> p2 in its current form was written prior to the final resolution of LWG issue 
<a href="lwg-defects.html#2510" title="Tag types should not be DefaultConstructible (Status: C++17)">2510</a><sup><a href="https://cplusplus.github.io/LWG/issue2510" title="Latest snapshot">(i)</a></sup> and <a href="https://wg21.link/cwg1518">CWG 1518</a>, and is arguably based on using an 
earlier proposed approach for tag types which was later superseded.
<p/>
<b>Details:</b>
<p/>
<a href="https://wg21.link/P0032R3" title=" Homogeneous interface for variant, any and optional (Revision 3)">P0032R3</a> was part of introducing <code>optional</code> and contained a discussion around the 
<code>nullopt_t</code> type, tag types in general, and the <i>DefaultConstructible</i> requirement. Particularly, 
from section 'Not assignable from <code>{}</code>':
</p>
<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"><p>
To re-enforce this design, there is an pending issue 2510-Tag types should not be DefaultConstructible Core issue 2510.
</p></blockquote>
<p>
At the time of <a href="https://wg21.link/P0032R3" title=" Homogeneous interface for variant, any and optional (Revision 3)">P0032R3</a>, the resolution of LWG issue <a href="lwg-defects.html#2510" title="Tag types should not be DefaultConstructible (Status: C++17)">2510</a><sup><a href="https://cplusplus.github.io/LWG/issue2510" title="Latest snapshot">(i)</a></sup> was indeed for all tag types
to not be <i>DefaultConstructible</i>, but this resolution was later superseded as part of the resolution of 
CWG issue <a href="https://wg21.link/cwg1518">CWG 1518</a>, which in turn reverted parts of the resolution of 
CWG issue <a href="https://wg21.link/cwg1630">CWG 1630</a> (which "went too far in allowing use of explicit 
constructors for default initialization").
<p/>
The final resolution for CWG 1518 (<a href="https://wg21.link/P0398R0" title=" Core issue 1518: Explicit default constructors and copy-list-initialization">P0398R0</a>) lead to the requirement for aggregate classes to not 
have any explicit [user-declared] constructors, and LWG <a href="lwg-defects.html#2510" title="Tag types should not be DefaultConstructible (Status: C++17)">2510</a><sup><a href="https://cplusplus.github.io/LWG/issue2510" title="Latest snapshot">(i)</a></sup> was resolved accordingly by making all 
tag types have explicit user-declared (constexpr) default constructors.
<p/>
However, the spec of <code>nullopt_t</code> never changed after these events or as part of updating other tag types, 
and it is still explicitly required to not be <i>DefaultConstructible</i> (nor have a initialization-list constructor) 
and to not be an aggregate, whereas the implementation of it is otherwise unspecified.
<p/>
We may note that we still see some compiler variance on the topic of CWG issues 1518 &amp; 1630, see this
<a href="https://godbolt.org/z/Tf96s4Krf">godbolt link</a>.
</p>
<p><i>[2021-06-14 Reflector poll:
Current wording prevents an implicit conversion sequence from <code>{}</code>.
Status changed: New &rarr; NAD.]</i></p>



<p id="res-3562"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4885">N4885</a>.
</p>

<ol>
<li><p>Modify 22.5.2 <a href="https://wg21.link/optional.syn">[optional.syn]</a>, header <code>&lt;optional&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]

<i>// 22.5.5 <a href="https://wg21.link/optional.nullopt">[optional.nullopt]</a>, no-value state indicator</i>
struct nullopt_t{<del><i>see below</i></del> <ins>explicit nullopt_t() = default;</ins> };
inline constexpr nullopt_t nullopt<ins>{}</ins><del>(<i>unspecified</i>)</del>;

[&hellip;]
</pre>
</blockquote>
</li>

<li><p>Modify 22.5.5 <a href="https://wg21.link/optional.nullopt">[optional.nullopt]</a> as indicated:</p>

<blockquote>
<blockquote>
<pre>
struct nullopt_t{<del><i>see below</i></del> <ins>explicit nullopt_t() = default;</ins> };
inline constexpr nullopt_t nullopt<ins>{}</ins><del>(<i>unspecified</i>)</del>;
</pre>
</blockquote>
<p>
-1- The struct <code>nullopt_t</code> is an empty class type used as a unique type to indicate the state 
of not containing a value for <code>optional</code> objects. In particular, <code>optional&lt;T&gt;</code> 
has a constructor with <code>nullopt_t</code> as a single argument; this indicates that an optional object 
not containing a value shall be constructed.
<p/>
<del>-2- Type <code>nullopt_t</code> shall not have a default constructor or an initializer-list constructor, 
and shall not be an aggregate.</del>
</p>
</blockquote>
</li>

</ol>




</body>
</html>
