<!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=US-ASCII">

<style type="text/css">
pre {margin-left:20pt; }
pre > i {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
code > i {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
pre > em {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
code > em {
  font-family: "OCR A Extended", "Consolas", "Lucida Console", monospace;
  font-style:italic;
}
body { color: #000000; background-color: #FFFFFF; }
del { text-decoration: line-through; color: #8B0040; }
ins { text-decoration: underline; color: #005100; }

p.example { margin-left: 2em; }
pre.example { margin-left: 2em; }
div.example { margin-left: 2em; }

code.extract { background-color: #F5F6A2; }
pre.extract { margin-left: 2em; background-color: #F5F6A2;
  border: 1px solid #E1E28E; }

p.function { }
.attribute { margin-left: 2em; }
.attribute dt { float: left; font-style: italic;
  padding-right: 1ex; }
.attribute dd { margin-left: 0em; }

blockquote.std { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding-left: 0.5empadding-right: 0.5em; ; }

blockquote.stdins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3; padding: 0.5em; }

table { border: 1px solid black; border-spacing: 0px;
  margin-left: auto; margin-right: auto; }
th { text-align: left; vertical-align: top;
  padding-left: 0.4em; border: none; 
  padding-right: 0.4em; border: none; }
td { text-align: left; vertical-align: top;
  padding-left: 0.4em; border: none;
  padding-right: 0.4em; border: none; }
</style>

<title>A proposal to add a utility class to represent optional objects (Revision 4)</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">$(function() {
    var next_id = 0
    function find_id(node) {
        // Look down the first children of 'node' until we find one
        // with an id. If we don't find one, give 'node' an id and
        // return that.
        var cur = node[0];
        while (cur) {
            if (cur.id) return curid;
            if (cur.tagName == 'A' && cur.name)
                return cur.name;
            cur = cur.firstChild;
        };
        // No id.
        node.attr('id', 'gensection-' + next_id++);
        return node.attr('id');
    };

    // Put a table of contents in the #toc nav.

    // This is a list of <ol> elements, where toc[N] is the list for
    // the current sequence of <h(N+2)> tags. When a header of an
    // existing level is encountered, all higher levels are popped,
    // and an <li> is appended to the level
    var toc = [$("<ol/>")];
    $(':header').not('h1').each(function() {
        var header = $(this);
        // For each <hN> tag, add a link to the toc at the appropriate
        // level.  When toc is one element too short, start a new list
        var levels = {H2: 0, H3: 1, H4: 2, H5: 3, H6: 4};
        var level = levels[this.tagName];
        if (typeof level == 'undefined') {
            throw 'Unexpected tag: ' + this.tagName;
        }
        // Truncate to the new level.
        toc.splice(level + 1, toc.length);
        if (toc.length < level) {
            // Omit TOC entries for skipped header levels.
            return;
        }
        if (toc.length == level) {
            // Add a <ol> to the previous level's last <li> and push
            // it into the array.
            var ol = $('<ol/>')
            toc[toc.length - 1].children().last().append(ol);
            toc.push(ol);
        }
        var header_text = header.text();
        toc[toc.length - 1].append(
            $('<li/>').append($('<a href="#' + find_id(header) + '"/>')
                              .text(header_text)));
    });
    $('#toc').append(toc[0]);
})
</script>
</head>

<body>


<!--<table>
<tr><td>Doc. no.</td><td>NXXXX=XX-XXXX</td></tr>
<tr><td>Date</td><td>YYYY-MM-DD</td></tr>
<tr><td valign='top'>Authors</td><td>Fernando Cacciola<br>Andrzej Krzemie&#x144;ski</td></tr>
<tr><td valign='top'>Contact</td><td><a href="mailto:fernando.cacciola@gmail.com">fernando.cacciola@gmail.com</a><br><a href='mailto:akrzemi1@gmail.com'>akrzemi1@gmail.com</a></td></tr>
<tr><td>Addresses</td><td>LWG</td></tr>
</table>-->

<h1><a name='title'>A proposal to add a utility class to represent optional objects (Revision&nbsp;5)</a></h1>

<p>
ISO/IEC JTC1 SC22 WG21 N3793 2013-10-03
</p>

<address>
Fernando Cacciola, fernando.cacciola@gmail.com
</address>
<address>
Andrzej Krzemie&#x144;ski, akrzemi1@gmail.com

</address>


<h2><a name='intro'>Introduction</a></h2>

<p>Class template <code>optional&lt;T&gt;</code> proposed here is a type that may or may not store a value of type <code>T</code> in its storage space. Its interface allows to query if a value of type <code>T</code> is currently stored, and if so, to access it. The interface is based on Fernando Cacciola's Boost.Optional library<sup>[2]</sup>, shipping since March, 2003, and widely used. It requires no changes to core language, and breaks no existing code.</p>


<h2><a name='toc'>Table of contents</a></h2>

<p id='toc'></p>


<h2><a name='revision'>Revision history</a></h2>

<p>Changes since <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3672.html">N3672</a><p>
<ul>
<li>Implemented op::operator!= in terms of op::operator== (agreed in Chicago meeting).</li>
<li>Implemented relational operators in terms of op::operator&lt; (agreed in Chicago meeting).</li>
<li>Implentation of op::operator&lt; does not use std::less (agreed in Chicago meeting).</li>
<li>Mixed relops use <code>T</code>&rsquo;'s operators &lt; and == (agreed in Chicago meeting).</li>
<li>Optional is proposed for Fundamentals TR rather than 2014 C++ Standard. It is now in namespace <code>std::experimental</code> and in header <code>&lt;experimental/optional&gt;</code>.</li>
<li>Resolved Library Issue 2282 (Incorrect <code>is_assignable</code> constraint in <code>optional::op=(U&amp;&amp;)</code>).</li>
<li>Resolved Library Issue 2283. (<code>optional</code> declares and then does not define an <code>operator&lt;()</code>).</li>
<li>Resolved Library Issue 2287 (Incorrect exception safety for <code>optional</code> copy assignment operator).</li>
<li>Added missing <code>constexpr</code> specifier to constructor declarations to match synopsis and Remarks.</li>
<li>Cloned minor fixes applied by the Editor when incorporating the previous revision of this proposal into the Standard Draft:
  <ul>
	<li>Reorganized [optional.defs]/1 as an itemized list.</li>
	<li>Reworded [optional.object]/2.</li>
	<li>Reworded [optional.object.ctor]/2.</li>
	<li>Corrected [optional.object.observe]/18 to depend on both constructors.</li>
	<li>Capitalized first letter of Remarks for operator bool().</li>
  </ul>
  </li>
<li>Removed most of the Rationale section.</li>
<li>Added discussion about why we consider a converting constructor from <code>U</code> (requested in Chicago meeting) a dangerous idea (it can silently turn explicit conversions into implicit ones).</li>
<li>Clarified that initialization of the contained value should be direct-non-list-initialization -- it was missing in a couple of places.</li>
<li>Replaced incorrect type trait <code>remove_reference</code> with <code>decay</code> in the specification of the converting assignment.</li>
<li>Clarified the result of hashing a disengaged <code>optional</code>.</li>
</ul>

<p>Changes since R4C<p>
<ul>
<li>Removed all relational operators except operator== and operator&lt;.</li>
</ul>

<p>Changes since <a href='http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3527.html'>N3527</a>:</p>
<ul>
  <li>Renamed tag <code>emplace</code> to <code>in_place</code> based on feedback from LEWG.</li>
  <li>Removed the Motivation section.</li>
  <li>Added description of <code>op = {}</code> syntax and explained why we had to apply some tricks to support it. See <a href='#rationale.reset_syntax'>The <code>op = {}</code> syntax</a>.</li>
  <li>Clarified why we do not require in function template <code>swap</code> that <code>T</code> be MoveAssignamle. See <a href='#rationale.swap_reqs'>Requirements for <code>swap</code></a>.</li>
  <li>Added short discussion on the choice of the header. See <a href='#rationale.header'>Header <code>&lt;utility&gt;</code> or <code>&lt;optional&gt;</code>?</a>.</li>
  <li>Optional's <code>operator&lt;</code> now uses <code>less&lt;T&gt;</code> rather than <code>T::operator&lt;</code>. See <a href='#rationale.relops'>Relational operators</a> for rationale.</li>
  <li>Provided a comparison between <code>optional</code> in this proposal and <code>boost::optional</code>. See <a href='#comparison_with_boost'>Comparison with Boost.Optional</a>.</li>
  <li>Added "open questions" section.</li>
  <li>Reduced the discussion in Rationale section by omitting some alternatives we considered but rejected.</li>
  <li>Removed namespace <code>experimental</code>, because we are not targetting a TS anymore.</li>
  <li>Applied fixes in the standardese as per LWG's feedback.</li>
  <li>Function <code>emplace</code> now returns <code>void</code> &mdash; LWG recommendation.</li>
  <li>All mixed ordering relations now use only <code>less&lt;T&gt;</code> in implementation &mdash; LWG recommendation.</li>
  <li>Clarified (in section <a href='#overview'>Overview of <code>optional</code></a>) that Optional cannot be implemented with <code>aligned_storage</code> .</li>
  <li>Reworded complicated Effects elements for copy/move assignment and swap, so that they are easier to read &mdash; LWG recommendation.</li>
</ul>

<p>Changes since <a href='http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3406.html'>N3406=12-0096</a>:</p>
<ul>
  <li>Wording changes are now relative to <a href='http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdf'>N3485</a>.</li>
  <li><code>optional&lt;T&gt;</code> is hashable for hashable <code>T</code>&rsquo;s.</li>
  <li>Optional refereces are now an auxiliary proposal &mdash; this gives the possibility to accept optional values without references.</li>
  <li>Optional refereces are now assignamble and swappable.</li>
  <li><code>get_value_or</code> is now <code>optional</code>-specific member function, renamed to <code>value_or</code>.</li>
  <li>Added member function <code>value</code> &mdash; an alternative to <code>operator*</code> that checks if the object is engaged.</li>
  <li><code>optional&lt;T&gt;</code> is a literal type.</li>
  <li>Mixed relational operations between <code>optional&lt;T&gt;</code> and <code>T</code> are now allowed.</li>
  <li>Removed reference implementation. We now only provide the implementation of the parts that we consider non-trivial.</li>
</ul>

<p>Changes since <a href='http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1878.htm'>N1878=05-0138</a>:</p>
<ul>
  <li>Revised wording; the changes are now relative to <a href='http://www.open-std.org/jtc1/sc22/wg21/prot/14882fdis/n3290.pdf'>N3290</a>.</li>
  <li>Removed any form of assignment for optional references.</li>
  <li>Removed duplicate interface for accessing the value stored by the optional.</li>
  <li>Added in-place construction and assignment.</li>
  <li>Now using different tag <code>nullopt</code> instead of <code>nullptr</code> to indicate the 'disengaged' (uninitialized) optional.</li>
  <li>Included C++11 features: move semantics, <code>noexcept</code>, variadic templates, perfect forwarding, static initialization.</li>
  <li>Changed the motivation section.</li>
  <li>Changed the design rationale section.</li>
  <li>Added reference implementation</li>
</ul>



<h2><a name='impact'>Impact on the Standard</a></h2>


<p>This proposal depends on library proposal <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3471.html">N3471</a>: it requires that Standard Library components <code>move</code>, <code>forward</code> and member functions of <code>initializer_list</code> are <code>constexpr</code>. The paper has already been incorporated into the Working Draft of the Standard <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3485.pdf">N3485</a>. This proposal also depends on language proposal <a href="www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm">N2439</a> (Rvalue references for <code>*this</code>). While this feature proposal has been incorporated into C++11, we are aware of only two compilers that implemented it: Clang and GCC 4.8.1. There is a risk that if compiler vendors do not implement it, they will also not be able to fully implement this proposal. In that case, the signature of member function <code>optional&lt;T&gt;::value_or</code> from this proposal will need to be modified.</p>

<p><a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3507.html">N3507</a> (A URI Library for C++) depends on this library.</p>



<h2><a name='comparison_with_boost'>Comparison with Boost.Optional</a></h2>

<p>This proposal basically tries to follow Boost.Optional's interface. Here we list the significant differences.</p>

<table>
<tr><th>aspect</th><th>this proposal</th><th>Boost.Optional</th></tr>
<tr><td>Move semantics</td><td>yes</td><td>no</td></tr>
<tr><td>noexcept</td><td>yes</td><td>no</td></tr>
<tr><td>hash support</td><td>yes</td><td>no</td></tr>
<tr><td>a throwing value accessor</td><td>yes</td><td>no</td></tr>
<tr><td>literal type</td><td>partially</td><td>no</td></tr>
<tr><td>in place construction</td><td><code><code>emplace</code>, tag <code>in_place</code></td><td>utility <code>in_place_factory</code></td></tr>
<tr><td>disengaged state tag</td><td><code>nullopt</code></td><td><code>none</code></td></tr>
<tr><td>optional references</td><td>no (optionally)</td><td>yes</td></tr>
<tr><td>conversion from <code>optional&lt;U&gt;</code> to <code>optional&lt;T&gt;</code></td><td>no</td><td>yes</td></tr>
<tr><td>dupplicated interface functions <br>(<code>is_initialized</code>, <code>reset</code>, <code>get</code>)</td><td>no</td><td>yes</td></tr>
<tr><td>explicit convert to ptr <br>(<code>get_ptr</code>)</td><td>no</td><td>yes</td></tr>
</table>


<h2><a name='rationale'>Design rationale</a></h2>




<h3><a name='rationale.conversion'>"Copy initialization forwarding"</a></h3>

<p>Since <code>optional&lt;T&gt;</code> can be thought of as an "almost <code>T</code>", one could expect that if the following works:</p>

<pre>void fun(std::string s);
fun("text");
</pre>

<p>the following should also work:</p>

<pre>void gun(optional&lt;std::string&gt; s);
gun("text");
</pre>

<p>Supporting this was also requested in Chicago meeting. </p>
<p>However, naively implementing a converting constructor would also enable a non-explicit converting constructor from any type <code>U</code> to type <code>optional&lt;T&gt;</code> for any type <code>T</code>. This would turn some types that are explicitly constructible into optional types that are implicitly constructible. Consider:
</p>


<pre>void explicit_conv( int * ptr ) {
  unique_ptr&lt;int&gt; v = ptr;           <em>// ILLEGAL</em> 
}

void implicit_conv( int * ptr ) {
  optional&lt;unique_ptr&lt;int&gt;&gt; v = ptr; <em>// LEGAL</em>
}</pre>

<p>In order to make the former example work on the one hand and to prevent the problem with the latter example on the other, we considered a solution that could be informally called a conditionally-explicit converting constructor. We could achieve this by specifying two constructor templates with identical template and function parameters, one explicit and one non-explicit, and make them mutually exclusive by means of SFINAE:</p>

<pre>
template &lt;class U&gt; 
  <em>// enable_if: Constructible&lt;T, U&amp;&amp;&gt; &amp;&amp; !Convertible&lt;U&amp;&amp;, T&gt;</em>
  explicit optional&lt;T&gt;::optional(U&amp;&amp;);
   
template &lt;class U&gt; 
  <em>// enable_if: Convertible&lt;U&amp;&amp;, T&gt;</em>
  optional&lt;T&gt;::optional(U&amp;&amp;);
</pre>


<p>Such concept-like behaviour as used above can be implemented in C++ with type traits and <code>enable_if</code>. It was noted, however, that the existence of such converting constructor would cause unexpected ambiguities in overload resolution. Consider the following scenario. We start from a working program:</p>

<pre><em>// library</em>
void fun(string const&amp; s);

<em>// usage</em>
fun("hello");
</pre>

<p>At some point we decide to add a second overload that accepts an optional string:</p>

<pre><em>// library</em>
void fun(string const&amp; s);
void fun(optional&lt;string&gt; const&amp; s);   <em>// new overload</em>

<em>// usage</em>
fun("hello");                          <em>// ERROR: ambiguity </em>
</pre>

<p>Does it make sense to add an overload for optional rather than substituting it for the original? It might be useful for performance reasons: if you already have <code>string</code> it is cheaper to bind it directly to <code>string const&amp;</code> than to create a temporary optional object and trigger the copy constructor of <code>string</code>:</p>

<pre><em>// library</em>
void fun(optional&lt;string&gt; const&amp; s);   <em>// only this fun</em>

<em>// usage</em>
string s = "hello";
fun(s);                                <em>// copy ctor invoked!</em>
</pre>


<p>This example shows how an implicit conversion can cause an inadvertent and unexpected (potentially expensive) copy constructor. For this reason we do not propose a converting constructor from arbitrary type <code>U</code>. (Although we do propose a converting constructor from <code>T</code>.)</p>



<h3><a name='rationale.converting_ctor'>Converting constructor (from <code>T</code>)</a></h3>


<p>An object of type <code>T</code> is convertible to an engaged object of type <code>optional&lt;T&gt;</code>:</p>

<pre>optional&lt;int&gt; oi = 1; <em>// works</em></pre>

<p>This convenience feature is not strictly necessary because you can achieve the same effect by using tagged forwarding constructor:</p>

<pre>optional&lt;int&gt; oi{in_place, 1};</pre>

<p>If the latter appears too inconvenient, one can always use function <code>make_optional</code> described below:</p>
 
<pre>optional&lt;int&gt; oi = make_optional(1); 
auto oj = make_optional(1); 
</pre>

<p>The implicit converting constructor comes in handy in case of optional function arguments:</p>

<pre>void fun(std::string s, optional&lt;int&gt; oi = nullopt);

fun("dog", 2);
fun("dog");
fun("dog", nullopt); <em>// just to be explicit</em> 
</pre>

<p>While being a nice convenience the converting constructor causes certain problems. It automatically implies mixed comparisons, and the latter are considered error-prone by many. For instance: </p>

<pre>
optional&lt;bool&gt; ob;
assert (!ob == (ob == false)); <em>// error</em>
</pre>

<p>The expectation in assertion isn't always satisfied. Also, some people prefer a mixed comparison to be a type error -- a type-safety feature.</p> 




 
<h3><a name='rationale.requirements'>Requirements on <code>T</code></a></h3>

<p>If <code>T</code> is <code>EqualityComparable</code> then (and only then) we expect <code>optional&lt;T&gt;</code> to be <code>EqualityComparable</code>. </p>

<p>For orderting predicates, we do not require that <code>T</code> is <code>LessThanComparable</code>. Instead we require that expression <code>T{} &lt; T{}</code> is valid and convertible to <code>bool</code>. We consider requirements that require Strict Weak Ordering axioms too strict. less-than comparisons should still work if user uses <code>T::operator&lt;</code> for something tricky.</p>




<h3><a name='rationale.reset_syntax'>The <code>op = {}</code> syntax</a></h3>


<p>We put the extra requirements in the standardese to make sure that the following syntax works for resetting the optional:</p>

<code>op = {};</code>

<p>We consider that this will become a common idiom for resetting (putting into default-constructed state) values in C++. While you get that syntax for free for POD types, in <code>optional</code> we have to take extra care to enable it; this is because <code>optional</code> provides three assignment operators: copy/move assignment, assignment from <code>T</code> and from <code>nullopt_t</code>. If we just provided the "intuitive" declaration of assignment from <code>const T&amp;</code> and <code>T&amp;&amp;</code>, the expression above would become ambiguous. The expression above is processed as:</p>

<code>op = DEDUCED{};</code>

<p>where <code>DEDUCED</code> needs to be deduced from all available overloads. We would have two candidates: move assignment and assignemnt from <code>T&amp;&amp;</code>, which would cause an ambiguity. Therefore, we require that the assignment from <code>T&amp;&amp;</code> is declared in a more convoluted way &mdash; as a template:</p>

<code>template &lt;class U&gt; optional& optional&lt;T&gt;::operator=(U&amp;&amp;);<br>
<em>// enable if decay&lt;U&gt; == T</em></code>

<p>The additional requirement that <code>decay&lt;U&gt; == T</code> says that the only valid instantiations from this template are these for <code>const T&amp;</code> and <code>T&amp;&amp;</code> (and some other less relevant variations of references to <code>T</code>). But it is still a template, and templates do not participate in the resolution of type <code>DEDUCED</code>.</p>

<p>For the same reason, we require that tag <code>nullopt_t</code> is not DefaultConstructible. Otherwise, because <code>optional</code> provides an assignment from <code>nullopt_t</code>, <code>DEDUCED</code> might also have been deduced as <code>nullopt_t</code>.</p>

<p><strong>Note</strong> that it is not the only way to disengage an optional object. You can also use:</p>

<pre>op = std::nullopt;</pre>



<h2><a name='open_questions'>Open questions</a></h2>

<h3><a name='open_questions.allocator'>Allocator support</a></h3>

<p>Optional does not allocate memory. So it can do without allocators. However, it can be useful in compound types like:</p>

<pre>typedef vector&lt; optional&lt;vector&lt;int, MyAlloc&gt;&gt;, MyAlloc&gt;; MyVec;
MyVec v{ v2, MyAlloc{} };</pre>

<p>One could expect that the allocator argument is forwarded in this constructor call to the nested vectors that use the same allocator. Allocator support would enable this. <code>std::tuple</code> offers this functionality.</p>




<h2><a name='wording'>Proposed wording</a></h2>



<p>Grayish background indicates the wording to be added. In fact we are only adding new wording (no deletions). <ins>Insertions</ins> and <del>deletions</del> are used to indicate changes from the previous proposed wording: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3672.html">N3672</a>.</p>

<p>Insert a new subclause:</p>

<blockquote class="std">
<h4><a name="defns.direct-non-list-init">N.M.N <span style="float:right">[defns.direct-non-list-init]</span></a></h4>

<p><strong>dierect-non-list-initialization</strong><p>

<p>A direct-initialization that is not list-initialization.</p>

</blockquote>



<p>Insert a new paragraph.</p>
  
 <blockquote class="std"> 
<h3><a name="optional">X.Y Optional objects <span style="float:right">[optional]</span></a></h3>


<h4><a name="optional.general">X.Y.1 In general <span style="float:right">[optional.general]</span></a></h4>

<p>This subclause describes class template <code>optional</code> that represents <em>optional objects</em>. An <em>optional object for object types</em> is an object that contains the storage for another object and manages the lifetime of this contained object, if any. The contained object may be initialized after the optional object has been initialized, and may be destroyed before the optional object has been destroyed. The initialization state of the contained object is tracked by the optional object.</p>

  
<h4><a name="optional.synop">X.Y.2 Header <kbd>&lt;experimental/optional&gt;</kbd> synopsis <span style="float:right">[optional.synop]</span></a></h4>

<pre>
namespace std {
<ins>namespace experimental {</ins>
  // <em><a href="#optional.object">X.Y.4</a>, <code>optional</code> for object types</em>
  template &lt;class T&gt; class optional;

  // <em><a href="#optional.inplace">X.Y.5</a>, In-place construction</em>
  struct in_place_t{};
  constexpr in_place_t in_place{};

  // <em><a href="#optional.nullopt">X.Y.6</a>, Disengaged state indicator</em>
  struct nullopt_t{<em>see below</em>};
  constexpr nullopt_t nullopt(<em>unspecified</em>);
  
  // <em><a href="#optional.bad_optional_access">X.Y.7</a>, class bad_optional_access</em>
  class bad_optional_access;

  // <em><a href="#optional.relops">X.Y.8</a>, Relational operators</em>
  template &lt;class T&gt;
    constexpr bool operator==(const optional&lt;T&gt;&amp;, const optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt;
    constexpr bool operator!=(const optional&lt;T&gt;&amp;, const optional&lt;T&gt;&amp;);</ins>
  template &lt;class T&gt;
    constexpr bool operator&lt;(const optional&lt;T&gt;&amp;, const optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt;
    constexpr bool operator&gt;(const optional&lt;T&gt;&amp;, const optional&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&lt;=(const optional&lt;T&gt;&amp;, const optional&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&gt;=(const optional&lt;T&gt;&amp;, const optional&lt;T&gt;&amp;);</ins>

  // <em><a href="#optional.nullops">X.Y.9</a>, Comparison with <code>nullopt</code></em>
  template &lt;class T&gt; constexpr bool operator==(const optional&lt;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator==(nullopt_t, const optional&lt;T&gt;&amp;) noexcept;
  <ins>template &lt;class T&gt; constexpr bool operator!=(const optional&lt;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator!=(nullopt_t, const optional&lt;T&gt;&amp;) noexcept;</ins>
  template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&lt;(nullopt_t, const optional&lt;T&gt;&amp;) noexcept;
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=(const optional&lt;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&lt;=(nullopt_t, const optional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;(nullopt_t, const optional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;=(const optional&lt;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;=(nullopt_t, const optional&lt;T&gt;&amp;) noexcept;</ins>

  // <em><a href="#optional.comp_with_t">X.Y.10</a>, Comparison with T</em>
  template &lt;class T&gt; constexpr bool operator==(const optional&lt;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator==(const T&amp;, const optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator!=(const optional&lt;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator!=(const T&amp;, const optional&lt;T&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&lt;(const T&amp;, const optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=(const optional&lt;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&lt;=(const T&amp;, const optional&lt;T&gt;&amp;);
  template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, const optional&lt;T&gt;&amp;);
  template &lt;class T&gt; constexpr bool operator&gt;=(const optional&lt;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&gt;=(const T&amp;, const optional&lt;T&gt;&amp;);</ins>

  // <em><a href="#optional.specalg">X.Y.11</a>, Specialized algorithms</em>
  template &lt;class T&gt; void swap(optional&lt;T&gt;&amp;, optional&lt;T&gt;&amp;) noexcept(<em>see below</em>);<!--
  template &lt;class T, class V&gt; 
    constexpr typename decay&lt;T&gt;::type get_value_or(const optional&lt;T&gt;&amp;, V&amp;&amp;);
  template &lt;class T, class V&gt; 
    constexpr typename decay&lt;T&gt;::type get_value_or(optional&lt;T&gt;&amp;&amp;, V&amp;&amp;);-->
  template &lt;class T&gt; constexpr optional&lt;<em>see below</em>&gt; make_optional(T&amp;&amp;);

  // <em><a href="#optional.hash">X.Y.12</a>, hash support</em>
  template &lt;class T&gt; struct hash;
  template &lt;class T&gt; struct hash&lt;optional&lt;T&gt;&gt;;
<ins>} <em>// namespace experimental</em></ins>
} <em>// namespace std</em>
</pre>
  
  <p>A program that necessitates the instantiation of template <code>optional</code> for a reference type, or for possibly cv-qualified types <code>in_place_t</code> or <code>nullopt_t</code> is ill-formed.</p>

<h4><a name="optional.defs">X.Y.3 Definitions <span style="float:right">[optional.defs]</span></a></h4>
 
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>disengaged</em> if <del>it has been default constructed, constructed with or assigned with a value of type <code>nullopt_t</code>, constructed with or assigned with a disengaged optional object of type <code>optional&lt;T&gt;</code></del>
  <ul><li><ins>it default-initialized; or</ins></li>
  <li><ins>it is initialized with a value of type <code>nullopt_t</code> or with a disengaged optional object of type <code>optional&lt;T&gt;</code>; or</ins></li>
  <li><ins>a value of type <code>nullopt_t</code> or a disengaged optional object of type <code>optional&lt;T&gt;</code> is assigned to it</ins>.</li>
  </ul>
  </p>

  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>engaged</em> if it is not disengaged. 
  </p>
  
  <!--
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>disengaged</em> if it has been default constructed, constructed or assigned with a value of type <code>nullopt_t</code>, constructed or assigned with a disengaged optional object of type <code>optional&lt;U&gt;</code>, with <code>U</code> being equal or not to <code>T</code>.
  </p>
  
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>engaged</em> if it has 
  been modified with member function <code>emplace</code>, constructed with a value of type <code>U</code>, assigned a value of type <code>U</code>, copy-constructed from or assigned with an engaged optional object of type <code>optional&lt;U&gt;</code>, where <code>U</code> is same as or convertible to <code>T</code>. 
  </p>
  -->

  
<h4><a name="optional.object">X.Y.4 <code>optional</code> for object types <span style="float:right">[optional.object]</span></a></h4>

  
<pre>
namespace std {
<ins>namespace experimental {</ins>

  template &lt;class T&gt;
  class optional
  {
  public:
    typedef T value_type;

    // <em><a href="#optional.object.ctor">X.Y.4.1</a>, constructors</em>
    constexpr optional() noexcept;
    constexpr optional(nullopt_t) noexcept;
    optional(const optional&amp;);
    optional(optional&amp;&amp;) noexcept(<em>see below</em>);
    constexpr optional(const T&amp;);
    constexpr optional(T&amp;&amp;);<!--
    template &lt;class U&gt; explicit optional(U&amp;&amp;);
    template &lt;class U&gt; optional(U&amp;&amp;);
    template &lt;class U&gt; explicit optional(const optional&lt;U&gt;&amp;);
    template &lt;class U&gt; optional(const optional&lt;U&gt;&amp;);
    template &lt;class U&gt; explicit optional(optional&lt;U&gt;&amp;&amp;);
    template &lt;class U&gt; optional(optional&lt;U&gt;&amp;&amp;);
    template &lt;class U&gt; explicit optional(initializer_list&lt;U&gt;);
    template &lt;class U&gt; optional(initializer_list&lt;U&gt;);-->
    template &lt;class... Args&gt; constexpr explicit optional(in_place_t, Args&amp;&amp;...);
    template &lt;class U, class... Args&gt;
      constexpr explicit optional(in_place_t, initializer_list&lt;U&gt;, Args&amp;&amp;...);

    // <em><a href="#optional.object.dtor">X.Y.4.2</a>, destructor</em>
    ~optional();

    // <em><a href="#optional.object.assign">X.Y.4.3</a>, assignment</em>
    optional&amp; operator=(nullopt_t) noexcept;<!--
    optional&amp; operator=(const T&amp;);
    optional&amp; operator=(T&amp;&amp;);-->
    optional&amp; operator=(const optional&amp;);
    optional&amp; operator=(optional&amp;&amp;) noexcept(<em>see below</em>);
    template &lt;class U&gt; optional&amp; operator=(U&amp;&amp;);<!--
    template &lt;class U&gt; optional&amp; operator=(const optional&lt;U&gt;&amp;);
    template &lt;class U&gt; optional&amp; operator=(optional&lt;U&gt;&amp;&amp;);
    template &lt;class U&gt; optional&amp; operator=(initializer_list&lt;U&gt;);-->
    template &lt;class... Args&gt; void emplace(Args&amp;&amp;...);
    template &lt;class U, class... Args&gt;
      void emplace(initializer_list&lt;U&gt;, Args&amp;&amp;...);

    // <em><a href="#optional.object.swap">X.Y.4.4</a>, swap</em>
    void swap(optional&) noexcept(<em>see below</em>);

    // <em><a href="#optional.object.observe">X.Y.4.5</a>, observers</em>
    constexpr T const* operator -&gt;() const;
    T* operator -&gt;();
    constexpr T const&amp; operator *() const;
    T&amp; operator *();
    constexpr explicit operator bool() const noexcept;
    constexpr T const&amp; value() const;
    T&amp; value();
    template &lt;class U&gt; constexpr T value_or(U&amp;&amp;) const&amp;;
    template &lt;class U&gt; T value_or(U&amp;&amp;) &amp;&amp;;

  <var>private:</var>
    <var>bool init; //</var> <em>exposition only</em>
    <var>T*   val;  //</var> <em>exposition only</em>
  };

<ins>} // <em>namespace experimental</em></ins>
} // <em>namespace std</em></pre>

<p>Engaged instances of <code>optional&lt;T&gt;</code> where <code>T</code> is of object type shall contain a value of type <code>T</code> within its own storage. This value is referred to as the <em>contained value</em> of the optional object. Implementations are not permitted to use additional storage, such as dynamic memory, to allocate its contained value.  The contained value shall be allocated in a region of the <code>optional&lt;T&gt;</code> storage suitably aligned for the type <code>T</code>.

<p>Members <code><var>init</var></code> and <code><var>val</var></code> are provided for exposition only. Implementations need not provide those members. <code><var>init</var></code> indicates whether the optional object's contained value has been initialized (and not yet destroyed); <ins>when <code><var>init</var></code> is true</ins> <code><var>val</var></code> points to <del>(a possibly uninitialized)</del> <ins>the</ins> contained value.
  </p>

<p><code>T</code> shall be an object type and shall satisfy the requirements of <code>Destructible</code> (Table 24).</p>




<h5><a name="optional.object.ctor">X.Y.4.1 Constructors <span style="float:right">[optional.object.ctor]</span></a></h5>

  
  <p class="function">
  <code>constexpr optional&lt;T&gt;::optional() noexcept;</code><br>
  <code>constexpr optional&lt;T&gt;::optional(nullopt_t) noexcept;</code>
  </p>

  <dl class="attribute">
  <dt>Postconditions:</dt> <dd><p><code>*this</code> is disengaged.</p></dd>
  <dt>Remarks:</dt> <dd><p>No <del><code>T</code> object referenced</del><ins>contained value</ins> is initialized. For every object type <code>T</code> these constructors shall be <code>constexpr</code> constructors (7.1.5).</p></dd>
  </dl>

  
  <p class="function">
  <code>optional&lt;T&gt;::optional(const optional&lt;T&gt;&amp; <var>rhs</var>);</code>
  </p>

  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_copy_constructible&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>rhs</var></code> is engaged initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with the expression <code>*<var>rhs</var></code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) == bool(*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>optional&lt;T&gt;::optional(optional&lt;T&gt; &amp;&amp; <var>rhs</var>) noexcept(<em>see below</em>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>rhs</var></code> is engaged initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with the expression <code>std::move(*<var>rhs</var>)</code>. <code>bool(<var>rhs</var>)</code> is unchanged.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) == bool(*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The expression inside <code>noexcept</code> is equivalent to:</p> <pre>is_nothrow_move_constructible&lt;T&gt;::value</pre></dd>
  </dl>

  
  <p class="function">
  <code><ins>constexpr</ins> optional&lt;T&gt;::optional(const T&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_copy_constructible&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with the expression <code><var>v</var></code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>T</code>'s selected constructor is a <code>constexpr</code> constructor, this constructor shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>
  
  
  <p class="function">
  <code><ins>constexpr</ins> optional&lt;T&gt;::optional(T&amp;&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with the expression <code>std::move(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>T</code>'s selected constructor is a <code>constexpr</code> constructor, this constructor shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>
  
<!--  
  <p class="function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(U&amp;&amp; <var>v</var>);<br>template &lt;class U&gt; optional&lt;T&gt;::optional(U&amp;&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&amp;&amp;&gt;::value</code> is true.</p></dd>
    <dt>Effects:</dt> <dd><p>Initializes the contained value as if initializing an object of type <code>T</code> with argument <code>std::forward&lt;U&gt;(<var>v</var>)</code>.</p></dd>

    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged and <code>*(*this)</code> is equivalent to <code><var>v</var></code> if converted to <code>T</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in overload resolution unless <code>OnlyExplicitlyConstructible(T, U&amp;&amp;)</code> is <code>true</code>. The second function shall not participate in the overload resolution unless <code>is_convertible&lt;U&amp;&amp;, T&gt;::value</code> is <code>true</code>. The expression inside <code>noexcept</code> is equivalent to:</p> <pre>noexcept(T(std::declval&lt;U&amp;&amp;&gt;()))</pre></dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(const optional&lt;U&gt;&amp; <var>rhs</var>);<br>template &lt;class U&gt;  optional&lt;T&gt;::optional(const optional&lt;U&gt;&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, const U&amp;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(<var>rhs</var>) == true</code> initializes the contained value as if constructing an object of type <code>T</code> with argument <code>*<var>rhs</var></code></p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) == false</code> then <code>*this</code> is disengaged; otherwise <code>*this</code> is engaged and <code>*(*this)</code> is equivalent to <code>*<var>rhs</var></code> if converted to <code>T</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in overload resolution unless <code>OnlyExplicitlyConstructible(T, const U&amp;)</code> is <code>true</code>. The second function shall not participate in the overload resolution unless <code>is_convertible&lt;const U&amp;, T&gt;::value</code> is <code>true</code>.</dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);<br>template &lt;class U&gt;  optional&lt;T&gt;::optional(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&amp;&amp;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(<var>rhs</var>) == true</code> initializes the contained value as if constructing an object of type <code>T</code> with argument <code>std::move(*<var>rhs</var>)</code></p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in overload resolution unless <code>OnlyExplicitlyConstructible(T, U&amp;&amp;)</code> is <code>true</code>. The second function shall not participate in the overload resolution unless <code>is_convertible&lt;U&amp;&amp;, T&gt;::value</code> is <code>true</code>.</dd>
  </dl>
  
  <p class="function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(initializer_list&lt;U&gt; <var>il</var>);<br>template &lt;class U&gt;  optional&lt;T&gt;::optional(initializer_list&lt;U&gt;<var>il</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(<var>rhs</var>) == true</code> initializes the contained value as if constructing an object of type <code>T</code> with argument <code><var>il</var></code></p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in overload resolution unless <code>OnlyExplicitlyConstructible(T, initializer_list&lt;U&gt;)</code> is <code>true</code>. The second function shall not participate in the overload resolution unless <code>is_convertible&lt;initializer_list&lt;U&gt;&amp;, T&gt;::value</code> is <code>true</code>.</dd>
  </dl>
-->
  
  <p class="function">
  <code>template &lt;class... Args&gt; constexpr explicit optional(in_place_t, Args&amp;&amp;... <var>args</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Initializes the contained value as if <del>constructing</del><ins>direct-non-list-initializing</ins> an object of type <code>T</code> with the arguments <code>std::forward&lt;Args&gt;(<var>args</var>)...</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>T</code>'s constructor selected for the initialization is a <code>constexpr</code> constructor, this constructor shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>


  <p class="function">
  <code>template &lt;class U, class... Args&gt;<br>
  <ins>constexpr</ins> explicit optional(in_place_t, initializer_list&lt;U&gt; <var>il</var>, Args&amp;&amp;... <var>args</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Initializes the contained value as if <del>constructing</del><ins>direct-non-list-initializing</ins> an object of type <code>T</code> with the arguments <code><var>il</var>, std::forward&lt;Args&gt;(<var>args</var>)...</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload resolution unless <code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>T</code>'s constructor selected for the initialization is a <code>constexpr</code> constructor, this constructor shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>


<h5><a name="optional.object.dtor">X.Y.4.2 Destructor  <span style="float:right">[optional.object.dtor]</span></a></h5>
  
  <p class="function"> 
  <code>optional&lt;T&gt;::~optional();</code>
  </p>
  
  <dl class="attribute">
    <dt>Effects:</dt> <dd><p>If <code>is_trivially_destructible&lt;T&gt;::value != true</code> and <code>*this</code> is engaged, calls <code><var>val</var>-&gt;T::~T()</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>is_trivially_destructible&lt;T&gt;::value == true</code> then this destructor shall be a trivial destructor.</p></dd>
  </dl>
  
  
  
<h5><a name="optional.object.assign">X.Y.4.3 Assignment  <span style="float:right">[optional.object.assign]</span></a></h5>
  
  
  <p class="function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(nullopt_t) noexcept;</code>
  </p>

  <dl class="attribute">
    <dt>Effects:</dt> <dd><p>If <code>*this</code> is engaged calls <code><var>val</var>-&gt;T::~T()</code> to destroy the <em>contained value</em>; otherwise no effect.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is disengaged.</p></dd>
  </dl>
  
  <!--
  <p class="function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(const T&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_copy_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_copy_assignable&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>*this</code> is engaged assigns <code><var>rhs</var></code> to the contained value; otherwise initializes the contained value as if direct-non-list-initializing object of type <code>T</code> with <code><var>rhs</var></code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged and <code>*(*this)</code> is equivalent to <code><var>rhs</var></code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, value of <code><var>init</var></code> remains unchanged. If an exception is thrown during the call to <code>T</code>'s copy constructor, no effect. If an exception is thrown during the call to <code>T</code>'s copy assignment, the state of its contained value is as defined by the exception safety guarantee of <code>T</code>'s copy assignment.</p></dd>
  </dl>

  
  <p class="function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(T&amp;&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_move_assignable&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>*this</code> is engaged assigns <code>std::move(<var>rhs</var>)</code> to the contained value; otherwise initializes the contained value as if direct-non-list-initializing object of type <code>T</code> with <code>std::move(<var>rhs</var>)</code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged and <code>*(*this)</code> is equivalent to the value that <code><var>rhs</var></code> had initially.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, value of <code><var>init</var></code> remains unchanged. If an exception is thrown during the call to <code>T</code>'s move constructor, the state of <code><var>rhs</var></code> is determined by 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><var>*val</var></code> and <code><var>rhs</var></code> is determined by exception safety guarantee of <code>T</code>'s move assignment.</p></dd>
  </dl>
  
  -->
  <p class="function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(const optional&lt;T&gt;&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_copy_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_copy_assignable&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <!--<dd><p>If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == false</code>, no effect. If <code><var>init</var> == true && <var>rhs.init</var> == false</code>, destroys the contained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == true</code>, initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with <code>*<var>rhs</var></code>. If <code><var>init</var> == true &amp;&amp; <var>rhs.init</var> == true</code>, assigns <code>*<var>rhs</var></code> to the contained value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>-->
	<dd><br><ul>
	  <li>If <code>*this</code> is disengaged and <code><var>rhs</var></code> is disengaged, no effect, otherwise</li> 
	  <li>if <code>*this</code> is engaged and <code><var>rhs</var></code> is disengaged, destroys the contained value by calling <code><var>val</var>-&gt;T::~T()</code>, otherwise</li>
	  <li>if <code>*this</code> is disengaged and <code><var>rhs</var></code> is engaged, initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with <code>*<var>rhs</var></code>, otherwise</li>
	  <li>(if both <code>*this</code> and <code><var>rhs</var></code> are engaged) assigns <code>*<var>rhs</var></code> to the contained value.</li>
	  </ul></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) == bool(*this)</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, <ins>the</ins> values of <code><var>init</var></code> and <code><var>rhs.init</var></code> remain unchanged. If an exception is thrown during the call to <code>T</code>'s copy constructor, no effect. If an exception is thrown during the call to <code>T</code>'s copy assignment, the state of its contained value is as defined by the exception safety guarantee of <code>T</code>'s copy <del>constructor</del><ins>assignment</ins>.</p></dd>
  </dl>
  
 
  <p class="function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(optional&lt;T&gt;&amp;&amp; <var>rhs</var>) noexcept(<em>see below</em>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_move_assignable&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <!--<dd><p>If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == false</code>, no effect. If <code><var>init</var> == true && <var>rhs.init</var> == false</code>, destroys the contained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == true</code>, initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with <code>std::move(*<var>rhs</var>)</code>. If <code><var>init</var> == true &amp;&amp; <var>rhs.init</var> == true</code>, assigns <code>std::move(*<var>rhs</var>)</code> to the contained value.</p></dd>-->
	<dd><br><ul>
	  <li>If <code>*this</code> is disengaged and <code><var>rhs</var></code> is disengaged, no effect, otherwise</li> 
	  <li>if <code>*this</code> is engaged and <code><var>rhs</var></code> is disengaged, destroys the contained value by calling <code><var>val</var>-&gt;T::~T()</code>, otherwise</li>
	  <li>if <code>*this</code> is disengaged and <code><var>rhs</var></code> is engaged, initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with <code>std::move(*<var>rhs</var>)</code>, otherwise</li>
	  <li>(if both <code>*this</code> and <code><var>rhs</var></code> are engaged) assigns <code>std::move(*<var>rhs</var>)</code> to the contained value.</li>
	  </ul></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) == bool(*this)</code>.</p></dd>

    <dt>Remarks:</dt> <dd><p>The expression inside <code>noexcept</code> is equivalent to:</p> <pre>is_nothrow_move_assignable&lt;T&gt;::value &amp;&amp; is_nothrow_move_constructible&lt;T&gt;::value</pre></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, <ins>the</ins> values of <code><var>init</var></code> and <code><var>rhs.init</var></code> remain unchanged. If an exception is thrown during the call to <code>T</code>'s move constructor, the state of <code>*rhs.val</code> is determined by 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><var>*val</var></code> and <code>*rhs.val</code> is determined by exception safety guarantee of <code>T</code>'s move assignment.</p></dd>

  </dl>
  
  
  <p class="function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(U&amp;&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&gt;::value</code> is <code>true</code> and <code>is_assignable&lt;<del>U, T</del><ins>T&amp;, U</ins>&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>*this</code> is engaged assigns <code>std::forward&lt;U&gt;(<var>v</var>)</code> to the contained value; otherwise initializes the contained value as if direct-non-list-initializing object of type <code>T</code> with <code>std::forward&lt;U&gt;(<var>v</var>)</code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, value of <code><var>init</var></code> remains unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of <code><var>v</var></code> is determined by 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><var>*val</var></code> and <code><var>v</var></code> is determined by exception safety guarantee of <code>T</code>'s assignment.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload resolution unless  <code>is_same&lt;typename <del>remove_reference</del><ins>decay</ins>&lt;U&gt;::type, T&gt;::value</code> is  <code>true</code>.</dd>
  </dl>
  <p>[<i>Note:</i> The reason to provide such generic assignment and then constraining it so that effectively <code>T</code> == <code>U</code> is to guarantee that assignment of the form <code>o = {}</code> is unambiguous. &mdash;<i>end note</i>]</p>

  <!--
  <p class="function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(U&amp;&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&gt;::value</code> is <code>true</code> and <code>is_assignable&lt;U, T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>*this</code> is engaged assigns <code>std::forward&lt;U&gt;(<var>v</var>)</code> to the contained value; otherwise initializes the contained value as if direct-non-list-initializing object of type <code>T</code> with <code>std::forward&lt;U&gt;(<var>v</var>)</code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged and <code>*(*this)</code> is equivalent to the value that <code><var>v</var></code> had initially.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, value of <code><var>init</var></code> remains unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, the state of <code><var>v</var></code> is determined by 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><var>*val</var></code> and <code><var>v</var></code> is determined by exception safety guarantee of <code>T</code>'s assignment.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload resolution if type <code>decay&lt;U&gt;::type</code> is an instantiation of class template <code>optional</code>.</dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(const optional&lt;U&gt;&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, const U&amp;&gt;::value</code> is <code>true</code> and <code>is_assignable&lt;T, const U&amp;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == false</code>, no effect. If <code><var>init</var> == true && <var>rhs.init</var> == false</code>, destroys the contained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == true</code>, initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with <code>*<var>rhs</var></code>. If <code><var>init</var> == true &amp;&amp; <var>rhs.init</var> == true</code>, assigns <code>*<var>rhs</var></code> to the contained value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) == false</code> then <code>*this</code> is disengaged; otherwise <code>*this</code> is engaged and <code>*(*this)</code> is equivalent to <code>*<var>rhs</var></code> converted to <code>T</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, values of <code><var>init</var></code> and <code><var>rhs.init</var></code> remain unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, no effect. If an exception is thrown during the call to <code>T</code>'s  assignment, the state of i<code><var>*val</var></code> is defined by the exception safety guarantee of <code>T</code>'s  assignment.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&amp;&amp;&gt;::value</code> is <code>true</code> and <code>is_assignable&lt;T, U&amp;&amp;&gt;::value</code> is <code>true</code>.</p></dd>

    <dt>Effects:</dt> <dd><p>If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == false</code>, no effect. If <code><var>init</var> == true && <var>rhs.init</var> == false</code>, destroys the contained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code><var>init</var> == false &amp;&amp; <var>rhs.init</var> == true</code>, initializes the contained value as if direct-non-list-initializing an object of type <code>T</code> with <code>std::move(*<var>rhs</var>)</code>. If <code><var>init</var> == true &amp;&amp; <var>rhs.init</var> == true</code>, assigns <code>std::move(*<var>rhs</var>)</code> to the contained value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) == false</code> then <code>*this</code> is disengaged; otherwise <code>*this</code> is engaged and <code>*(*this)</code> is equivalent to the value <code>*<var>rhs</var></code> had initially if converted to <code>T</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, values of <code><var>init</var></code> and <code><var>rhs.init</var></code> remain unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, no effect. If an exception is thrown during the call to <code>T</code>'s assignment, the state of <code><var>*val</var></code> is defined by the exception safety guarantee of <code>T</code>'s assignment.</p></dd>
  </dl>
  

  <p class="function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=(initializer_list&lt;U&gt; <var>il</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;&gt;::value</code> is <code>true</code> and <code>is_assignable&lt;T, initializer_list&lt;U&gt;&amp;&gt;::value</code> is <code>true</code>.</p></dd>
     <dt>Effects:</dt> <dd><p>If <code>*this</code> is engaged assigns <code><var>il</var></code> to the contained value; otherwise initializes the contained value as if direct-non-list-initializing object of type <code>T</code> with <code><var>il</var></code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever <code>T::T(initializer_list&lt;U&gt;)</code> or <code>T::operator=(initializer_list&lt;U&gt;)</code> throws.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, value of <code><var>init</var></code> remains unchanged. If an exception is thrown during the call to <code>T</code>'s constructor, no effect. If an exception is thrown during the call to <code>T</code>'s assignment, the state of its contained value is as defined by the exception safety guarantee of <code>T</code>'s assignemnt.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload resolution unless <code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;&gt;::value</code> is <code>true</code> and <code>is_assignable&lt;T, initializer_list&lt;U&gt;&gt;::value</code> is <code>true</code>.</dd>
  </dl>
-->
  
  <p class="function">
  <code>template &lt;class... Args&gt; void optional&lt;T&gt;::emplace(Args&amp;&amp;... <var>args</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Calls <code>*this = nullopt</code>. Then initializes the contained value as if constructing an object of type <code>T</code> with the arguments <code>std::forward&lt;Args&gt;(<var>args</var>)...</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If an exception is thrown during the call to <code>T</code>'s constructor, <code>*this</code> is disengaged, and the previous <code><var>*val</var></code> (if any) has been destroyed.</p></dd>
  </dl>
  
  
   <p class="function">
  <code>template &lt;class U, class... Args&gt; void optional&lt;T&gt;::emplace(initializer_list&lt;U&gt; <var>il</var>, Args&amp;&amp;... <var>args</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Calls <code>*this = nullopt</code>. Then initializes the contained value as if constructing an object of type <code>T</code> with the arguments <code><var>il</var>, std::forward&lt;Args&gt;(<var>args</var>)...</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If an exception is thrown during the call to <code>T</code>'s constructor, <code>*this</code> is disengaged, and the previous <code><var>*val</var></code> (if any) has been destroyed.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload resolution unless <code>is_constructible&lt;T, initializer_list&lt;U&gt;&amp;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
  </dl>


  
<h5><a name="optional.object.swap">X.Y.4.4 Swap <span style="float:right">[optional.object.swap]</span></a></h5>


  <p class="function">
  <code>void optional&lt;T&gt;::swap(optional&lt;T&gt;&amp; rhs) noexcept(<em>see below</em>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> LValues of type T shall be swappable and <code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <!--<dd><p>If <code><var>init</var> == false && <var>rhs.init</var> == false</code>, no effect. If <code><var>init</var> == true && <var>rhs.init</var> == false</code>, initializes the contained value of <code><var>rhs</var></code> by direct-initialization with <code>std::move(*(*this))</code>, followed by <code>val-&gt;T::~T(), swap(<var>init</var>, <var>rhs.init</var>)</code>. If <code><var>init</var> == false && <var>rhs.init</var> == true</code>, initializes the contained value of <code>*this</code> by direct-initialization with <code>std::move(*<var>rhs</var>)</code>, followed by <code>rhs.val-&gt;T::~T(), swap(<var>init</var>, <var>rhs.init</var>)</code>. If <code><var>init</var> == true && <var>rhs.init</var> == true</code>, calls <code>swap(*(*this), *<var>rhs</var>)</code>.</p></dd>-->
	<dd><br><ul>
	  <li>If <code>*this</code> is disengaged and <code><var>rhs</var></code> is disengaged, no effect, otherwise</li> 
	  <li>if <code>*this</code> is engaged and <code><var>rhs</var></code> is disengaged, initializes the contained value of <code><var>rhs</var></code> by direct-initialization with <code>std::move(*(*this))</code>, followed by <code>val-&gt;T::~T(), swap(<var>init</var>, <var>rhs.init</var>)</code>, otherwise</li>
	  <li>if <code>*this</code> is disengaged and <code><var>rhs</var></code> is engaged, initializes the contained value of <code>*this</code> by direct-initialization with <code>std::move(*<var>rhs</var>)</code>, followed by <code>rhs.val-&gt;T::~T(), swap(<var>init</var>, <var>rhs.init</var>)</code>, otherwise</li>
	  <li>(if both <code>*this</code> and <code><var>rhs</var></code> are engaged) calls <code>swap(*(*this), *<var>rhs</var>)</code>.</li>
	  </ul></dd>
    <dt>Throws:</dt> <dd><p>Any exceptions that the expressions in the Effects clause throw.</p></dd>
    <dt>Remarks:</dt> <dd><p>The expression inside <code>noexcept</code> is equivalent to: <pre>is_nothrow_move_constructible&lt;T&gt;::value &amp;&amp; noexcept(swap(declval&lt;T&amp;&gt;(), declval&lt;T&amp;&gt;()))</pre></dd>
    <dt>Exception Safety:</dt> <dd><p>If any exception is thrown, values of <code><var>init</var></code> and <code><var>rhs.init</var></code> remain unchanged. If an exception is thrown during the call to function <code>swap</code> the state of <code><var>*val</var></code> and <code>*rhs.val</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><var>*val</var></code> and <code>*rhs.val</code> is determined by the exception safety guarantee of <code>T</code>'s move constructor.</p></dd>

  </dl>

  
  
<h5><a name="optional.object.observe">X.Y.4.5 Observers  <span style="float:right">[optional.object.observe]</span></a></h5>


  <p class="function">
  <code>constexpr T const* optional&lt;T&gt;::operator-&gt;() const;<br>T* optional&lt;T&gt;::operator-&gt;();</code>

  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Returns:</dt> <dd><p><code><var>val</var></code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Nothing.</p></dd>
    <dt>Remarks:</dt> <dd><p>Unless <code>T</code> is a user-defined type with overloaded unary <code>operator&amp;</code>, the first function shall be a <code>constexpr</code> function.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>constexpr T const&amp; optional&lt;T&gt;::operator*() const;<br>T&amp; optional&lt;T&gt;::operator*();</code>

  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>*this</code> is engaged.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*<var>val</var></code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Nothing.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall be a <code>constexpr</code> function.</p></dd>

  </dl>
  
  
  <p class="function">
  <code>constexpr explicit optional&lt;T&gt;::operator bool() noexcept;</code>
  </p>
  
  <dl class="attribute">
    <dt>Returns:</dt> <dd><p><code><var>init</var></code>.</p></dd>
    <dt>Remarks:</dt> <dd><p><del>t</del><ins>T</ins>his function shall be a <code>constexpr</code> function.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>constexpr T const&amp; optional&lt;T&gt;::value() const;<br>T&amp; optional&lt;T&gt;::value();</code>
  </p>
  
  <dl class="attribute">
    <dt>Returns:</dt> <dd><p><code><var>*val</var></code>, if <code>bool(*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p><code>bad_optional_access</code> if <code>!*this</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall be a <code>constexpr</code> function.</p></dd>

  </dl>
  
  

  <p class="function">
  <code>template &lt;class U&gt; constexpr T optional&lt;T&gt;::value_or(U&amp;&amp; <var>v</var>) const&amp;;</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_copy_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_convertible&lt;U&amp;&amp;, T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>bool(*this) ? **this : static_cast&lt;T&gt;(std::forward&lt;U&gt;(<var>v</var>))</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> == true</code> and exception is thrown during the call to <code>T</code>'s constructor, the value of <code><var>init</var></code> and <code><var>v</var></code> remains unchanged and the state of <code><var>*val</var></code> is determined by the exception safety guarantee of the selected constructor of <code>T</code>. Otherwise, when exception is thrown during the call to <code>T</code>'s constructor, the value of <code><var>*this</var></code> remains unchanged and the state of <code><var>v</var></code> is determined by the exception safety guarantee of the selected constructor of <code>T</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p><del>If the selected constructor of <code>T</code> is a <code>constexpr</code> constructor</del><ins>If both constructors of <code>T</code> which could be selected are <code>constexpr </code> constructors</ins>, this function shall be a <code>constexpr</code> function.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class U&gt; T optional&lt;T&gt;::value_or(U&amp;&amp; <var>v</var>) &amp;&amp;;</code>
  </p>
  
  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_convertible&lt;U&amp;&amp;, T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code>bool(*this) ? std::move(**this) : static_cast&lt;T&gt;(std::forward&lt;U&gt;(<var>v</var>))</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Any exception thrown by the selected constructor of <code>T</code>.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> == true</code> and exception is thrown during the call to <code>T</code>'s constructor, the value of <code><var>init</var></code> and <code><var>v</var></code> remains unchanged and the state of <code><var>*val</var></code> is determined by the exception safety guarantee of the <code>T</code>'s constructor. Otherwise, when exception is thrown during the call to <code>T</code>'s constructor, the value of <code><var>*this</var></code> remains unchanged and the state of <code><var>v</var></code> is determined by the exception safety guarantee of the selected constructor of <code>T</code>.</p></dd>
  </dl>


  
<h4><a name="optional.inplace">X.Y.5 In-place construction  <span style="float:right">[optional.inplace]</span></a></h4>


  <p class="function">
  <code>struct in_place_t{}; <br>constexpr in_place_t in_place{};</code>
  </p>

  <p>The struct <code>in_place_t</code> is an empty structure type used as a unique type to disambiguate constructor and function overloading. Specifically, <code>optional&lt;T&gt;</code> has a constructor with <code>in_place_t</code> as the first argument followed by an argument pack; this indicates that <code>T</code> should be constructed in-place (as if by a call to placement new expression) with the forwarded argument pack as parameters.
  </p>

  
<h4><a name="optional.nullopt">X.Y.6 Disengaged state indicator  <span style="float:right">[optional.nullopt]</span></a></h4>


  <p class="function">
  <code>struct nullopt_t{<em>see below</em>}; <br>constexpr nullopt_t nullopt(<em>unspecified</em>);</code>
  </p>

  <p>The struct <code>nullopt_t</code> is an empty structure type used as a unique type to indicate a disengaged state for <code>optional</code> objects. In particular, <code>optional&lt;T&gt;</code> has a constructor with <code>nullopt_t</code> as single argument; this indicates that a disengaged optional object shall be constructed.
  </p>
  
  <p>Type <code>nullopt_t</code> shall not have a default constructor. It shall be a literal type. Constant <code>nullopt</code> shall be initialized with an argument of literal type.</p>
  
 
  
<h4><a name="optional.bad_optional_access">X.Y.7 Class <code>bad_optional_access</code>  <span style="float:right">[optional.bad_optional_access]</span></a></h4>
 
<pre>namespace std {
  class bad_optional_access : public logic_error {
  public:
    explicit bad_optional_access(const string&amp; what_arg);
    explicit bad_optional_access(const char* what_arg);
  };
}</pre>

<p>The class <code>bad_optional_access</code> defines the type of objects thrown as exceptions to report the situation where an attempt is made to access the value of a disengaged optional object.</p>

  <p class="function"> 
  <code>bad_optional_access(const string&amp; what_arg);</code>
  </p>
  
  <dl class="attribute">
  <dt>Effects:</dt> <dd><p>Constructs an object of class <code>bad_optional_access</code>.</p></dd>
  <dt>Postcondition:</dt> <dd><p><code>strcmp(what(), what_arg.c_str()) == 0</code>.</p></dd>
  </dl>
  
  <p class="function"> 
  <code>bad_optional_access(const char* what_arg);</code>
  </p>
  
  <dl class="attribute">
  <dt>Effects:</dt> <dd><p>Constructs an object of class <code>bad_optional_access</code>.</p></dd>
  <dt>Postcondition:</dt> <dd><p><code>strcmp(what(), what_arg) == 0</code>.</p></dd>
  </dl>

 
<h4><a name="optional.relops">X.Y.8 Relational operators  <span style="float:right">[optional.relops]</span></a></h4>
 

  <p class="function"> 
  <code>template &lt;class T&gt; constexpr bool operator==(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code>
  </p>
  
  <dl class="attribute">
  <dt>Requires:</dt> <dd><p><code>T</code> shall meet the requirements of <code>EqualityComparable</code>.</p></dd>
  <dt>Returns:</dt> <dd><p>If <code>bool(x) != bool(y)</code>, <code>false</code>; otherwise if <code>bool(x) == false</code>, <code>true</code>; otherwise <code>*x == *y</code>.</p></dd>
  <dt>Remarks:</dt> <dd><p>Instantiations of this function template for which <code>*x == *y</code> is a core constant expression, shall be <code>constexpr</code> functions.</p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator!=(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(x == y)</code>.</ins></p></dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&gt;&amp; <var>x</var>, const optional&lt;T&gt;&amp; <var>y</var>);</code> 
  </p>

  <dl class="attribute">
  <dt>Requires:</dt> <dd><p>Expression <del><code>less&lt;T&gt;{}(*<var>x</var>, *<var>y</var>)</code></del><ins><code>*<var>x</var> &lt; *<var>y</var></code></ins> shall be well-formed <ins>and its result shall be convertible to <code>bool</code></ins>.</p></dd>
  <dt>Returns:</dt> <dd><p>If <code>(!<var>y</var>)</code>, <code>false</code>; otherwise, if <code>(!<var>x</var>)</code>, <code>true</code>; otherwise <del><code>less&lt;T&gt;{}(*<var>x</var>, *<var>y</var>)</code></del><ins><code>*<var>x</var> &lt; *<var>y</var></code></ins>.</p></dd>
  <dt>Remarks:</dt> <dd><p>Instantiations of this function template for which <del><code>less&lt;T&gt;{}(*<var>x</var>, *<var>y</var>)</code></del> <ins>expression <code>*<var>x</var> &lt; *<var>y</var></code></ins> is a core constant expression, shall be <code>constexpr</code> functions.</p></dd>
  </dl>

  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code></ins>
  </p>

  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>(y &lt; x)</code>.</ins></p></dd>
  </dl>
  
 
  <p class="function"> 
  <ins><code>template &lt;class T&gt; constexpr bool operator&lt;=(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code></ins>
  </p>

  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(y &lt; x)</code>.</ins></p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;=(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code></ins> 
  </p>

  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(x &lt; y)</code>.</ins></p></dd>
  </dl>

 
<h4><a name="optional.nullops">X.Y.9 Comparison with <code>nullopt</code> <span style="float:right">[optional.nullops]</span></a></h4>
 
  
  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator==(const optional&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;<br>template &lt;class T&gt; constexpr bool operator==(nullopt_t, const optional&lt;T&gt;&amp; x) noexcept;</code>
  </p>
  
  <dl class="attribute">
  <dt>Returns:</dt> <dd><p><code>(!<var>x</var>)</code>.</p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator!=(const optional&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;<br>template &lt;class T&gt; constexpr bool operator!=(nullopt_t, const optional&lt;T&gt;&amp; x) noexcept;</code></ins>
  </p>

  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>)</code>.</ins></p></dd>
  </dl> 

  <!-- -->
  
  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;</code>
  </p>
  
  <dl class="attribute">
  <dt>Returns:</dt> <dd><p><code>false</code>.</p></dd>
  </dl>

  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(nullopt_t, const optional&lt;T&gt;&amp; <var>x</var>) noexcept;</code>
  </p>
  
  <dl class="attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>)</code>.</p></dd>
  </dl>

  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&lt;=(const optional&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>(!<var>x</var>)</code>.</ins></p></dd>
  </dl>

  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&lt;=(nullopt_t, const optional&lt;T&gt;&amp; <var>x</var>) noexcept;</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>true</code>.</ins></p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>)</code>.</ins></p></dd>
  </dl>

  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;(nullopt_t, const optional&lt;T&gt;&amp; <var>x</var>) noexcept;</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>false</code>.</ins></p></dd>
  </dl>

  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;=(const optional&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>true</code>.</ins></p></dd>
  </dl>

  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;=(nullopt_t, const optional&lt;T&gt;&amp; <var>x</var>) noexcept;</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>(!<var>x</var>)</code>.</ins></p></dd>
  </dl>
 
  
  
 
<h4><a name="optional.comp_with_t">X.Y.10 Comparison with <code>T</code> <span style="float:right">[optional.comp_with_t]</span></a></h4>


  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator==(const optional&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> == <var>v</var> : false</code>.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator==(const T&amp; <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
  
  <dl class="attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> == *<var>x</var> : false</code>.</p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator!=(const optional&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>) ? !(*<var>x</var> == <var>v</var>) : true</code>.</ins></p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator!=(const T&amp; <var>v</var>, const optional&lt;T&gt;&amp; x);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>) ? !(<var>v</var> == *<var>x</var>) : true</code>.</ins></p></dd>
  </dl>
  
  <!-- -->
  
  <p class="function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br></code>
  </p>
  
  <dl class="attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <del>less&lt;T&gt;{}(*<var>x</var>, <var>v</var>)</del><ins>*<var>x</var> &lt; <var>v</var></ins> : true</code>.</p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp; <var>v</var>, const optional&lt;T&gt;&amp; <var>x</var>);<br></code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>) ? <var>v</var> &lt; *<var>x</var> : false</code>.</ins></p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp; <var>v</var>, const optional&lt;T&gt;&amp; x);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>) ? *<var>x</var> &lt; <var>v</var> : true</code>.</ins></p></dd>
  </dl>
  
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>bool(<var>x</var>) ? <var>v</var> &lt; *<var>x</var> : false</code>.</ins></p></dd>
  </dl>

  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;=(const optional&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(<var>x</var> &lt; <var>v</var>)</code>.</ins></p></dd>
  </dl>
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&gt;=(const T&amp; <var>v</var>, const optional&lt;T&gt;&amp; x);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(<var>v</var> &lt; <var>x</var>)</code>.</ins></p></dd>
  </dl>
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&lt;=(const optional&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(<var>x</var> &gt; <var>v</var>)</code>.</ins></p></dd>
  </dl>
  
  <p class="function">
  <ins><code>template &lt;class T&gt; constexpr bool operator&lt;=(const T&amp; <var>v</var>, const optional&lt;T&gt;&amp; x);</code></ins>
  </p>
  
  <dl class="attribute">
  <dt><ins>Returns:</ins></dt> <dd><p><ins><code>!(<var>v</var> &gt; <var>x</var>)</code>.</ins></p></dd>
  </dl>
  
  
<h4><a name="optional.specalg">X.Y.11 Specialized algorithms <span style="float:right">[optional.specalg]</span></a></h4>


  <p class="function">
  <code>template &lt;class T&gt; void swap(optional&lt;T&gt;&amp; x, optional&lt;T&gt;&amp; y) noexcept(noexcept(x.swap(y)));</code>
  </p>

  <dl class="attribute">
    <!--<dt>Requires:</dt> <dd><p><code>is_reference&lt;T&gt;::value == false</code>.</p></dd>-->
    <dt>Effects:</dt> <dd><p>calls <code>x.swap(y)</code>.</p>  
  </dl><!--


  <p class="function">
  <code>template &lt;class T, class V&gt;<br>
    &nbsp;&nbsp;typename decay&lt;T&gt;::type get_value_or(const optional&lt;T&gt;&amp; <var>op</var>, V&amp;&amp; <var>v</var>);</code>
  </p>

  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_copy_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_convertible&lt;V&amp;&amp;, T&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Returns:</dt> <dd><p><code><var>op</var> ? *<var>op</var> : static_cast&lt;T&gt;(std::forward&lt;V&gt;(<var>v</var>))</code>.</p></dd>
  </dl>
  
  
  <p class="function">
  <code>template &lt;class T, class V&gt;<br>
    &nbsp;&nbsp;typename decay&lt;T&gt;::type get_value_or(optional&lt;T&gt;&amp;&amp; <var>op</var>, V&amp;&amp; <var>v</var>);</code>
  </p>

  <dl class="attribute">
    <dt>Requires:</dt> <dd><p><code>is_move_constructible&lt;T&gt;::value</code> is <code>true</code> and <code>is_convertible&lt;V&amp;&amp;, T&gt;::value</code> is <code>true</code>.</p></dd>

    <dt>Returns:</dt> <dd><p><code><var>op</var> ? std::move(*<var>op</var>) : static_cast&lt;T&gt;(std::forward&lt;V&gt;(<var>v</var>))</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This function provides the same exception safety as <code>T</code>'s move-constructor.</p></dd>
  </dl>-->
  
  
  <p class="function">
  <code>template &lt;class T&gt;<br>
    &nbsp;&nbsp;constexpr optional&lt;typename decay&lt;T&gt;::type&gt; make_optional(T&amp;&amp; <var>v</var>);</code>
  </p>
  
  <dl class="attribute">
    <dt>Returns:</dt> <dd><p><code>optional&lt;typename decay&lt;T&gt;::type&gt;(std::forward&lt;T&gt;(<var>v</var>))</code>.</p></dd>
  </dl>
  
  
<h4><a name="optional.hash">X.Y.12 Hash support <span style="float:right">[optional.hash]</span></a></h4>


  <p class="function">
  <code>template &lt;class T&gt; struct hash&lt;optional&lt;T&gt;&gt;;</code>
  </p>

  <dl class="attribute">
    <dt>Requires:</dt> <dd><p>the template specilaization <code>hash&lt;T&gt;</code> shall meet the requirements of class template <code>hash</code> (Z.X.Y).
      The template specilaization <code>hash&lt;optional&lt;T&gt;&gt;</code> shall meet the requirements of class template <code>hash</code>. 
      For an object <code>o</code> of type <code>optional&lt;T&gt;</code>, if <code>bool(o) == true</code>, 
      <code>hash&lt;optional&lt;T&gt;&gt;()(o)</code> shall evaluate to the same value as <code>hash&lt;T&gt;()(*o)</code>; <ins>otherwise it evaluates to an unspecified value</ins>.</p></dd> 
  </dl>
  
</blockquote>





<h2><a name='implementability'>Implementability</a></h2>


<p>This proposal can be implemented as pure library extension, without any compiler magic support, in C++11. An almost full rerefence implementation of this proposal can be found at <a href="https://github.com/akrzemi1/Optional/">https://github.com/akrzemi1/Optional/</a>. Below we demonstrate how one can implement <code>optional</code>'s <code>constexpr</code> constructors to engaged and disengaged state as well as <code>constexpr</code> <code>operator*</code> for <code>TriviallyDestructible</code> <code>T</code>'s. </p>


<pre>namespace std {

#if defined NDEBUG
# define ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR)
#else
# define ASSERTED_EXPRESSION(CHECK, EXPR) ((CHECK) ? (EXPR) : (fail(#CHECK, __FILE__, __LINE__), (EXPR)))
  inline void fail(const char* expr, const char* file, unsigned line) { <em>/*...*/</em> }
#endif

struct dummy_t{};

template &lt;class T&gt;
union optional_storage
{
  static_assert( is_trivially_destructible&lt;T&gt;::value, "" );

  dummy_t dummy_;
  T       value_;

  constexpr optional_storage()            <em>// null-state ctor</em>
    : dummy_{} {}

  constexpr optional_storage(T const&amp; v)  <em>// value ctor</em>
    : value_{v} {}

  ~optional_storage() = default;          <em>// trivial dtor</em>
};


template &lt;class T&gt;
<em>// requires: is_trivially_destructible&lt;T&gt;::value</em>
class optional
{
  bool initialized_;
  optional_storage&lt;T&gt; storage_;

public:
  constexpr optional(nullopt_t) : initialized_{false}, storage_{} {}

  constexpr optional(T const&amp; v) : initialized_{true}, storage_{v} {}

  constexpr T const&amp; operator*() 
  {
    return ASSERTED_EXPRESSION(bool(*this), storage_.value_);
  }
  
  constexpr T const&amp; value()
  {
    return *this ? storage_.value_ : (throw bad_optional_access(""), storage_.value_);
  }

  <em>// ...</em>
};

} <em>// namespace std</em>
</pre>



<h2><a name='acknowledgements'>Acknowledgements</a></h2>


<p>Many people from the Boost community, participated in the developement of the Boost.Optional library. Sebastian Redl suggested the usage of function <code>emplace</code>. </p>

<p>Daniel Kr&uuml;gler provided numerous helpful suggestions, corrections and comments on this paper; in particular he suggested the addition of and reference implementation for "perfect initialization" operations.</p>

<p>Tony Van Eerd offered many useful suggestions and corrections to the proposal.</p>

<p>People in discussion group "ISO C++ Standard - Future Proposals" provided numerous insightful suggestions: Vladimir Batov (who described and supported the perfect forwarding constructor), Nevin Liber, Ville Voutilainen, Richard Smiths, Dave Abrahams, Chris Jefferson, Jeffrey Yasskin, Nikolay Ivchenkov, Matias Capeletto, Olaf van der Spek, Vincent Jacquet, Kazutoshi Satoda, Vicente J. Botet Escriba, R&oacute;bert D&aacute;vid, Vincent Jacquet, Luc Danton, Greg Marr, and many more. </p>

<p>Joe Gottman suggested the support for hashing some optional objects.</p>

<p>Nicol Bolas suggested to make <code>operator-&gt;</code> conditionally <code>constexpr</code> based on whether <code>T::operator&amp;</code> is overloaded.</p>



<h2><a name='literature'>References</a></h2>


<ol>
<li>John J. Barton, Lee R. Nackman, "Scientific and Engineering C++: An Introduction with Advanced Techniques and Examples".</li>

<li>Fernando Cacciola, Boost.Optional library (<a href='http://www.boost.org/doc/libs/1_49_0/libs/optional/doc/html/index.html'>http://www.boost.org/doc/libs/1_49_0/libs/optional/doc/html/index.html</a>)</li>

<li>MSDN Library, "Nullable Types (C# Programming Guide)", (<a href='http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx'>http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx</a></li>

<li>Code Synthesis Tools, "C++ Object Persistence with ODB", (<a href='http://www.codesynthesis.com/products/odb/doc/manual.xhtml#7.3'>http://www.codesynthesis.com/products/odb/doc/manual.xhtml#7.3</a>)</li>

<li>Jaakko J&auml;rvi, Boost Tuple Library (<a href="http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html">http://www.boost.org/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html</a>) </li>

<li>Alisdair Meredith, John Lakos, "<code>noexcept</code> Prevents Library Validation" (N3248, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3248.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3248.pdf</a>)</li>

<li>Walter E. Brown, "A Preliminary Proposal for a Deep-Copying Smart Pointer" (N3339, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3339.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3339.pdf</a>)</li>

<li>Andrzej Krzemie&#x144;ski, Optional library implementation in C++11 (<a href="https://github.com/akrzemi1/Optional/">https://github.com/akrzemi1/Optional/</a>)</li>
</ol>



  

</body>
</html>
