<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4015: LWG 3973 broke const overloads of std::optional monadic operations</title>
<meta property="og:title" content="Issue 4015: LWG 3973 broke const overloads of std::optional monadic operations">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4015.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#Open">Open</a> status.</em></p>
<h3 id="4015"><a href="lwg-active.html#4015">4015</a>. LWG 3973 broke <code>const</code> overloads of <code>std::optional</code> monadic operations</h3>
<p><b>Section:</b> 22.5.3.8 <a href="https://wg21.link/optional.monadic">[optional.monadic]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2023-11-24 <b>Last modified:</b> 2024-10-02</p>
<p><b>Priority: </b>1
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The resolution of LWG <a href="lwg-defects.html#3973" title="Monadic operations should be ADL-proof (Status: WP)">3973</a><sup><a href="https://cplusplus.github.io/LWG/issue3973" title="Latest snapshot">(i)</a></sup> (adopted in Kona) changed all
occurrences of <code>value()</code> to <code>*val</code>.
The intention was not to change the meaning, just avoid the non-freestanding
<code>value()</code> function, and avoid ADL that would be caused by using
<code>**this</code>.
However, in the <code>const</code> overloads such as
<code>and_then(F&amp;&amp;) const</code> the type of <code>value()</code>
was <code>const T&amp;</code>, but the type of <code>*val</code> is always
<code>T&amp;</code>. This implies that the const overloads invoke the callable
with a non-const argument, which is incorrect (and would be undefined
behaviour for a <code>const std::optional&lt;T&gt;</code>).

</p>
<p>
On the LWG reflector it was suggested that we should rewrite the specification
of <code>std::optional</code> to stop using an exposition-only data member
of type <code>T*</code>. No such member ever exists in real implemetations,
so it is misleading and leads to specification bugs of this sort.
</p>
<p>
Change the class definition in 22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a>
to use a union, and update every use of <code>val</code> accordingly
throughout 22.5.3 <a href="https://wg21.link/optional.optional">[optional.optional]</a>.
For consistency with 22.8.6.1 <a href="https://wg21.link/expected.object.general">[expected.object.general]</a> we might
also want to introduce a <code>bool has_val</code> member and refer to
that in the specification.
</p>
<blockquote>
<pre><code>
  private:
    <del>T *val;         <em>// exposition only</em></del>
    <ins>bool has_val;   <em>// exposition only</em></ins>
    <ins>union {</ins>
      <ins>T val;        <em>// exposition only</em></ins>
    <ins>};</ins>
  };
</code></pre>
</blockquote>
<p>For example, in 22.5.3.9 <a href="https://wg21.link/optional.mod">[optional.mod]</a>:</p>
<blockquote>
<p>
-1- <i>Effects</i>:
If <code>*this</code> contains a value, calls
<code>val<del>-&gt;</del><ins>.</ins>T::~T()</code> to destroy the contained
value<ins> and sets <code>has_val</code> to <code>false</code></ins>;
otherwise no effect.
</p>
</blockquote>

<p><i>[2023-11-26; Daniel provides wording]</i></p>

<p>
The proposed wording is considerably influenced by that of the specification of <code>expected</code>, but
attempts to reduce the amount of changes to not perfectly mimic it. Although "the contained value" is
a magic word of power it seemed feasible and simpler to use the new exposition-only member <code><i>val</i></code>
directly in some (but not all) places, usually involved with initializations.
<p/>
Furthermore, I have only added "and sets <code><i>has_val</i></code> to <code>true/false</code>"
where either the <i>Effects</i> wording says "otherwise no effect" or in other cases if the postconditions
did not already say that indirectly. I also added extra mentioning of <code><i>has_val</i></code> changes in tables
where different cells had very different effects on that member (unless these cells specify postconditions),
to prevent misunderstanding.
</p>

<p><i>[2024-03-11; Reflector poll]</i></p>

<p>
Set priority to 1 after reflector poll in November 2023.
Six votes for 'Tentatively Ready' but enough uncertainty to deserve
discussion at a meeting.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>
This wording is relative to <a href="https://wg21.link/N4964" title=" Working Draft, Programming Languages — C++">N4964</a> <em>after</em> application of the wording of LWG <a href="lwg-defects.html#3973" title="Monadic operations should be ADL-proof (Status: WP)">3973</a><sup><a href="https://cplusplus.github.io/LWG/issue3973" title="Latest snapshot">(i)</a></sup>.
</p>

<ol>

<li><p>Modify 22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a>, class template <code>optional</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std {
  template&lt;class T&gt;
  class optional {
  public:
    using value_type = T;
    [&hellip;]
  private:
    <ins>bool <i>has_val</i>; // <i>exposition only</i>
    union {</ins>
      T <ins><i>val</i></ins><del>*val</del>; // <i>exposition only</i>
    <ins>};</ins>
  };

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

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

<blockquote>
<p>
-2- <ins>Member <code><i>has_val</i></code> indicates whether an <code>optional&lt;T&gt;</code> object contains a
value</ins><del>When an <code>optional&lt;T&gt;</code> object contains a value, member <code>val</code> points to
the contained value</del>.
</p>
</blockquote>
</li>

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

<blockquote class="note">
<p>
[<i>Drafting note</i>: Normatively, this subclause doesn't require any changes, but I'm suggesting to replace
phrases of the form "[&hellip;]initializes the contained value with"] by "[&hellip;]initializes <code><i>val</i></code> with"
as we do in 22.8.6.2 <a href="https://wg21.link/expected.object.cons">[expected.object.cons]</a>. I intentionally did not add extra
"and sets <code><i>has_val</i></code> to <code>true/false</code>" since those effects are already guaranteed by the postconditions]
</p>
</blockquote>

<blockquote>
<pre>
constexpr optional(const optional&amp; rhs);
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code><del>*</del>rhs<ins>.<i>val</i></ins></code>.
<p/>
-5- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
constexpr optional(optional&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-8- <i>Constraints</i>: [&hellip;]
<p/>
-9- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code>.
<code>rhs.has_value()</code> is unchanged.
<p/>
-10- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class... Args&gt; constexpr explicit optional(in_place_t, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-13- <i>Constraints</i>: [&hellip;]
<p/>
-14- <i>Effects</i>: Direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code>std::forward&lt;Args&gt;(args)...</code>.
<p/>
-15- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U, class... Args&gt;
  constexpr explicit optional(in_place_t, initializer_list&lt;U&gt; il, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-18- <i>Constraints</i>: [&hellip;]
<p/>
-19- <i>Effects</i>: Direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code>il, std::forward&lt;Args&gt;(args)...</code>.
<p/>
-20- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U = T&gt; constexpr explicit(<i>see below</i>) optional(U&amp;&amp; v);
</pre>
<blockquote>
<p>
-23- <i>Constraints</i>: [&hellip;]
<p/>
-24- <i>Effects</i>: Direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code>std::forward&lt;U&gt;(v)</code>.
<p/>
-25- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr explicit(<i>see below</i>) optional(const optional&lt;U&gt;&amp; rhs);
</pre>
<blockquote>
<p>
-28- <i>Constraints</i>: [&hellip;]
<p/>
-29- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code><del>*</del>rhs<ins>.<i>val</i></ins></code>.
<p/>
-30- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr explicit(<i>see below</i>) optional(optional&lt;U&gt;&amp;&amp; rhs);
</pre>
<blockquote>
<p>
-33- <i>Constraints</i>: [&hellip;]
<p/>
-34- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with
<code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code>. <code>rhs.has_value()</code> is unchanged.
<p/>
-35- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr ~optional();
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: If <code>is_trivially_destructible_v&lt;T&gt; != true</code> and <code>*this</code> contains a value,
calls <code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code><ins>.</ins>
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr optional&lt;T&gt;&amp; operator=(nullopt_t) noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: If <code>*this</code> contains a value, calls
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code> to destroy the contained
value <ins>and sets <code><i>has_val</i></code> to <code>false</code></ins>; otherwise no effect.
<p/>
-2- <i>Postconditions</i>: <code>*this</code> does not contain a value.
</p>
</blockquote>
<pre>
constexpr optional&lt;T&gt;&amp; operator=(const optional&amp; rhs);
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: See Table 58.
</p>
<table border="1">
<caption>Table 58 &mdash; <code>optional::operator=(const optional&amp;)</code> effects [tab:optional.assign.copy]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code><del>*</del>rhs<ins>.<i>val</i></ins></code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code><del>*</del>rhs<ins>.<i>val</i></ins></code><br/>
<ins>and sets <code><i>has_val</i></code> to <code>true</code></ins>
</td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling <code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code><br/>
<ins>and sets <code><i>has_val</i></code> to <code>false</code></ins>
</td>
<td>no effect</td>
</tr>
</table>
<p>
-5- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
constexpr optional&lt;T&gt;&amp; operator=(optional&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-8- <i>Constraints</i>: [&hellip;]
<p/>
-9- <i>Effects</i>: See Table 59. The result of the expression <code>rhs.has_value()</code> remains unchanged.
<p/>
-10- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
-11- <i>Returns</i>: <code>*this</code>.
</p>
<table border="1">
<caption>Table 59 &mdash; <code>optional::operator=(optional&amp;&amp;)</code> effects [tab:optional.assign.move]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del> with
<code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> <ins>and sets <code><i>has_val</i></code> to <code>true</code></ins></td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling<br/>
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code><ins>and sets <code><i>has_val</i></code> to <code>false</code></ins></td>
<td>no effect</td>
</tr>
</table>
<p>
-12- <i>Remarks</i>: [&hellip;]
<p/>
-13- If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code> remains
unchanged. If an exception is thrown during the call to <code>T</code>'s move constructor, the state
of <code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception
safety guarantee of <code>T</code>'s move constructor. If an exception is thrown during the call to
<code>T</code>'s move assignment, the state of <code><del>*</del><ins><i>val</i></ins><del>val</del></code>
and <code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception
safety guarantee of <code>T</code>'s move assignment.
</p>
</blockquote>
<pre>
template&lt;class U = T&gt; constexpr optional&lt;T&gt;&amp; operator=(U&amp;&amp; v);
</pre>
<blockquote>
<p>
-14- <i>Constraints</i>: [&hellip;]
<p/>
-15- <i>Effects</i>: If <code>*this</code> contains a value, assigns <code>std::forward&lt;U&gt;(v)</code>
to <ins><code><i>val</i></code></ins><del>the contained value</del>; otherwise direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>std::forward&lt;U&gt;(v)</code>.
<p/>
-16- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
-17- <i>Returns</i>: <code>*this</code>.
<p/>
-18- <i>Remarks</i>: If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code>
remains unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of
<code>v</code> is determined by the exception safety guarantee of <code>T</code>'s constructor. If an exception
is thrown during the call to <code>T</code>'s assignment, the state of <code><ins><i>val</i></ins><del>*val</del></code>
and <code>v</code> is determined by the exception safety guarantee of <code>T</code>'s assignment.
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr optional&lt;T&gt;&amp; operator=(const optional&lt;U&gt;&amp; rhs);
</pre>
<blockquote>
<p>
-19- <i>Constraints</i>: [&hellip;]
<p/>
-20- <i>Effects</i>: See Table 60.
</p>
<table border="1">
<caption>Table 60 &mdash; <code>optional::operator=(const optional&lt;U&gt;&amp;)</code> effects [tab:optional.assign.copy.templ]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code><del>*</del>rhs<ins>.<i>val</i></ins></code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code><del>*</del>rhs<ins>.<i>val</i></ins></code> <ins>and sets <code><i>has_val</i></code> to <code>true</code></ins></td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling<br/>
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code> <ins>and sets <code><i>has_val</i></code> to <code>false</code></ins></td>
<td>no effect</td>
</tr>
</table>
<p>
-21- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
-22- <i>Returns</i>: <code>*this</code>.
<p/>
-23- If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code> remains
unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s constructor. If an exception is thrown during the call to <code>T</code>'s
assignment, the state of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s assignment.
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr optional&lt;T&gt;&amp; operator=(optional&lt;U&gt;&amp;&amp; rhs);
</pre>
<blockquote>
<p>
-24- <i>Constraints</i>: [&hellip;]
<p/>
-25- <i>Effects</i>: See Table 61. The result of the expression <code>rhs.has_value()</code> remains unchanged.
</p>
<table border="1">
<caption>Table 61 &mdash; <code>optional::operator=(optional&lt;U&gt;&amp;&amp;)</code> effects [tab:optional.assign.move.templ]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del> with<br/>
<code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> <ins>and sets <code><i>has_val</i></code> to <code>true</code></ins></td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling<br/>
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code> <ins>and sets <code><i>has_val</i></code> to <code>false</code></ins></td>
<td>no effect</td>
</tr>
</table>
<p>
-26- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
-27- <i>Returns</i>: <code>*this</code>.
<p/>
-28- If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code> remains
unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s constructor. If an exception is thrown during the call to <code>T</code>'s
assignment, the state of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s assignment.
</p>
</blockquote>
<pre>
template&lt;class... Args&gt; constexpr T&amp; emplace(Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-29- <i>Mandates</i>: [&hellip;]
<p/>
-30- <i>Effects</i>: Calls <code>*this = nullopt</code>. Then direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>std::forward&lt;Args&gt;(args)...</code>.
<p/>
-31- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
-32- <i>Returns</i>: <ins><code><i>val</i></code></ins><del>A reference to the new contained value</del>.
<p/>
[&hellip;]
<p/>
-34- <i>Remarks</i>: If an exception is thrown during the call to <code>T</code>'s constructor, <code>*this</code>
does not contain a value, and the previous <code><ins><i>val</i></ins><del>*val</del></code> (if any)
has been destroyed.
</p>
</blockquote>
<pre>
template&lt;class U, class... Args&gt; constexpr T&amp; emplace(initializer_list&lt;U&gt; il, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-35- <i>Constraints</i>: [&hellip;]
<p/>
-36- <i>Effects</i>: Calls <code>*this = nullopt</code>. Then direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>il, std::forward&lt;Args&gt;(args)...</code>.
<p/>
-37- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
-38- <i>Returns</i>: <ins><code><i>val</i></code></ins><del>A reference to the new contained value</del>.
<p/>
[&hellip;]
<p/>
-40- <i>Remarks</i>: If an exception is thrown during the call to <code>T</code>'s constructor, <code>*this</code>
does not contain a value, and the previous <code><ins><i>val</i></ins><del>*val</del></code> (if any)
has been destroyed.
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr void swap(optional&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-1- <i>Mandates</i>: [&hellip;]
<p/>
-2- <i>Preconditions</i>: [&hellip;]
<p/>
-3- <i>Effects</i>: See Table 62.
</p>
<table border="1">
<caption>Table 62 &mdash; <code>optional::swap(optional&amp;)</code> effects [tab:optional.swap]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>calls <code>swap(<ins><i>val</i></ins><del>*(*this)</del>, <del>*</del>rhs<ins>.<i>val</i></ins>)</code></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value of <code>*this</code></del><br/>
with <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code>, followed by <code>rhs.<ins><i>val</i>.</ins><del>val-&gt;</del>T::~T()</code>;<br/>
postcondition is that <code>*this</code> contains a value and <code>rhs</code> does<br/>
not contain a value</td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>direct-non-list-initializes <del>the contained value of</del> <code>rhs<ins>.<i>val</i></ins></code><br/>
with <code>std::move(<ins><i>val</i></ins><del>*(*this)</del>)</code>, followed by <code><ins><i>val</i>.</ins><del>val-&gt;</del>T::~T()</code>;<br/>
postcondition is that <code>*this</code> does not contain a value and <code>rhs</code><br/>
contains a value</td>
<td>no effect</td>
</tr>
</table>
<p>
-4- <i>Throws</i>: [&hellip;]
<p/>
-5- <i>Remarks</i>: [&hellip;]
<p/>
-6- If any exception is thrown, the results of the expressions <code>this-&gt;has_value()</code> and
<code>rhs.has_value()</code> remain unchanged. If an exception is thrown during the call to function <code>swap</code>,
the state of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>swap</code> for lvalues of <code>T</code>. If an exception is thrown during the call to
<code>T</code>'s move constructor, the state of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s move constructor.
<p/>
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr const T* operator-&gt;() const noexcept;
constexpr T* operator-&gt;() noexcept;
</pre>
<blockquote>
<p>
-1- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-2- <i>Returns</i>: <code><ins>addressof(<i>val</i>)</ins><del>val</del></code>.
<p/>
-3- [&hellip;]
</p>
</blockquote>
<pre>
constexpr const T&amp; operator*() const &amp; noexcept;
constexpr T&amp; operator*() &amp; noexcept;
</pre>
<blockquote>
<p>
-4- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-5- <i>Returns</i>: <code><ins><i>val</i></ins><del>*val</del></code>.
<p/>
-6- [&hellip;]
</p>
</blockquote>
<pre>
constexpr T&amp;&amp; operator*() &amp;&amp; noexcept;
constexpr const T&amp;&amp; operator*() const &amp;&amp; noexcept;
</pre>
<blockquote>
<p>
-7- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-8- <i>Effects</i>: Equivalent to: <code>return std::move(<ins><i>val</i></ins><del>*val</del>);</code>
</p>
</blockquote>
<pre>
constexpr explicit operator bool() const noexcept;
</pre>
<blockquote>
<p>
<del>-9- <i>Returns</i>: <code>true</code> if and only if <code>*this</code> contains a value.</del>
<p/>
<del>-10- <i>Remarks</i>: This function is a constexpr function.</del>
</p>
</blockquote>
<pre>
constexpr bool has_value() const noexcept;
</pre>
<blockquote>
<p>
-11- <i>Returns</i>: <ins><code><i>has_val</i></code></ins><del><code>true</code> if and only if <code>*this</code> contains a value</del>.
<p/>
-12- <i>Remarks</i>: <ins>These functions are</ins><del>This function is a</del> constexpr function<ins>s</ins>.
</p>
</blockquote>
<pre>
constexpr const T&amp; value() const &amp;;
constexpr T&amp; value() &amp;;
</pre>
<blockquote>
<p>
-13- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? <ins><i>val</i></ins><del>*val</del> : throw bad_optional_access();
</pre></blockquote>
</blockquote>
<pre>
constexpr T&amp;&amp; value() &amp;&amp;;
constexpr const T&amp;&amp; value() const &amp;&amp;;
</pre>
<blockquote>
<p>
-14- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? std::move(<ins><i>val</i></ins><del>*val</del>) : throw bad_optional_access();
</pre></blockquote>
</blockquote>
<pre>
template&lt;class U&gt; constexpr T value_or(U&amp;&amp; v) const &amp;;
</pre>
<blockquote>
<p>
-15- <i>Mandates</i>: [&hellip;]
<p/>
-16- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? <ins><i>val</i></ins><del>**this</del> : static_cast&lt;T&gt;(std::forward&lt;U&gt;(v));
</pre></blockquote>
</blockquote>
<pre>
template&lt;class U&gt; constexpr T value_or(U&amp;&amp; v) &amp;&amp;;
</pre>
<blockquote>
<p>
-17- <i>Mandates</i>: [&hellip;]
<p/>
-18- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? std::move(<ins><i>val</i></ins><del>**this</del>) : static_cast&lt;T&gt;(std::forward&lt;U&gt;(v));
</pre></blockquote>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-1- Let <code>U</code> be <code>invoke_result_t&lt;F, decltype(<ins>(<i>val</i>)</ins><del>*val</del>)&gt;</code>.
<p/>
-2- <i>Mandates</i>: [&hellip;]
<p/>
-3- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (*this) {
  return invoke(std::forward&lt;F&gt;(f), <ins><i>val</i></ins><del>*val</del>);
} else {
  return remove_cvref_t&lt;U&gt;();
}
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-4- Let <code>U</code> be <code>invoke_result_t&lt;F, decltype(std::move(<ins><i>val</i></ins><del>*val</del>))&gt;</code>.
<p/>
-5- <i>Mandates</i>: [&hellip;]
<p/>
-6- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (*this) {
  return invoke(std::forward&lt;F&gt;(f), std::move(<ins><i>val</i></ins><del>*val</del>));
} else {
  return remove_cvref_t&lt;U&gt;();
}
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-7- Let <code>U</code> be <code>remove_cv_t&lt;invoke_result_t&lt;F, decltype(<ins>(<i>val</i>)</ins><del>*val</del>)&gt;&gt;</code>.
<p/>
-8- <i>Mandates</i>: <code>U</code> is a non-array object type other than <code>in_place_t</code> or <code>nullopt_t</code>. The declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), <ins><i>val</i></ins><del>*val</del>));
</pre></blockquote>
<p>
is well-formed for some invented variable <code>u</code>.
<p/>
[&hellip;]
<p/>
-9- <i>Returns</i>: If <code>*this</code> contains a value, an <code>optional&lt;U&gt;</code> object whose contained value is
direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), <ins><i>val</i></ins><del>*val</del>)</code>; otherwise,
<code>optional&lt;U&gt;()</code>.
</p>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-10- Let <code>U</code> be <code>remove_cv_t&lt;invoke_result_t&lt;F, decltype(std::move(<ins><i>val</i></ins><del>*val</del>))&gt;&gt;</code>.
<p/>
-11- <i>Mandates</i>: <code>U</code> is a non-array object type other than <code>in_place_t</code> or <code>nullopt_t</code>. The declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), std::move(<ins><i>val</i></ins><del>*val</del>)));
</pre></blockquote>
<p>
is well-formed for some invented variable <code>u</code>.
<p/>
[&hellip;]
<p/>
-12- <i>Returns</i>: If <code>*this</code> contains a value, an <code>optional&lt;U&gt;</code> object whose contained value is
direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), std::move(<ins><i>val</i></ins><del>*val</del>))</code>; otherwise,
<code>optional&lt;U&gt;()</code>.
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr void reset() noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: If <code>*this</code> contains a value, calls <code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code>
to destroy the contained value <ins>and sets <code><i>has_val</i></code> to <code>false</code></ins>; otherwise no effect.
<p/>
-2- <i>Postconditions</i>: <code>*this</code> does not contain a value.
</p>
</blockquote>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[St. Louis 2024-06-24; Jonathan provides improved wording]</i></p>

<p><i>[2024-08-21; LWG telecon]</i></p>

<p>
During telecon review it was suggested to replace
22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a> p1 and p2.
On the reflector Daniel requested to keep the "additional storage" prohibition,
so that will be addressed by issue <a href="lwg-defects.html#4141" title="Improve prohibitions on &quot;additional storage&quot; (Status: WP)">4141</a><sup><a href="https://cplusplus.github.io/LWG/issue4141" title="Latest snapshot">(i)</a></sup> instead.
</p>

<p><i>[2024-10-02; Jonathan tweaks proposed resolution]</i></p>

<p>
On the reflector we decided that the union member should use <code class='backtick'>remove_cv_t</code>,
as proposed for <code class='backtick'>expected</code> by issue <a href="lwg-active.html#3891" title="LWG 3870 breaks std::expected&lt;cv T, E&gt; (Status: New)">3891</a><sup><a href="https://cplusplus.github.io/LWG/issue3891" title="Latest snapshot">(i)</a></sup>.
The rest of the proposed resolution is unchanged, so that edit was made
in-place below, instead of as a new resolution that supersedes the old one.
</p>



<p id="res-4015"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4988" title=" Working Draft, Programming Languages — C++">N4988</a>.
</p>

<ol>

<li><p>Modify 22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a>, class template <code>optional</code> synopsis, as indicated:</p>

<blockquote>
<pre>
namespace std {
  template&lt;class T&gt;
  class optional {
  public:
    using value_type = T;
    [&hellip;]
  private:
    <del>*val // <i>exposition only</i></del>;
    <ins>union {</ins>
      <ins>remove_cv_t&lt;T&gt; <i>val</i>; // <i>exposition only</i></ins>
    <ins>};</ins>
  };

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

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

<blockquote>
<p>
-1-
<ins>
When its member <code><i>val</i></code> is active
(11.5.1 <a href="https://wg21.link/class.union.general">[class.union.general]</a>),
an instance of <code>optional&lt;T&gt;</code> is said to
<i>contain a value</i>, and <code><i>val</i></code> is referred to as its
<i>contained value</i>.
</ins>
<del>
Any instance of <code>optional&lt;T&gt;</code> at any given time either
contains a value or does not contain a value.
When an instance of <code>optional&lt;T&gt;</code> <i>contains a value</i>,
it means that an object of type <code>T</code>,
referred to as the</del>
<ins>An</ins>
optional object's
<ins>contained value</ins>
<del><i>contained value</i>, </del>
is allocated within the storage of the optional object.
Implementations are not permitted to use additional storage,
such as dynamic memory, to allocate its contained value.
<del>
When an object of type <code>optional&lt;T&gt;</code>
is contextually converted to <code>bool</code>,
the conversion returns <code class='backtick'>true</code> if the object contains a value;
otherwise the conversion returns <code class='backtick'>false</code>.
</del>
</p>
<p>
<del>
-2- When an <code>optional&lt;T&gt;</code> object contains a value,
member <code>val</code>
points to the contained value.
</del>
</p>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr optional(const optional&amp; rhs);
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code><del>*</del>rhs<ins>.<i>val</i></ins></code>.
<p/>
-5- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
constexpr optional(optional&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-8- <i>Constraints</i>: [&hellip;]
<p/>
-9- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code>.
<code>rhs.has_value()</code> is unchanged.
<p/>
-10- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class... Args&gt; constexpr explicit optional(in_place_t, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-13- <i>Constraints</i>: [&hellip;]
<p/>
-14- <i>Effects</i>: Direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code>std::forward&lt;Args&gt;(args)...</code>.
<p/>
-15- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U, class... Args&gt;
  constexpr explicit optional(in_place_t, initializer_list&lt;U&gt; il, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-18- <i>Constraints</i>: [&hellip;]
<p/>
-19- <i>Effects</i>: Direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code>il, std::forward&lt;Args&gt;(args)...</code>.
<p/>
-20- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U = T&gt; constexpr explicit(<i>see below</i>) optional(U&amp;&amp; v);
</pre>
<blockquote>
<p>
-23- <i>Constraints</i>: [&hellip;]
<p/>
-24- <i>Effects</i>: Direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code>std::forward&lt;U&gt;(v)</code>.
<p/>
-25- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr explicit(<i>see below</i>) optional(const optional&lt;U&gt;&amp; rhs);
</pre>
<blockquote>
<p>
-28- <i>Constraints</i>: [&hellip;]
<p/>
-29- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code><del>*</del>rhs<ins>.<i>val</i></ins></code>.
<p/>
-30- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr explicit(<i>see below</i>) optional(optional&lt;U&gt;&amp;&amp; rhs);
</pre>
<blockquote>
<p>
-33- <i>Constraints</i>: [&hellip;]
<p/>
-34- <i>Effects</i>: If <code>rhs</code> contains a value, direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with
<code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code>. <code>rhs.has_value()</code> is unchanged.
<p/>
-35- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr ~optional();
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: If <code>is_trivially_destructible_v&lt;T&gt; != true</code> and <code>*this</code> contains a value,
calls <code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code><ins>.</ins>
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr optional&lt;T&gt;&amp; operator=(nullopt_t) noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: If <code>*this</code> contains a value, calls
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code> to destroy the contained
value; otherwise no effect.
<p/>
-2- <i>Postconditions</i>: <code>*this</code> does not contain a value.
</p>
</blockquote>
<pre>
constexpr optional&lt;T&gt;&amp; operator=(const optional&amp; rhs);
</pre>
<blockquote>
<p>
-4- <i>Effects</i>: See Table 58.
</p>
<table border="1">
<caption>Table 58 &mdash; <code>optional::operator=(const optional&amp;)</code> effects [tab:optional.assign.copy]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code><del>*</del>rhs<ins>.<i>val</i></ins></code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code><del>*</del>rhs<ins>.<i>val</i></ins></code><br/>
</td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling <code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code><br/>
</td>
<td>no effect</td>
</tr>
</table>
<p>
-5- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
[&hellip;]
</p>
</blockquote>
<pre>
constexpr optional&lt;T&gt;&amp; operator=(optional&amp;&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-8- <i>Constraints</i>: [&hellip;]
<p/>
-9- <i>Effects</i>: See Table 59. The result of the expression <code>rhs.has_value()</code> remains unchanged.
<p/>
-10- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
-11- <i>Returns</i>: <code>*this</code>.
</p>
<table border="1">
<caption>Table 59 &mdash; <code>optional::operator=(optional&amp;&amp;)</code> effects [tab:optional.assign.move]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del> with
<code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code></td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling<br/>
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code></td>
<td>no effect</td>
</tr>
</table>
<p>
-12- <i>Remarks</i>: [&hellip;]
<p/>
-13- If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code> remains
unchanged. If an exception is thrown during the call to <code>T</code>'s move constructor, the state
of <code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception
safety guarantee of <code>T</code>'s move constructor. If an exception is thrown during the call to
<code>T</code>'s move assignment, the <ins>states</ins> <del>state</del> of <code><del>*</del><ins><i>val</i></ins><del>val</del></code>
and <code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> <ins>are</ins> <del>is</del> determined by the exception
safety guarantee of <code>T</code>'s move assignment.
</p>
</blockquote>
<pre>
template&lt;class U = T&gt; constexpr optional&lt;T&gt;&amp; operator=(U&amp;&amp; v);
</pre>
<blockquote>
<p>
-14- <i>Constraints</i>: [&hellip;]
<p/>
-15- <i>Effects</i>: If <code>*this</code> contains a value, assigns <code>std::forward&lt;U&gt;(v)</code>
to <ins><code><i>val</i></code></ins><del>the contained value</del>; otherwise direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>std::forward&lt;U&gt;(v)</code>.
<p/>
-16- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
-17- <i>Returns</i>: <code>*this</code>.
<p/>
-18- <i>Remarks</i>: If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code>
remains unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of
<code>v</code> is determined by the exception safety guarantee of <code>T</code>'s constructor. If an exception
is thrown during the call to <code>T</code>'s assignment, the <ins>states</ins> <del>state</del> of <code><ins><i>val</i></ins><del>*val</del></code>
and <code>v</code> <ins>are</ins> <del>is</del> determined by the exception safety guarantee of <code>T</code>'s assignment.
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr optional&lt;T&gt;&amp; operator=(const optional&lt;U&gt;&amp; rhs);
</pre>
<blockquote>
<p>
-19- <i>Constraints</i>: [&hellip;]
<p/>
-20- <i>Effects</i>: See Table 60.
</p>
<table border="1">
<caption>Table 60 &mdash; <code>optional::operator=(const optional&lt;U&gt;&amp;)</code> effects [tab:optional.assign.copy.templ]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code><del>*</del>rhs<ins>.<i>val</i></ins></code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del>
with <code><del>*</del>rhs<ins>.<i>val</i></ins></code> </td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling<br/>
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code> </td>
<td>no effect</td>
</tr>
</table>
<p>
-21- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
-22- <i>Returns</i>: <code>*this</code>.
<p/>
-23- If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code> remains
unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s constructor. If an exception is thrown during the call to <code>T</code>'s
assignment, the <ins>states</ins> <del>state</del> of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> <ins>are</ins> <del>is</del> determined by the exception safety
guarantee of <code>T</code>'s assignment.
</p>
</blockquote>
<pre>
template&lt;class U&gt; constexpr optional&lt;T&gt;&amp; operator=(optional&lt;U&gt;&amp;&amp; rhs);
</pre>
<blockquote>
<p>
-24- <i>Constraints</i>: [&hellip;]
<p/>
-25- <i>Effects</i>: See Table 61. The result of the expression <code>rhs.has_value()</code> remains unchanged.
</p>
<table border="1">
<caption>Table 61 &mdash; <code>optional::operator=(optional&lt;U&gt;&amp;&amp;)</code> effects [tab:optional.assign.move.templ]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>assigns <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> to <ins><code><i>val</i></code></ins><del>the contained value</del></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value</del> with<br/>
<code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code> </td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>destroys the contained value by calling<br/>
<code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code> </td>
<td>no effect</td>
</tr>
</table>
<p>
-26- <i>Postconditions</i>: <code>rhs.has_value() == this-&gt;has_value()</code>.
<p/>
-27- <i>Returns</i>: <code>*this</code>.
<p/>
-28- If any exception is thrown, the result of the expression <code>this-&gt;has_value()</code> remains
unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>T</code>'s constructor. If an exception is thrown during the call to <code>T</code>'s
assignment, the <ins>states</ins> <del>state</del> of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> <ins>are</ins> <del>is</del> determined by the exception safety
guarantee of <code>T</code>'s assignment.
</p>
</blockquote>
<pre>
template&lt;class... Args&gt; constexpr T&amp; emplace(Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-29- <i>Mandates</i>: [&hellip;]
<p/>
-30- <i>Effects</i>: Calls <code>*this = nullopt</code>. Then direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>std::forward&lt;Args&gt;(args)...</code>.
<p/>
-31- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
-32- <i>Returns</i>: <ins><code><i>val</i></code></ins><del>A reference to the new contained value</del>.
<p/>
[&hellip;]
<p/>
-34- <i>Remarks</i>: If an exception is thrown during the call to <code>T</code>'s constructor, <code>*this</code>
does not contain a value, and the previous <code><ins><i>val</i></ins><del>*val</del></code> (if any)
has been destroyed.
</p>
</blockquote>
<pre>
template&lt;class U, class... Args&gt; constexpr T&amp; emplace(initializer_list&lt;U&gt; il, Args&amp;&amp;... args);
</pre>
<blockquote>
<p>
-35- <i>Constraints</i>: [&hellip;]
<p/>
-36- <i>Effects</i>: Calls <code>*this = nullopt</code>. Then direct-non-list-initializes
<ins><code><i>val</i></code></ins><del>the contained value</del> with <code>il, std::forward&lt;Args&gt;(args)...</code>.
<p/>
-37- <i>Postconditions</i>: <code>*this</code> contains a value.
<p/>
-38- <i>Returns</i>: <ins><code><i>val</i></code></ins><del>A reference to the new contained value</del>.
<p/>
[&hellip;]
<p/>
-40- <i>Remarks</i>: If an exception is thrown during the call to <code>T</code>'s constructor, <code>*this</code>
does not contain a value, and the previous <code><ins><i>val</i></ins><del>*val</del></code> (if any)
has been destroyed.
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr void swap(optional&amp; rhs) noexcept(<i>see below</i>);
</pre>
<blockquote>
<p>
-1- <i>Mandates</i>: [&hellip;]
<p/>
-2- <i>Preconditions</i>: [&hellip;]
<p/>
-3- <i>Effects</i>: See Table 62.
</p>
<table border="1">
<caption>Table 62 &mdash; <code>optional::swap(optional&amp;)</code> effects [tab:optional.swap]</caption>

<tr>
<th></th>
<th><b><code>*this</code> contains a value</b></th>
<th><b><code>*this</code> does not contain a value</b></th>
</tr>

<tr>
<td><b><code>rhs</code> contains a value</b></td>
<td>calls <code>swap(<ins><i>val</i></ins><del>*(*this)</del>, <del>*</del>rhs<ins>.<i>val</i></ins>)</code></td>
<td>direct-non-list-initializes <ins><code><i>val</i></code></ins><del>the contained value of <code>*this</code></del><br/>
with <code>std::move(<del>*</del>rhs<ins>.<i>val</i></ins>)</code>, followed by <code>rhs.<ins><i>val</i>.</ins><del>val-&gt;</del>T::~T()</code>;<br/>
postcondition is that <code>*this</code> contains a value and <code>rhs</code> does<br/>
not contain a value</td>
</tr>

<tr>
<td><b><code>rhs</code> does not contain a value</b></td>
<td>direct-non-list-initializes <del>the contained value of</del> <code>rhs<ins>.<i>val</i></ins></code><br/>
with <code>std::move(<ins><i>val</i></ins><del>*(*this)</del>)</code>, followed by <code><ins><i>val</i>.</ins><del>val-&gt;</del>T::~T()</code>;<br/>
postcondition is that <code>*this</code> does not contain a value and <code>rhs</code><br/>
contains a value</td>
<td>no effect</td>
</tr>
</table>
<p>
-4- <i>Throws</i>: [&hellip;]
<p/>
-5- <i>Remarks</i>: [&hellip;]
<p/>
-6- If any exception is thrown, the results of the expressions <code>this-&gt;has_value()</code> and
<code>rhs.has_value()</code> remain unchanged. If an exception is thrown during the call to function <code>swap</code>,
the state of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> is determined by the exception safety
guarantee of <code>swap</code> for lvalues of <code>T</code>. If an exception is thrown during the call to
<code>T</code>'s move constructor, the <ins>states</ins> <del>state</del> of <code><ins><i>val</i></ins><del>*val</del></code> and
<code><del>*</del>rhs.<ins><i>val</i></ins><del>val</del></code> <ins>are</ins> <del>is</del> determined by the exception safety
guarantee of <code>T</code>'s move constructor.
<p/>
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr const T* operator-&gt;() const noexcept;
constexpr T* operator-&gt;() noexcept;
</pre>
<blockquote>
<p>
-1- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-2- <i>Returns</i>: <code><ins>addressof(<i>val</i>)</ins><del>val</del></code>.
<p/>
-3- [&hellip;]
</p>
</blockquote>
<pre>
constexpr const T&amp; operator*() const &amp; noexcept;
constexpr T&amp; operator*() &amp; noexcept;
</pre>
<blockquote>
<p>
-4- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-5- <i>Returns</i>: <code><ins><i>val</i></ins><del>*val</del></code>.
<p/>
-6- [&hellip;]
</p>
</blockquote>
<pre>
constexpr T&amp;&amp; operator*() &amp;&amp; noexcept;
constexpr const T&amp;&amp; operator*() const &amp;&amp; noexcept;
</pre>
<blockquote>
<p>
-7- <i>Preconditions</i>: <code>*this</code> contains a value.
<p/>
-8- <i>Effects</i>: Equivalent to: <code>return std::move(<ins><i>val</i></ins><del>*val</del>);</code>
</p>
</blockquote>
<pre>
constexpr explicit operator bool() const noexcept;
</pre>
<blockquote>
<p>
-9- <i>Returns</i>: <code>true</code> if and only if <code>*this</code> contains a value.
<p/>
-10- <i>Remarks</i>: This function is a constexpr function.
</p>
</blockquote>
<pre>
constexpr bool has_value() const noexcept;
</pre>
<blockquote>
<p>
-11- <i>Returns</i>: <code>true</code> if and only if <code>*this</code> contains a value.
<p/>
-12- <i>Remarks</i>: This function is a constexpr function.
</p>
</blockquote>
<pre>
constexpr const T&amp; value() const &amp;;
constexpr T&amp; value() &amp;;
</pre>
<blockquote>
<p>
-13- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? <ins><i>val</i></ins><del>*val</del> : throw bad_optional_access();
</pre></blockquote>
</blockquote>
<pre>
constexpr T&amp;&amp; value() &amp;&amp;;
constexpr const T&amp;&amp; value() const &amp;&amp;;
</pre>
<blockquote>
<p>
-14- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? std::move(<ins><i>val</i></ins><del>*val</del>) : throw bad_optional_access();
</pre></blockquote>
</blockquote>
<pre>
template&lt;class U&gt; constexpr T value_or(U&amp;&amp; v) const &amp;;
</pre>
<blockquote>
<p>
-15- <i>Mandates</i>: [&hellip;]
<p/>
-16- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? <ins><i>val</i></ins><del>**this</del> : static_cast&lt;T&gt;(std::forward&lt;U&gt;(v));
</pre></blockquote>
</blockquote>
<pre>
template&lt;class U&gt; constexpr T value_or(U&amp;&amp; v) &amp;&amp;;
</pre>
<blockquote>
<p>
-17- <i>Mandates</i>: [&hellip;]
<p/>
-18- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
return has_value() ? std::move(<ins><i>val</i></ins><del>**this</del>) : static_cast&lt;T&gt;(std::forward&lt;U&gt;(v));
</pre></blockquote>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-1- Let <code>U</code> be <code>invoke_result_t&lt;F, decltype(<ins>(<i>val</i>)</ins><del>*val</del>)&gt;</code>.
<p/>
-2- <i>Mandates</i>: [&hellip;]
<p/>
-3- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (*this) {
  return invoke(std::forward&lt;F&gt;(f), <ins><i>val</i></ins><del>*val</del>);
} else {
  return remove_cvref_t&lt;U&gt;();
}
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-4- Let <code>U</code> be <code>invoke_result_t&lt;F, decltype(std::move(<ins><i>val</i></ins><del>*val</del>))&gt;</code>.
<p/>
-5- <i>Mandates</i>: [&hellip;]
<p/>
-6- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (*this) {
  return invoke(std::forward&lt;F&gt;(f), std::move(<ins><i>val</i></ins><del>*val</del>));
} else {
  return remove_cvref_t&lt;U&gt;();
}
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-7- Let <code>U</code> be <code>remove_cv_t&lt;invoke_result_t&lt;F, decltype(<ins>(<i>val</i>)</ins><del>*val</del>)&gt;&gt;</code>.
<p/>
-8- <i>Mandates</i>: <code>U</code> is a non-array object type other than <code>in_place_t</code> or <code>nullopt_t</code>. The declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), <ins><i>val</i></ins><del>*val</del>));
</pre></blockquote>
<p>
is well-formed for some invented variable <code>u</code>.
<p/>
[&hellip;]
<p/>
-9- <i>Returns</i>: If <code>*this</code> contains a value, an <code>optional&lt;U&gt;</code> object whose contained value is
direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), <ins><i>val</i></ins><del>*val</del>)</code>; otherwise,
<code>optional&lt;U&gt;()</code>.
</p>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-10- Let <code>U</code> be <code>remove_cv_t&lt;invoke_result_t&lt;F, decltype(std::move(<ins><i>val</i></ins><del>*val</del>))&gt;&gt;</code>.
<p/>
-11- <i>Mandates</i>: <code>U</code> is a non-array object type other than <code>in_place_t</code> or <code>nullopt_t</code>. The declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), std::move(<ins><i>val</i></ins><del>*val</del>)));
</pre></blockquote>
<p>
is well-formed for some invented variable <code>u</code>.
<p/>
[&hellip;]
<p/>
-12- <i>Returns</i>: If <code>*this</code> contains a value, an <code>optional&lt;U&gt;</code> object whose contained value is
direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), std::move(<ins><i>val</i></ins><del>*val</del>))</code>; otherwise,
<code>optional&lt;U&gt;()</code>.
</p>
</blockquote>
</blockquote>
</li>

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

<blockquote>
<pre>
constexpr void reset() noexcept;
</pre>
<blockquote>
<p>
-1- <i>Effects</i>: If <code>*this</code> contains a value, calls <code><del>val-&gt;</del><ins><i>val</i>.</ins>T::~T()</code>
to destroy the contained value; otherwise no effect.
<p/>
-2- <i>Postconditions</i>: <code>*this</code> does not contain a value.
</p>
</blockquote>
</blockquote>
</li>

</ol>





</body>
</html>
