Topic: std::optional -- request for feedback on the wording


Author: =?UTF-8?Q?Andrzej_Krzemie=C5=84ski?= <akrzemi1@gmail.com>
Date: Fri, 15 Feb 2013 05:44:08 -0800 (PST)
Raw View
------=_Part_1783_16865104.1360935848759
Content-Type: multipart/alternative;
 boundary="----=_Part_1784_17530136.1360935848759"

------=_Part_1784_17530136.1360935848759
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi everyone,
We have updated the proposal based on recent feedback. I enclose the latest=
=20
draft. It is also available at:
http://kojot.sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html
Changes include:

   1. Semantics of mixed relops for irregular types (thanks to Tony Van=20
   Eerd)
   2. Removed postconditions that would not be possible to achieve for=20
   irregular types (thanks to Tony Van Eerd)
   3. operator-> and most of operations on optional refs are not constexpr=
=20
   if our type has overloaded operator& (because there is no way to impleme=
nt=20
   a constexpr addressof) (thanks to Nicol Bolas)
   4. Added function value() that throws on disengaged optionals (thanks to=
=20
   Daniel Kr=FCgler)
  =20
We would appreciate your feedback, especially on the standardese.

Regards,
&rzej

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



------=_Part_1784_17530136.1360935848759
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi everyone,<br>We have updated the proposal based on recent feedback. I en=
close the latest draft. It is also available at:<br><a href=3D"http://kojot=
..sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html">http://kojot.sg=
gw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html</a><br>Changes incl=
ude:<br><ol><li>Semantics of mixed relops for irregular types (thanks to To=
ny Van Eerd)<br></li><li>Removed postconditions that would not be possible =
to achieve for irregular types (thanks to Tony Van Eerd)</li><li>operator-&=
gt; and most of operations on optional refs are not constexpr if our type h=
as overloaded operator&amp; (because there is no way to implement a constex=
pr addressof) (thanks to Nicol Bolas)</li><li>Added function value() that t=
hrows on disengaged optionals (thanks to Daniel Kr=FCgler)<br></li></ol><p>=
We would appreciate your feedback, especially on the standardese.</p><p>Reg=
ards,<br>&amp;rzej<br></p>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1784_17530136.1360935848759--
------=_Part_1783_16865104.1360935848759
Content-Type: text/html; charset=US-ASCII; name=tr2.optional.proposal.html
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=tr2.optional.proposal.html
X-Attachment-Id: 9d4f679e-8e2e-4947-8e45-7053fee56477
Content-ID: <9d4f679e-8e2e-4947-8e45-7053fee56477>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/htm=
l4/strict.dtd">
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html;charset=3DUS-ASCII">

<style type=3D"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;=20
  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 (Rev=
ision 2)</title>
<script src=3D"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.mi=
n.js" type=3D"text/javascript"> </script>
<script type=3D"text/javascript">$(function() {
    var next_id =3D 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 =3D node[0];
        while (cur) {
            if (cur.id) return curid;
            if (cur.tagName =3D=3D 'A' && cur.name)
                return cur.name;
            cur =3D 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 =3D [$("<ol/>")];
    $(':header').not('h1').each(function() {
        var header =3D $(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 =3D {H2: 0, H3: 1, H4: 2, H5: 3, H6: 4};
        var level =3D levels[this.tagName];
        if (typeof level =3D=3D '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 =3D=3D level) {
            // Add a <ol> to the previous level's last <li> and push
            // it into the array.
            var ol =3D $('<ol/>')
            toc[toc.length - 1].children().last().append(ol);
            toc.push(ol);
        }
        var header_text =3D header.text();
        toc[toc.length - 1].append(
            $('<li/>').append($('<a href=3D"#' + find_id(header) + '"/>')
                              .text(header_text)));
    });
    $('#toc').append(toc[0]);
})
</script>
</head>

<body>


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

<h1><a name=3D'title'>A proposal to add a utility class to represent option=
al objects (Revision&nbsp;3)</a></h1>

<p>
ISO/IEC JTC1 SC22 WG21 D3527
</p>

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

</address>


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

<p>Class template <code>optional&lt;T&gt;</code> proposed here is a type th=
at 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 curren=
tly stored, and if so, to access it. The interface is based on Fernando Cac=
ciola's Boost.Optional library<sup>[2]</sup>, shipping since March, 2003, a=
nd widely used. It requires no changes to core language, and breaks no exis=
ting code.</p>


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

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


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

<p>Changes since <a href=3D'http://www.open-std.org/jtc1/sc22/wg21/docs/pap=
ers/2012/n3406.html'>N3406=3D12-0096</a>:</p>
<ul>
  <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 t=
he 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 membe=
r function, renamed to <code>value_or</code>.</li>
  <li>Added member function <code>value</code> &mdash; an alternative to <c=
ode>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> an=
d <code>T</code> are now allowed.</li>
  <li>Removed reference implementation. We now only provide the implementat=
ion of the parts that we consider non-trivial.</li>
</ul>

<p>Changes since <a href=3D'http://www.open-std.org/jtc1/sc22/wg21/docs/pap=
ers/2005/n1878.htm'>N1878=3D05-0138</a>:</p>
<ul>
  <li>Revised wording; the changes are now relative to <a href=3D'http://ww=
w.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 opt=
ional.</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>, varia=
dic 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=3D'motivation'>Motivation and scope</a></h2>



<h3><a name=3D'motivation.missingval'>Missing return values</a></h3>


<p>It sometimes happens that a function that is declared to return values o=
f type <code>T</code> may not have a value to return. For one instance, con=
sider function <code>double sqrt(double)</code>. It is not defined for nega=
tive numbers. <em>The only way</em> function <b>sqrt</b> can find itself in=
 the situation where it cannot produce a value is when it is passed an inva=
lid argument, and it is possible for the caller to check if the argument is=
 valid. Therefore, such cases are dealt with by stating a function <em>prec=
ondition</em>. Violating the precondition may either result in undefined be=
havior, or (as it is the case for <code>std::vector::at</code>) may be defi=
ned to throw an exception. However, there are cases where we cannot know wh=
ether a function is able to return a value before we call it. For instance:=
=20

</p>

<pre>char c =3D stream.getNextChar();
int x =3D DB.execute("select x_coord from coordinates where point_id =3D 11=
2209");
</pre>

<p>In the first case, one could argue that <code>stream</code> could provid=
e another function for checking if the end of stream has been reached. But =
for the latter there is no easy way to make sure that the requested informa=
tion exists in the database before we request for it. Also, throwing an exc=
eption is not a good solution, because there is nothing unusual or erroneou=
s about not having found a record in the DB.
</p>



<h3><a name=3D'motivation.optargs'>Optional function arguments</a></h3>


<p>Sometimes it is useful to indicate that we are unable to, or do not want=
 to, provide one of the function arguments:</p>

<pre>void execute( function&lt;void(int)&gt; fa, function&lt;void(int)&gt; =
fb )
{
  int i =3D computeI();
  fa(i); <em>// implicit assumption that fa !=3D nullptr</em>
  if (fb) {
    fb(i);
  }
}
</pre>

<p>
Here, <code>fb</code> is an <em>optional</em> argument: it may simply repre=
sent no function. It typically requires an if-statement in function body. I=
t is often argued that such design can be replaced by providing two functio=
n overloads; however, this also is problematic: it adds certain duplication=
; may make the code less clear; the number of overloads increases expotenti=
ally with the number of optional parameters. <code>std::function</code> is =
already an optional type, however, other types in general are not; for inst=
ance, <code>std::pair&lt;int, std::string&gt;</code>.
</p>



<h3><a name=3D'motivation.trackinit'>Indicating a <em>null-state</em></a></=
h3>


<p>Some types are capable of storing a special <em>null-state</em> or <em>n=
ull-value</em>. It indicates that no 'meaningful' value has been assigned t=
o the object yet. It is then possible to query the object whether it contai=
ns a null-state, typically by invoking a contextual conversion to type <cod=
e>bool</code> or by checking for equality with <code>nullptr</code>. Exampl=
es of such types include: <code>std::unique_ptr</code> or <code>std::functi=
on</code>. This is convenient for instance when implementing a <q>lazy init=
ialization</q> optimization:

</p>

<pre>class Car=20
{
  mutable mutex m_;
  mutable unique_ptr&lt;const Engine&gt; engine_;
 =20
public:
  const Engine&amp; engine() const
  {
    lock_guard&lt;mutex&gt; _(m_);
    if (engine_ =3D=3D nullptr) {
      engine_.reset( new engine{engineParams()} );
    }
    return *engine_;
  }
};</pre>

<p>Other types, on the other hand, do not provide this capability. For inst=
ance, type <code>int</code> does not have a special value that would signal=
 the null-state. Any value of <code>int</code> is a valid value. The same g=
oes for user defined types: e.g., <code>Rational&lt;int&gt;</code> that imp=
lements a rational number by holding two <code>int</code>&rsquo;s represent=
ing a numerator and a denumerator.</p>

<p>For a similar example, consider this function for finding biggest <code>=
int</code>.</p>

<pre>
int find_biggest( const vector&lt;int&gt;& vec )
{
  int biggest; <em>// =3D what initial value??</em>
  for (int val : vec) {
    if (biggest &lt; val) {
      biggest =3D val;
    }
  }
  return biggest;
}=20
</pre>

<p>
What initial value should we assign to <code>biggest</code>? Not 0, because=
 the vector may hold negative values. We can use <code>std::numeric_limits&=
lt;int&gt;::min()</code>. But what if we want to make our function generic?=
 <code>std::numeric_limits</code> will not be specialized for any possible =
type. And what if the vector contains no elements? Shall we return minimum =
possible value? But how will the caller know if the size of the vector was =
zero, or if it was one with the minimum possible value?
</p>



<h3><a name=3D'motivation.guards'>Manually controlling the lifetime of scop=
e guards</a></h3>


<p>
Consider that you have to run three actions in the fixed order:
</p>
<pre>
void runAction1( Resource1 & );
void runAction2( Resource1 &, Resource2 & );
void runAction3( Resource2 & );
</pre>

<p>Of course, the two resources need to be acquired before using them and r=
eleased once we are done. This is how we would write it conceptually, if we=
 could assume that none of the operations ever fails or throws exceptions:<=
/p>

<pre>
Result run3Actions( Parameter param ) <i>// BAD!!</i>
{
  Resource1 res1;
  Resource2 res2;

  res1.acquire(param);       <i>// res1 scope</i>
  runAction1( res1 );        <i>//</i>
  res2.acquire(param);       <i>//    // res2 scope</i>
  runAction2( res1, res2 );  <i>//    //</i>
  res1.release();            <i>//    //</i>
  runAction3( res2 );        <i>      //</i>
  res2.release();            <i>      //</i>
}
</pre>

<p>Note that the scopes of the two resources <em>overlap.</em> we cannot cl=
early divide our function into nested scopes that correspond to lifetimes o=
f the resources. This example represents a real-life situation if you imagi=
ne that  <tt>runAction2</tt> is a critical operation, and <tt>runAction3</t=
t> (and perhaps <tt>runAction1</tt>) is logging, which we can even skip if =
it fails. </p>

<p>Of course, this code is unacceptable, because it does not take into acco=
unt that  acquire operations, and the three actions may fail. So, we want t=
o rewrite <tt>run3Actions</tt> using RAII idiom. Especially, that the libra=
ry author that provides the interface to the two resources also knows that =
RAII is probably always what we want and the only interface he provides are=
 scope guards:=20

</p>

<pre>
Result run3Actions( Parameter param ) <i>// selfish</i>
{
  Resource1Guard res1{param};
  Resource2Guard res2{param};

  runAction1( res1 );
  runAction2( res1, res2 );  =20
  runAction3( res2 );
}
</pre>

<p>This solution is somewhat elegant: you first acquire all resources that =
you will need, and once you are sure you have them all, you just run the ac=
tions. But it has one problem. Someone else will also need to use resources=
 we are acquiring. If they need it at the moment, but we own the resource r=
ight now, they will be locked, or thrown a refusal exception &mdash; they w=
ill be disturbed and delayed. Using a resource is a critical part of the pr=
ogram, and we should be only acquiring them for as short a period as possib=
le, for the sake of efficiency: not only our program's but of the entire op=
erating system. In our example above, we hold the ownership of <tt>res2</tt=
> while executing <tt>runAction1</tt>, which does not require the resource.=
 Similarly, we unnecessarily hold <tt>res1</tt> when calling <tt>runAction3=
</tt>. </p>

<p>So, how about this?</p>

<pre>
Result run3Actions( Parameter param ) <i>// slow and risky</i>
{
  {
    Resource1Guard res1{param};
    runAction1( res1 );
  }
  {
    Resource1Guard res1{param};
    Resource2Guard res2{param};
    runAction2( res1, res2 ); =20
  } =20
  {
    Resource2Guard res2{param};
    runAction3( res2 );
  }=20
}
</pre>

<p>It does not have the above-mentioned problem, but it introduces a differ=
ent one. Now we have to acquire the same resource two times in a row for tw=
o subsequent actions. Resource acquisition is critical, may lock us, may sl=
ow us down, may fail; calling it four times rather than two is wrong, espec=
ially that acquiring the same resource twice may give us different resource=
 properties in each acquisition, and it may not work for our actions.</p>=
=20

<h3><a name=3D'motivation.skipinit'>Skipping the expensive initialization</=
a></h3>

<p>Not all types provide a cheap default construction, even if they are sma=
ll in size. If we want to put such types in certain containers, we are risk=
ing paying the price of expensive default construction even if we want to a=
ssign a different value a second later.

</p>

<pre>ExpensiveCtor array[100]; <em>// 100 default constructions</em>
std::array&lt;ExpensiveCtor, 100&gt; arr; <em>// 100 default constructions<=
/em>
std::vector&lt;ExpensiveCtor&gt; vec(100); <em>// 100 default constructions=
</em>

</pre>

<h3><a name=3D'motivation.practise'>Current techniques for solving the prob=
lems</a></h3>


<p>The above mentioned problems are usually dealt with in C++ in three ways=
:</p>
<ol>
  <li>Using ad-hoc special values, like <code>EOF</code>, -1, 0, <code>nume=
ric_limits&lt;T&gt;::min()</code>.</li>

  <li>Using additional flags, like <code>pair&lt;T, bool&gt;</code>.</li>
  <li>Using pointers even if the object would have been passed or returned =
by value if it wouldn't be possibly absent.</li>
</ol>

<p>Technique (1) suffers from the lack of uniformity. Special value, like <=
code>EOF</code> is not something characteristic of the type (<code>int</cod=
e>), but of the particular function. Other function may need to use a diffe=
rent special value. The notable example is the standard function <code>atoi=
</code>, where 0 may indicate either an unsuccessful conversion, or a succe=
ssful conversion to value 0. And again, sometimes it is not even possible t=
o designate a value with a special meaning, because all values of a given t=
ype are equally likely to appear; for instance:

</p>

<pre>bool accepted =3D DB.execute("select accepted from invitations where i=
nvitation_id =3D 132208");</pre>

<p>Technique (2) is problematic because it requires both members of <code>p=
air</code> to be initialized. But one of the reasons we are not returning a=
 'normal' value from a function or do not want to initialize the object jus=
t yet, is because we did not know how to initialize it. In case of optional=
 <code>int</code> we could give it a rubbish value or leave it uninitialize=
d, but user defined types, including resources, may not provide a default c=
onstructor, and require some effort to perform initialization.</p>

<p>Technique (3) is general. It uses value <code>nullptr</code> to indicate=
 the special value. Users can unequivocally test if the value is indeed abs=
ent and being formally undefined behavior to access an object pointed to by=
 a null pointer. It also does not require initializing the object unnecessa=
rily. But it comes with different problems. Automatic objects created insid=
e functions cannot be returned by pointer. Thus it often requires creating =
objects in free store which may be more expensive than the expensive initia=
lization itself. It also requires manual memory management problems. The la=
tter problem can be dealt with by using <code>std::unique_ptr</code>, but t=
his is still a pointer, and we must abandon value semantics. Now we return =
a smart pointer: we have shallow copy and shallow comparison.</p>



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


<p>This proposal depends on library proposal <a href=3D"http://www.open-std=
..org/jtc1/sc22/wg21/docs/papers/2012/n3471.html">N3471</a>: it requires tha=
t Standard Library components <code>move</code>, <code>forward</code> and m=
ember functions of <code>initializer_list</code> are <code>constexpr</code>=
.. The paper has already been incorporated into the Working Draft of the Sta=
ndard <a href=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3=
485.pdf">N3485</a>. This proposal also depends on language proposal <a href=
=3D"www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm">N2439</a> (=
Rvalue references for <code>*this</code>). While the latter feature proposa=
l has been incorporated into C++11, we are not aware of any compiler implem=
entation. There is a risk that if compiler vendors do not implement it, the=
y 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=3D"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=3D'overview'>Overview of <code>optional</code></a></h2>


<p>The primary purpose of <code>optional&lt;T&gt;</code>'s interface is to =
be able to answer the quesiotn "do you contain a value of type <code>T</cod=
e>?", and iff the answer is "yes", to provide access to the contained value=
.. Conceptually, <code>optional</code> can be illustrated by the following s=
tructure.</p>

<pre>template &lt;typename T&gt;
struct optional
{
  bool is_initialized_;
  typename aligned_storage&lt;sizeof(T), alignof(T)&gt;::type storage_;
};</pre>

<p>Flag <code>is_initialized_</code> stores the information if <code>option=
al</code> has been assigned a value of type <code>T</code>. <code>storage_<=
/code> is a raw fragment of memory (allocated within <code>optional</code> =
object) capable of storing an object of type <code>T</code>. Objects in thi=
s storage are created and destroyed using placement <code>new</code> and ps=
eudo destructor call as member functions of <code>optional</code> are execu=
ted and based on the value of flag <code>is_initialized_</code>.</p>=20

<p>Alternatively, one can think of <code>optional&lt;T&gt;</code> as <code>=
pair&lt;bool, T&gt;</code>, with one important difference: if <code>first</=
code> is <code>false</code>, member <code>second</code> has never been even=
 initialized, even with default constructor or value-initialization.</p>

<p>The basic usage of <code>optional&lt;T&gt;</code> can be illustrated wit=
h the following example.</p>

<pre>optional&lt;int&gt; str2int(string);    <em>// converts int to string =
if possible</em>

int get_int_form_user()
{
  string s;

  for (;;) {
    cin &gt;&gt; s;
    optional&lt;int&gt; o =3D str2int(s); <em>// 'o' may or may not contain=
 an int</em>
    if (o) {                      <em>// does optional contain a value?</em=
>
      return *o;                  <em>// use the value</em>
    }
  }
}
</pre>


<h3><a name=3D'overview.interface'>Interface of <code>optional</code></a></=
h3>

<p>Default construction of <code>optional&lt;T&gt;</code> creates an an obj=
ect that stores no value of type <code>T</code>. No default constructor of =
<code>T</code> is called. <code>T</code> doesn't even need to be <code>Defa=
ultConstructible</code>. We say that thus created optional object is <em>di=
sengaged</em>. We use a pair of somewhat arbitrary names, 'engaged' and 'di=
sengaged'. A default-constructed optional object is initialized (the lifeti=
me of <code>optional&lt;T&gt;</code> has started), but it is disengaged (th=
e lifetime of the contained object has not yet started). Trying to access t=
he value of <code>T</code> in this state causes undefined behavior. The onl=
y thing we can do with a disengaged optional object is to query whether it =
is engaged, copy it, compare it with another <code>optional&lt;T&gt;</code>=
, or <em>engage</em> it.</p>

<pre>optional&lt;int&gt; oi;                 <em>// create disengaged objec=
t</em>
optional&lt;int&gt; oj =3D nullopt;       <em>// alternative syntax</em>
oi =3D oj;                          <em>// assign disengaged object</em>
optional&lt;int&gt; ok =3D oj;            <em>// ok is disengaged</em>

if (oi)  assert(false);           <em>// 'if oi is engaged...'</em>
if (!oi) assert(true);            <em>// 'if oi is disengaged...'</em>

if (oi !=3D nullopt) assert(false); <em>// 'if oi is engaged...'</em>
if (oi =3D=3D nullopt) assert(true);  <em>// 'if oi is disengaged...'</em>

assert(oi =3D=3D ok);                 <em>// two disengaged optionals compa=
re equal</em>
</pre>

<p>Tag <code>nullopt</code> represents the disengaged state of an optional =
object. This reflects our conceptual model of optional objects: <code>optio=
nal&lt;T&gt;</code> can be thought of as a <code>T</code> with one addition=
al value <code>nullopt</code>. Being disengaged is just another value of <c=
ode>T</code>. Thus, <code>optional&lt;unsigned&gt;</code> can be thought of=
 as a type with possible range of values <code>{nullopt, 0, 1, ...}</code>.=
 Value <code>nullopt</code> is always picked by the default constructor.</p=
>


<p>We can create engaged optional objects or engage existing optional objec=
ts by using converting constructor (from type <code>T</code>) or by assigni=
ng a value of type <code>T</code>.

<pre>optional&lt;int&gt; ol{1};              <em>// ol is engaged; its cont=
ained value is 1</em>
ok =3D 2;                           <em>// ok becomes engaged; its containe=
d value is 2</em>
oj =3D ol;                          <em>// oj becomes engaged; its containe=
d value is 1</em>

assert(oi !=3D ol);                 <em>// disengaged !=3D engaged</em>
assert(ok !=3D ol);                 <em>// different contained values</em>
assert(oj =3D=3D ol);                 <em>// same contained value</em>
assert(oi &lt; ol);                  <em>// disengaged &lt; engaged</em>
assert(ol &lt; ok);                  <em>// less by contained value</em>
</pre>

<p>Copy constructor and copy assignment of <code>optional&lt;T&gt;</code> c=
opies both the engaged/disangaged flag and the contained value, if it exist=
s. </p>

<pre>optional&lt;int&gt; om{1};              <em>// om is engaged; its cont=
ained value is 1</em>
optional&lt;int&gt; on =3D om;            <em>// on is engaged; its contain=
ed value is 1</em>
om =3D 2;                           <em>// om is engaged; its contained val=
ue is 2</em>
assert (on !=3D om);                <em>// on still contains 3. They are no=
t pointers</em>
</pre>

<p>We access the contained value using the indirection operator.</p>

<pre>int i =3D *ol;                      <em>// i obtains the value contain=
ed in ol</em>
assert(i =3D=3D 1);
*ol =3D 9;                          <em>// the object contained in ol becom=
es 9</em>
assert(*ol =3D=3D 9);
</pre>

<p>We also provide consistent <code>operator-&gt;</code>. Even though <code=
>optional</code> provides operators <code>-&gt;</code> and <code>*</code> i=
t is not a pointer. Unlike pointers, it has full value semantics: deep copy=
 construction, deep copy assignmet, deep equality and less-than comparison,=
 and constness propagation (from optional object to the contained value).</=
p>

<pre>int p =3D 1;
optional&lt;int&gt; op =3D p;
assert(*op =3D=3D 1);
p =3D 2;                        =20
assert(*op =3D=3D 1);                 <em>// value contained in op is separ=
ated from p</em>
</pre>

<p>The typical usage of <code>optional</code> requires an if-statement.</p>

<pre>if (ol)                     =20
  process(*ol);                   <em>// use contained value if present</em=
>
else
  processNil();                   <em>// proceed without contained value</e=
m>
 =20
if (!om)  =20
  processNil();
else =20
  process(*om);    =20
</pre>

=20
<p>If the action to be taken for disengaged <code>optional</code> is to pro=
ceed with the default value, we provide a convenient idiom: </p>=20

<pre>process(ol.value_or(0));      <em>// use 0 if ol is disengaged</em>
</pre>

<p>Sometimes the initialization from <code>T</code> may not do. If we want =
to skip the copy/move construction for <code>T</code> because it is too exp=
ensive or simply not available, we can call an 'emplacing' constructor, or =
function <code>emplace</code>.</p>

<pre>optional&lt;Guard&gt; oga;                     <em>// Guard is non-cop=
yable (and non-moveable)</em>    =20
optional&lt;Guard&gt; ogb(emplace, "res1");    <em>// initialzes the contai=
ned value with "res1" </em>          =20
optional&lt;Guard&gt; ogc(emplace);            <em>// default-constructs th=
e contained value</em>

oga.emplace("res1");                     <em>// initialzes the contained va=
lue with "res1" </em>=20
oga.emplace();                           <em>// destroys the contained valu=
e and </em>
                                         <em>// default-constructs the new =
one</em>
</pre>

<p>There are two ways to <em>disengage</em> a perhaps engaged optional obje=
ct:</p>

<pre>ok =3D nullopt;                      <em>// if ok was engaged calls T'=
s dtor</em>
oj =3D {};                           <em>// assigns a temporary disengaged =
optional</em>
oga =3D nullopt;                     <em>// OK: disengage the optional Guar=
d</em>
ogb =3D {};                          <em>// ERROR: Guard is not Moveable</e=
m>
</pre>


<p>Optional propagates constness to its contained value:</p>

<pre>const optional&lt;int&gt; c =3D 4;=20
int i =3D *c;                        <em>// i becomes 4</em>
*c =3D i;                            <em>// ERROR: cannot assign to const i=
nt&amp;</em>
</pre>



<h3><a name=3D'overview.except.safety'>Exception safety</a></h3>


<p>Type <code>optional&lt;T&gt;</code> is a wrapper over <code>T</code>, th=
us its exception safety guarantees depend on exception safety guarantees of=
 <code>T</code>. We expect (as is the case for the entire C++ Standard Libr=
ary) that destructor of <code>T</code> does not throw exceptions. Copy assi=
gnment of <code>optional&lt;T&gt;</code> provides the same exception guaran=
tee as copy assignment of <code>T</code> and copy constructor of <code>T</c=
ode> (i.e., the weakest of the two). Move assignment of <code>optional&lt;T=
&gt;</code> provides the same exception guarantee as move assignment of <co=
de>T</code> and move constructor of <code>T</code>. Member function <code>e=
mplace</code> provides basic guarantee: if exception is thrown, <code>optio=
nal&lt;T&gt;</code> becomes disengaged, regardless of its prior state. </p>



<h3><a name=3D'overview.usecases'>Advanced use cases</a></h3>


<p>With <code>optional&lt;T&gt;</code> problems described in Motivation sec=
tion can be solved as follows. For lazy initialization:</p>


<pre>class Car=20
{
  mutable mutex m_;
  mutable optional&lt;const Engine&gt; engine_;
  mutable optional&lt;const int&gt; mileage_
 =20
public:
  const Engine&amp; engine() const
  {
    lock_guard&lt;mutex&gt; _(m_);
    if (engine_ =3D=3D nullopt) engine_.emplace( engineParams() );
    return *engine_;
  }
 =20
  const int&amp; mileage() const
  {
    lock_guard&lt;mutex&gt; _(m_);
    if (!mileage_) mileage_ =3D initMileage();
    return *mileage_;
  }
};</pre>

<p>The algorithm for finding the greatest element in vector can be written =
as: </p>


<pre>
optional&lt;int&gt; find_biggest( const vector&lt;int&gt;& vec )
{
  optional&lt;int&gt; biggest;  <em>// initialized to not-an-int</em>
  for (int val : vec) {
    if (!biggest || *biggest &lt; val) {
      biggest =3D val;
      <em>// or: biggest.emplace(val);</em>
    }
  }
  return biggest;
}=20
</pre>


<p>Missing return values are naturally modelled by optional values:</p>

<pre>optional&lt;char&gt; c =3D stream.getNextChar();
optional&lt;int&gt; x =3D DB.execute("select ...");

storeChar( c.value_or('\0') );
storeCount( x.value_or(-1) );
</pre>

<p>Optional arguments can be implemented as follows:<p>

<pre>template &lt;typename T&gt;
T getValue( optional&lt;T&gt; newVal =3D nullopt )
{
  if (newVal) {
    cached =3D *newVal;    =20
  }
  return cached;     =20
}
</pre>

<p>Manually controlling the life-time of guard-like objects can be achieved=
 by emplacement operations and <code>nullopt</code> assignment:</p>

<pre>{
  optional&lt;Guard&gt; grd1{emplace, "res1", 1};   <em>// guard 1 initiali=
zed</em>
  optional&lt;Guard&gt; grd2;

  grd2.emplace("res2", 2);                     <em>// guard 2 initialized</=
em>
  grd1 =3D nullopt;                              <em>// guard 1 released</e=
m>

}                                              <em>// guard 2 released (in =
dtor)</em>
</pre>

<p>It is possible to use <code>tuple</code> and <code>optional</code> to em=
ulate multiple return valuse for types without default constructor:</p>

<pre>tuple&lt;Date, Date, Date&gt; getStartMidEnd();
void run(Date const&amp;, Date const&amp;, Date const&amp;);
<em>// ...</em>

optional&lt;Date&gt; start, mid, end;           <em>// Date doesn't have de=
fault ctor (no good default date)</em>

tie(start, mid, end) =3D getStartMidEnd();
run(*start, *mid, *end);=20
</pre>



<h3><a name=3D'overview.value_ptr_comparison'>Comparison with <code>value_p=
tr</code></a></h3>

<p><a href=3D'http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3339=
..pdf'>N3339</a><sup>[7]</sup> proposes a smart pointer template <code>value=
_ptr</code>. In short, it is a smart pointer with deep copy semantics. It h=
as a couple of features in common with <code>optional</code>: both contain =
the notion of optionality, both are deep-copyable. Below we list the most i=
mportant differences. </p>

<p><code>value_ptr</code> requires that the pointed-to object is allocated =
in the free store. This means that the <code>sizeof(value_ptr&lt;T&gt;)</co=
de> is fixed irrespective of <code>T</code>. <code>value_ptr</code> is 'pol=
ymorphic': object of type <code>value_ptr&lt;T&gt;</code> can point to an o=
bject of type <code>DT</code>, derived from <code>T</code>. The deep copy p=
reserves the dynamic type. <code>optional</code> requires no free store all=
ocation: its creation is more efficient; it is not "polymorphic".</p>

<p>Relational operations on <code>value_ptr</code> are shallow: only addres=
ses are compared. Relational operations on <code>optional</code> are deep, =
based on object's value. In general, <code>optional</code> has a well defin=
ed value: being disengaged and the value of contained value (if it exists);=
 this value is expressed in the semantics of equality operator. This makes =
<code>optional</code> a full value-semantic type. Comparison of <code>value=
_ptr</code> does not have this property: copy semantics are incompatible wi=
th equality comparison semantics: a copy-constructed <code>value_ptr</code>=
 does not compare equal to the original. <code>value_ptr</code> is not a va=
lue semantic type.=20
</p>



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


<p>The very minimum, ascetic interface for optional objects &mdash; apart f=
rom copy/move &mdash; could consist of two constructors and two functions:<=
/p>

<pre><em>// not proposed</em>
int i =3D 9;
optional&lt;int&gt; oi{engaged, i};          <em>// create engaged</em>
optional&lt;int&gt; oj{disengaged};          <em>// create disengaged</em>
if (oi.is_engaged()) {                 <em>// contains value?</em>
  oi.get_value();                      <em>// access the value</em>
}
</pre>

<p>The reason we provide different and richer interface is motivated by use=
rs' convenience, performance improvements, secondary goals we want to achei=
ve, and the attempt to standardize the existing practice.</p>

<p>Nearly every function in the interface of <code>optional</code> has rise=
n some controversies. Different people have different expectations and diff=
erent concerns and it is not possible to satisfy all conflicting requiremen=
ts. Yet, we believe that <code>optional</code> is so universally useful, th=
at it is worth standardizing it even at the expense of introducing a contor=
versial interface. The current proposal reflects our arbitrary choice of ba=
lance between unambiguousity, genericity and flexibility of the interface. =
The interface is based on Fernanndo Cacciola's <a href=3D'http://www.boost.=
org/doc/libs/1_48_0/libs/optional/doc/html/index.html'>Boost.Optional</a> l=
ibrary,<sup>[2]</sup> and the users' feedback. The library has been widely =
accepted, used and even occasionally recommended ever since.=20
</p>



<h3><a name=3D'rationale.model'>Conceptual model for <code>optional&lt;T&gt=
;</code></a></h3>


<p>Optional objects serve a number of purposes and a couple of conceptual m=
odels can be provided to answer the question what <code>optional&lt;T&gt;</=
code> really is and what interface it should provide. The three most common=
 models are:</p>

<ol>
  <li>Just a <code>T</code> with deferred initialization (and additional in=
terface to check if the object has already been initialized).</li>
  <li>A discriminated union of types <code>nullopt_t</code> and <code>T</co=
de>.</li>
  <li>A container of <code>T</code>'s with the maximum size of 1.</li>
</ol>

<p>While (1) was the first motivation for <code>optional</code>, we do not =
choose to apply this model, because type <code>optional&lt;T&gt;</code> wou=
ld not be a value semantic type: it would not model concept <code>Regular</=
code> (if C++ had concepts). In particular, it would be not clear whather b=
eing engaged or disengaged is part of the object's state. Programmers who w=
ish to adapt this view, and don't mind the mentioned difficulties, can stil=
l use <code>optional</code> this way:</p>

<pre>optional&lt;int&gt; oi;
initializeSomehow(oi);

int i =3D (*oi);
use(*oi);
(*oi) =3D 2;
cout &lt;&lt; (*oi);
</pre>

<p>Note that this usage does not even require to check for engaged state, i=
f one is sure that the object is engaged. One just needs to use indirection=
 operator consistently anywhere one means to use the initialized value. </p=
>

<p>Model (2) treats <code>optional&lt;T&gt;</code> as etier a value of type=
 <code>T</code> or value <code>nullopt</code>, allocated in the same storag=
e, along with the way of determining which of the two it is. The interface =
in this model requires operations such as comparison to <code>T</code>, com=
parison to <code>nullopt</code>, assignment and creation from either. It is=
 easy to determine what the value of the optional object is in this model: =
the type it stores (<code>T</code> or <code>nullopt_t</code>) and possibly =
the value of <code>T</code>. This is the model that we propose.</p>

<p>Model (3) treats <code>optional&lt;T&gt;</code> as a special case contai=
ner. This begs for a container-like interface: <code>empty</code> to check =
if the object is disengaged, <code>emplace</code> to engage the object, and=
 <code>clear</code> to disengage it. Also, the value of optional object in =
this model is well defined: the size of the container (0 or 1) and the valu=
e of the element if the size is 1. This model would serve our pupose equall=
y well. The choice between models (2) and (3) is to a certain degree arbitr=
ary. One argument in favour of (2) is that it has been used in practise for=
 a while in Boost.Optional.</p>

<p>Additionally, within the affordable limits, we propose the view that <co=
de>optional&lt;T&gt;</code> just extends the set of the values of <code>T</=
code> by one additional value <code>nullopt</code>. This is reflected in in=
itialization, assignment, ordering, and equality comparison with both <code=
>T</code> and <code>nullopt</code>.</p>

<pre>optional&lt;int&gt; oi =3D 0;
optional&lt;int&gt; oj =3D 1;
optional&lt;int&gt; ok =3D nullopt;

oi =3D 1;
oj =3D nullopt;
ok =3D 0;

oi =3D=3D nullopt;
oj =3D=3D 0;
ok =3D=3D 1;
</pre>


<h3><a name=3D'rationale.initialization_variants'>Initialization of <code>o=
ptional&lt;T&gt;</code></a></h3>


<p>In cases <code>T</code> is a value semantic type capable of storing <em>=
n</em> distinct values, <code>optional&lt;T&gt;</code> can be seen as an ex=
tended <code>T</code> capable of storing <em>n</em> + 1 values: these that =
<code>T</code> stores and <code>nullopt</code>. Any valid initialization sc=
heme must provide a way to put an optional object to any of these states. I=
n addition, some <code>T</code>s (like scope guards) are not <code>MoveCons=
tructible</code> and their optional variants still should constructible wit=
h any set of arguments that work for <code>T</code>. Two models have been i=
dentified as feasible.</p>

<p>The first requires that you initialize either by providing an already co=
nstructed <code>T</code> on the tag <code>nullopt</code>.</p>

<pre>string s{"STR"};

optional&lt;string&gt; os{s};                   <em> // requires Copyable&l=
t;T&gt;</em>
optional&lt;string&gt; ot =3D s;                  <em> // requires Copyable=
&lt;T&gt;</em>
optional&lt;string&gt; ou{"STR"};               <em> // requires Movable&lt=
;T&gt;</em>
optional&lt;string&gt; ov =3D string{"STR"};      <em> // requires Movable&=
lt;T&gt;</em>

optional&lt;string&gt; ow;                      <em> // disengaged</em>
optional&lt;string&gt; ox{};                    <em> // disengaged</em>
optional&lt;string&gt; oy =3D {};                 <em> // disengaged</em>
optional&lt;string&gt; oz =3D optional&lt;string&gt;{}; <em> // disengaged<=
/em>
optional&lt;string&gt; op{nullopt};             <em> // disengaged</em>
optional&lt;string&gt; oq =3D {nullopt};          <em> // disengaged</em>
</pre>

<p>In order to avoid calling move/copy constructor of <code>T</code>, we us=
e a 'tagged' placement constructor: </p>

<pre>optional&lt;Guard&gt; og;                       <em> // disengaged</em=
>
optional&lt;Guard&gt; oh{};                     <em> // disengaged</em>
optional&lt;Guard&gt; oi{emplace};              <em> // calls Guard{} in pl=
ace</em>
optional&lt;Guard&gt; oj{emplace, "arg"};       <em> // calls Guard{"arg"} =
in place</em>
</pre>

<p>The in-place constructor is not strictly necessary. It could be dropped =
because one can always achieve the same effect with a two-liner:</p>

<pre>optional&lt;Guard&gt; oj;                       <em> // start disengag=
ed</em>
oj.emplace("arg");                        <em> // now engage</em>
</pre>


<p>Notably, there are two ways to create a disengaged optional object: eith=
er by using the default constructor or by calling the 'tagged constructor' =
that takes <code>nullopt</code>. One of these could be safely removed and <=
code>optional&lt;T&gt;</code> could still be initialized to any state. </p>

<p>An alternative and also comprehensive initialization scheme is to have a=
 variadic perfect forwarding constructor that just forwards any set of argu=
ments to the constructor of the contained object of type <code>T</code>:

<pre><em>// not proposed</em>

optional&lt;string&gt; os{"STR"};               <em> // calls string{"STR"}=
 in place </em>
optional&lt;string&gt; ot{2, 'a'};              <em> // calls string{2, 'a'=
} in place</em>
optional&lt;string&gt; ou{};                    <em> // calls string{} in p=
lace</em>
optional&lt;string&gt; or;                      <em> // calls string{} in p=
lace</em>

optional&lt;Guard&gt; og;                       <em> // calls Guard{} in pl=
ace</em>
optional&lt;Guard&gt; oh{};                     <em> // calls Guard{} in pl=
ace</em>
optional&lt;Guard&gt; ok{"arg"};                <em> // calls Guard{"arg"} =
in place</em>

optional&lt;vector&lt;int&gt;&gt; ov;                 <em> // creates 0-ele=
ment vector</em>
optional&lt;vector&lt;int&gt;&gt; ow{};               <em> // creates 0-ele=
ment vector</em>
optional&lt;vector&lt;int&gt;&gt; ox{5, 6};           <em> // creates 5-ele=
ment vector</em>
optional&lt;vector&lt;int&gt;&gt; oy{{5, 6}};         <em> // creates 2-ele=
ment vector</em>
</pre>

<p>In order to create a disengaged optional object a special tag needs to b=
e used: either <code>nullopt</code> or <code>T</code>-based:</p>

<pre>optional&lt;int&gt; oi =3D nullopt;               <em> // disengaged <=
/em>
optional&lt;int&gt; oj =3D optional&lt;int&gt;::nullopt;<em> // disengaged<=
/em>
</pre>

<p>The latter, perfect forwarding variant, has an obvious advantage: whatev=
er you can initialize <code>T</code> with, you can also use it to initializ=
e <code>optional&lt;T&gt;</code> with the same semantics. It becomes even m=
ore useful in copy-initialization contexts like returning values from funct=
ions or passing arguments to functions:</p>

<pre>void putch(optional&lt;char&gt; oc);
putch('c');
putch({});       // char '\0'
putch(nullopt);  // no char
</pre>

<p> However, there are also certain problems with this model. First, there =
are exceptions to perfect forwarding: the tag <code>nullopt</code> is not f=
orwarded. Also, arguments of type <code>optional&lt;T&gt;</code> are not. T=
his becomes visible if <code>T</code> has such constructor:</p>

<pre>struct MyType
{
  MyType(optional&lt;MyType&gt;, int =3D 0);
  <em>// ... </em>
};
</pre>

<p>Also, in general, it is impossible to perfect-forward <code>initializer_=
list</code> as the special deduction rules are involved. Second problem is =
that <code>optional</code>'s default constructor creates an engaged object =
and therby triggers the value-initialization of <code>T</code>. It can be a=
rgued that this is only a psychological argument resulting from the fact th=
at other <code>std</code> components behave this way: containers, smart poi=
nters, <code>function</code>, and only because perfect forwarding was not a=
vailable at the time they were designed. However we anticipate that this ex=
pectation (valid or not) could cause bugs in programs. Consider the followi=
ng example:</p>

<pre>optional&lt;char&gt; readChar(Stream str)
{
  optional&lt;char&gt; ans;

  if (str.buffered()) {
    ans =3D str.readBuffered();
    clearBuffer();
  }
  else if (!str.end()) {
    ans =3D str.readChar();
  }

  return ans;
}=20
</pre>

<p>This is so natural to expect that <code>ans</code> is not engaged until =
we decide how we wan to engage it, that people will write this code. And in=
 the effect, if they cannod read the character, they will return an engaged=
 optional object with value <code>'\0'</code>. For these reasons we choose =
to propose the former model.</p>



<h3><a name=3D'rationale.default_ctor'>The default constructor</a></h3>


<p>This proposal provides a default constructor for <code>optional&lt;T&gt;=
</code> that creates a disengaged optional. We find this feature convenient=
 for a couple of reasons. First, it is because this behaviour is intuitive =
as shown in the above example of function <code>readChar</code>. It avoids =
a certain kind of bugs. Also, it satisfies other expectations. If I declare=
 <code>optional&lt;T&gt;</code>  as a non-static member, without any initia=
lizer, I may expect it is already initialized to the most natural, disengag=
ed, state regardless of whether <code>T</code> is <code>DefaultConstructibl=
e</code> or not. Also when declaring a global object, one could expect that=
 default constructor would be initialized during static-initialization (thi=
s proposal guarantees that). One could argue that the tagged constructor co=
uld be used for that puropse: </p>

<pre>
optional&lt;int&gt; global =3D nullopt;

struct X
{
  optional&lt;M&gt; m =3D nullopt;
};
</pre>

<p>However, sometimes not providing the tag may be the result of an inadver=
tent omission rather than concious decision. Because of our default constru=
ctor semantics we have to reject the initialization scheme that uses a perf=
ect forwarding constructor. Even if this is fine, one could argue that we d=
o not need a default constructor if we have a tagged constructor. We find t=
his redundancy convenient. For instance, how do you resize a <code>vector&l=
t;optional&lt;T&gt;&gt;</code> if you do not have the default constructor? =
You could type:</p>

<pre>vec.resize(size, nullopt);</pre>

<p>However, that causes first the creation of disengaged optional, and then=
 copying it multiple times. The use of copy constructor may incur run-time =
overhead and not be available for non-copyable <code>T</code>s. Also, it wo=
uld be not possible to use subscript operator in maps that hold optional ob=
jects.</p>

<p>Also, owing to this constructor, <code>optional</code> has a nice side-e=
ffect feature: it can make "almost <code>Regular</code>" types fully <code>=
Regular</code> if the lack of default constructor is the only thing they ar=
e missing. For instance consider type <code>Date</code> for representing ca=
lendar days: it is copyable movable, comparable, but is not <code>DefaultCo=
nstructible</code> because there is no meaningful default date. However, <c=
ode>optional&lt;Date&gt;</code> is <code>Regular</code> with a meaningful n=
ot-a-date state created by default.</p>



<h3><a name=3D'rationale.converting_ctor'>Converting constructor (from <cod=
e>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 =3D 1; <em>// works</em></pre>

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

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

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

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

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

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

<p>It has been argued that the constructor from <code>T</code> should be ma=
de explicit. It is not trivial to decide whether <code>T</code> should be c=
onvertiblle to <code>optional&lt;T&gt;</code>. This is not a clear situatio=
n where value of one type is stored in another type with greater resolution=
, or the situation where the same abstract value is stored in a type with d=
ifferent internal representation. On the other hand, given our conceptual m=
odel, <code>optional&lt;T&gt;</code> can store all values of <code>T</code>=
, so it is possible to apply a "lossless conversion". We decided to provide=
 the conversion, in order to (1) adhere to our conceptual model, and (2) to=
 enable the above convenience for function argument passing. The implicit c=
onversion naturally implies that <code>optional&lt;T&gt;</code>'s can be co=
mpared with <code>T</code>'s. This is discussed further down. </p>

<p>At some point we considered the possibility to make this constructor con=
ditionally explicit: make it explicit if <code>T</code> has an explicit cop=
y/move constructor, and make it non-explicit if <code>T</code> has a normal=
, non-explicit constructor. In the end, we find explicit copy constructor s=
o unusual that we do not find it worthwile to addressing it at the expense =
of compliicating the design.</p>

<h3><a name=3D'rationale.bool_conversion'>Contextual conversion to <code>bo=
ol</code> for checking engaged state</a></h3>


<p>Objections have been risen to this decision. When using <code>optional&l=
t;bool&gt;</code>, contextual conversion to <code>bool</code> (used for che=
cking the engaged state) might be confused with accessing the stored value.=
 while such mistake is possible, it is not precedent in the standard: types=
 <code>bool*</code>, <code>unique_ptr&lt;bool&gt;</code>, <code>shared_ptr&=
lt;bool&gt;</code> suffer from the same potential problem, and it was never=
 considered a show-stopper. Some have suggested that a special case in the =
interface should be made for <code>optional&lt;bool&gt;</code> specializati=
on. This was however rejected because it would break the generic use of <co=
de>optional</code>. </p>

<p>Some have also suggested that a member function like <code>is_initialize=
d</code> would more clearly indicate the intent than explicit conversion to=
 <code>bool</code>. However, we believe that the latter is a well establish=
ed idiom in C++ comunity as well as in the C++ Standard Library, and <code>=
optional</code> appears so fundamental a type that a short and familiar not=
ation appears more appropriate. It also allows us to combine the constructi=
on and checking for being engaged in a condition:
=09</p>

<pre>if (optional&lt;char&gt; ch =3D readNextChar()) {
  // ...
}
</pre>


<h3><a name=3D'rationale.nullopt'>Using tag <code>nullopt</code> for indica=
ting disengaged state</a></h3>

<p>The proposed interface uses special tag <code>nullopt</code> to indicate=
 disengaged <code>optional</code> state. It is used for construction, assig=
nment and relational operations. This might rise a couple of objections. Fi=
rst, it introduces redundancy into the interface:</p>

<pre>optional&lt;int&gt; opt1 =3D nullopt;=20
optional&lt;int&gt; opt2 =3D {};=20

opt1 =3D nullopt;
opt2 =3D {};

if (opt1 =3D=3D nullopt) ...
if (!opt2) ...
if (opt2 =3D=3D optional&lt;int&gt;{}) ...
</pre>

<p>On the other hand, there are usages where the usage of <code>nullopt</co=
de> cannot be replaced with any other convenient notation:</p>

<pre>void run(complex&lt;double&gt; v);
void run(optional&lt;string&gt; v);

run(nullopt);              <em>// pick the second overload</em>
run({});                   <em>// ambiguous</em>

if (opt1 =3D=3D nullopt) ...   <em>// fine</em>
if (opt2 =3D=3D {}) ...        <em>// illegal</em>

bool is_engaged( optional&lt;int&gt; o)
{
  return bool(o);          <em>// ok, but unclear</em>
  return o !=3D nullopt;     <em>// familiar</em>
}
</pre>

<p>While some situations would work with <code>{}</code> syntax, using <cod=
e>nullopt</code> makes the programmer's intention more clear. Compare these=
:</p>

<pre>optional&lt;vector&lt;int&gt;&gt; get1() {
  return {};
}

optional&lt;vector&lt;int&gt;&gt; get2() {
  return nullopt;
}

optional&lt;vector&lt;int&gt;&gt; get3() {
  return optional&lt;vector&lt;int&gt;&gt;{};
}
</pre>

<p>The usage of <code>nullopt</code> is also a consequence of the adapted m=
odel for optional: a discriminated union of <code>T</code> and <code>nullop=
t_t</code>. Also, a similar redundancy in the interface already exists in a=
 number of components in the standard library: <code>unique_ptr</code>, <co=
de>shared_ptr</code>, <code>function</code> (which use literal <code>nullpt=
r</code> for the same purpose); in fact, type requirements <code>NullablePo=
inter</code> require of types this redundancy. </p>

<p>Name "nullopt" has been chosen because it clearly indicates that we are =
interested in creating a null (disengaged) <code>optional&lt;T&gt;</code> (=
of unspecified type <code>T</code>). Other short names like "null", "naught=
", "nothing" or "none" (used in Boost.Optional library) were rejected becau=
se they were too generic: they did not indicate unambiguously that it was <=
code>optional&lt;T&gt;</code> that we intend to create. Such a generic tag =
<code>nothing</code> could be useful in many places (e.g., in types like <c=
ode>variant&lt;nothing_t, T, U&gt;</code>), but is outside the scope of thi=
s proposal.</p>
=20

<p>Note also that the definition of tag struct <code>nullopt</code> is more=
 complicated than that of other, similar, tags: it has explicitly deleted d=
efault constructor. This is in order to enable the reset idiom (<code>opt2 =
=3D {};</code>), which would otherwise not work because of ambiguuity when =
deducing the right-hand side argument.</p>


<h3><a name=3D'rationale.no_nullptr'>Why not <code>nullptr</code></a></h3>


<p>One could argue that since we have keyword <code>nullptr</code>, which a=
lready indicates a 'null-state' for a number of Standard Library types, not=
 necessarily pointers (class template <code>function</code>), it could be e=
qually well used for <code>optional</code>. In fact, the previous revision =
of this proposal did propose <code>nullptr</code>, however there are certai=
n difficulties that arise when the null-pointer literal is used.</p>

<p>First, the interface of <code>optional</code> is already criticized for =
resembling too much the interface of a (raw or smart) pointer, which incorr=
ectly suggests external heap storage and shallow copy and comparison semant=
ics. The "ptr" in "nullptr" would only increase this confusion. While <code=
>std::function</code> is not a pointer either, it also does not provide a c=
onfusing <code>operator-&gt;</code>, or equality comparison, and in case it=
 stores a function pointer it does shallow copying.</p>

<p>Second, using literal <code>nullptr</code> in <code>optional</code> woul=
d make it impossible to provide some of the natural and expected initializa=
tion and assignment semantics for types that themselves are nullable:</p>

<ul>
<li><code>optional&lt;int*&gt;</code>, </li>
<li><code>optional&lt;const char*&gt;</code>, </li>
<li><code>optional&lt;M C::*&gt;</code>, </li>
<li><code>optional&lt;function&lt;void(int)&gt;&gt;</code>, </li>
<li><code>optional&lt;NullableInteger&gt;</code>,</li>
<li><code>optional&lt;nullptr_t&gt;</code>.</li>
</ul>

<p>Should the following initialization render an engaged or a disengaged op=
tional?</p>

<pre>optional&lt;int*&gt; op =3D nullptr;</pre>

<p>One could argue that if we want to initialize an engaged optional we sho=
uld indicate that explicitly: </p>

<pre>optional&lt;int*&gt; op{emplace, nullptr};</pre>

<p>But this argument would not work in general. One of the goals of the des=
ign of <code>optional</code> is to allow a seamless "optionalization" of fu=
nction arguments. That is, given the folowing function signature:</p>

<pre>void fun(T v) {
  process(v);
}
</pre>

<p>It should be possible to change the signature and the implementation to:=
</p>

<pre>void fun(optional&lt;T&gt; v) {
  if (v) process(*v);
  else   doSthElse();
}
</pre>

<p>and expect that all the places that call function <code>fun</code> are n=
ot affected. But if <code>T</code> happens to be <code>int*</code> and we o=
ccasionally pass value <code>nullptr</code> to it, we will silently change =
the intended behavior of the refactoring: because it will not be the pointe=
r that we null-initialize anymore but a disengaged optional.</p>

<p>Note that this still does not save us from the above problem with refact=
oring function <code>fun</code> in case where <code>T</code> happens to be =
<code>optional&lt;U&gt;</code>, but we definately limit the amount of surpr=
ises.</p>

<p>In order to avoid similar problems with tag <code>nullopt</code>, instan=
tiating template <code>optional</code> with types <code>nullopt_t</code> an=
d <code>emplace_t</code> is prohibitted.</p>

<p>There exist, on the other hand, downsides of introducing a special token=
 in place of <code>nullptr</code>. The number of ways to indicate the 'null=
-state' for different library components will grow: you will have <code>NUL=
L</code>, <code>nullptr</code>, <code>nullopt</code>. New C++ programmers w=
ill ask "which of these should I use now?" What guidelines should be provid=
ed? Use only <code>nullptr</code> for pointers? But does it mean that we sh=
ould use <code>nullopt</code> for <code>std::function</code>? Having only o=
ne way of denoting null-state, would make the things easier, even if "ptr" =
suggests a pointer. </p>


<h3><a name=3D'rationale.t_based_none'>Why not a tag dependent on <code>T</=
code>? </a></h3>


<p>It has been suggested that instead of 'typeless' <code>nullopt</code> a =
tag nested in class <code>optional</code> be used instead:</p>

<pre>optional&lt;int&gt; oi =3D optional&lt;int&gt;::nullopt;
</pre>

<p>This has several advantages. Namespace <code>std</code> is not polluted =
with an additional <code>optional</code>-specific name. Also, it resolves c=
ertain ambiguities when types like <code>optional&lt;<code>optional&lt;T&gt=
;</code>&gt;</code> are involved:</p>

<pre>optional&lt;optional&lt;int&gt;&gt; ooi =3D optional&lt;int&gt;::nullo=
pt;           <em>// engaged</em>
optional&lt;optional&lt;int&gt;&gt; ooj =3D optional&lt;optional&lt;int&gt;=
&gt;::nullopt; <em>// disengaged</em>
</pre>

<pre>void fun(optional&lt;string&gt;);
void fun(optional&lt;int&gt;);

fun(optional&lt;string&gt;::nullopt); <em>// unambiguous: a typeless nullop=
t would not do</em>=20
</pre>

<p>Yet, we choose to propose a typeless tag because we consider the above p=
roblems rare and a typeless tag offers a very short notation in other cases=
:</p>

<pre>optional&lt;string&gt; fun()
{
  optional&lt;int&gt oi =3D nullopt;  <em>// no ambiguity</em>
  oi =3D nullopt;                <em>// no ambiguity</em>
  <em>// ...</em>
  return nullopt;              <em>// no ambiguity</em>
}
</pre>

<p>If the typeless tag does not work for you, you can always use the follow=
ing construct, although at the expense of invoking a (possibly elided) move=
 constructor:</p>=20

<pre>optional&lt;optional&lt;int&gt;&gt; ooi =3D optional&lt;int&gt;{};    =
       <em>// engaged</em>
optional&lt;optional&lt;int&gt;&gt; ooj =3D optional&lt;optional&lt;int&gt;=
&gt;{}; <em>// disengaged</em>
</pre>

<pre>void fun(optional&lt;string&gt;);
void fun(optional&lt;int&gt;);

fun(optional&lt;string&gt;{}); <em>// unambiguous</em>=20
</pre>

<h3><a name=3D'rationale.access'>Accessing the contained value</a></h3>


<p>It was chosen to use indirection operator because, along with explicit c=
onversion to <code>bool</code>, it is a very common pattern for accessing a=
 value that might not be there: </p>
<pre>if (p) use(*p);</pre>

<p>This pattern is used for all sort of pointers (smart or dumb), and it cl=
early indicates the fact that the value may be missing and that we return a=
 reference rather than a value.  The indirection operator has risen some ob=
jections because it may incorrectly imply that <code>optional</code> is a (=
possibly smart) pointer, and thus provides shallow copy and comparison sema=
ntics. All library components so far use indirection operator to return an =
object that is not part of the pointer's/iterator's value. In contrast, <co=
de>optional</code> indirects to the part of its own state. We do not consid=
er it a problem in the design; it is more like an unprecedented usage of in=
direction operator.  We believe that the cost of potential confusion is ove=
rweighed by the benefit of an easy to grasp and intuitive interface for acc=
essing the contained value. </p>

<p>We do not think that providing an implicit conversion to <code>T</code> =
would be a good choice. First, it would require different way of checking f=
or the empty state; and second, such implicit conversion is not perfect and=
 still requires other means of accessing the contained value if we want to =
call a member function on it.</p>

<p>Using the indirection operator for a disengaged object is an undefined b=
ehavior. This behavior offers maximum runtime performance. In addition to i=
ndirection operator, we provide member function <code>value</code> that ret=
urns a reference to to the contained value if one exists or throws an excep=
tion (derived from <code>logic_error</code>) otherwise:</p>

<pre>void interact()
{
  std::string s;
  cout &lt;&lt; "enter number ";
  cin &gt;&gt; s;
  optional&lt;int&gt; oi =3D str2int(s);
 =20
  try {
    process_int(oi.value());
  }
  catch(bad_optional_access const&amp;) {
    cout &lt;&lt; "this was not a number";
  }
}</pre>



<h3><a name=3D'rationale.relops'>Relational operators</a></h3>


<p>One of the design goals of <code>optional</code> is that objects of type=
 <code>optional&lt;T&gt;</code> should be valid elements in STL containers =
 and usable with STL algorithms (at least if objects of type <code>T</code>=
 are). Equality comparison is essential for <code>optional&lt;T&gt;</code> =
to model concept <code>Regular</code>. C++ does not have concepts, but bein=
g regular is still essential for the type to be effectively used with STL. =
Ordering is essential if we want to store optional values in ordered associ=
ative containers. A number of ways of including the disengaged state in com=
parisons have been suggested. The ones proposed, have been crafted such tha=
t the axioms of equivalence and strict weak ordering are preserved: disenga=
ged <code>optional&lt;T&gt;</code> is simply treated as an additional and u=
nique value of <code>T</code> equal only to itself; this value is always co=
mpared as less than any value of <code>T</code>:</p>

<pre>optional&lt;unsigned&gt; o0{0};
optional&lt;unsigned&gt; o1{1};
optional&lt;unsigned&gt; oN{nullopt};

assert (oN &lt; o0);
assert (o0 &lt; o1);
assert (!(oN  &lt; oN));
assert (!(o1 &lt; o1));

assert (oN !=3D o0);
assert (o0 !=3D o1);
assert (oN =3D=3D oN);
assert (o0 =3D=3D o0);
</pre>

<p>Given that both <code>nullopt_t</code> and <code>T</code> are implicitly=
 convertible to <code>optional&lt;T&gt;</code>, this implies the existence =
and semantics of mixed comparison between <code>optional&lt;T&gt;</code> an=
d <code>T</code>, as well as between <code>optional&lt;T&gt;</code> and <co=
de>nullopt_t</code>:</p>

<pre>assert (oN =3D=3D nullopt);
assert (o0 !=3D nullopt);
assert (oN !=3D 1);
assert (o1 =3D=3D 1);

assert (oN &lt; 1);
assert (o0 &gt; nullopt);
</pre>

<p>Although it is difficult to imagine any practical use case of ordering r=
elation between <code>optional&lt;T&gt;</code> and <code>nullopt_t</code>, =
we still provide it for completness's sake</p>

<p>The mixed relational operators, especially these representing order, bet=
ween <code>optional&lt;T&gt;</code> and <code>T</code> have been accused of=
 being dangerous. In code examples like the following, it may be unclear if=
 the author did not really intend to compare two <code>T</code>'s. </p>

<pre>auto count =3D get_optional_count();
if (count &lt; 20) {}                        <em>// or did you mean: *count=
 &lt; 20 ?</em>
if (count =3D=3D nullopt || *count &lt; 20) {}   <em>// verbose, but unambi=
guous</em>
</pre>

<p>Given that <code>optional&lt;T&gt;</code> is comparable and implicitly c=
onstructible from <code>T</code>, the mixed comparison is there already. We=
 would have to artificially create the mixed overloads only for them to cau=
se controlled compilation errors. A consistent approach to prohibiting mixe=
d relational operators would be to also prohibit the convesion from <code>T=
</code> or to also prohibit homogenous relational operators for <code>optio=
nal&lt;T&gt;</code>; we do not want to do either, for other reasons discuss=
ed in this proposal. Also, mixed relational operations are available in Boo=
st.Optional and were found useful by the users. Mixed operators come as som=
ething natural when we consider the model "<code>T</code> with one addition=
al value".</p>

<p>For completeness sake, we also provide ordering relations between <code>=
optional&lt;T&gt;</code> and <code>nullopt_t</code>, even though we see no =
practical use case for them:</p>

<pre>bool test(optional&lt;int&gt; o)
{
  assert (o &gt;=3D nullopt);    <em>// regardless of o's state</em>
  assert (!(o &lt; nullopt));  <em>// regardless of o's state</em>
  assert (nullopt &lt;=3D o);    <em>// regardless of o's state</em>=20
  return (o &gt; nullopt);     <em>// o !=3D nullopt is cleaner</em>
}</pre>

<p>This is similar to comparing values of type <code>unsigned int</code> wi=
th 0:</p>

<pre>bool test(unsigned int i)
{
  assert (i &gt;=3D 0);          <em>// regardless of i's state</em>
  assert (0 &lt;=3D i);          <em>// regardless of i's state</em>
  return (i &gt; 0);           <em>// i !=3D 0 is cleaner</em>
}</pre>


<p>There exist two ways of implementing <code>operator&gt;</code> for optio=
nal objects:</p>

<pre>bool operator&gt;(const optional&lt;T&gt;&amp; x, const optional&lt;T&=
gt;&amp; y)
{
  return (!x) ? false : (!y) ? true : *x &gt; *y;  <em>// use T::operator&g=
t;</em>
}

bool operator&gt;(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&a=
mp; y)
{
  return y &lt; x;                                 <em>// use optional&lt;T=
&gt;::operator&lt;</em>
}
</pre>

<p>In case <code>T::operator&gt;</code> and <code>T::operator&lt;</code> ar=
e defined consistently, both above implementations are equivalent. If the t=
wo operators are not consistent, the  choice of implementation makes a diff=
erence. For homogenous relational operations (between two <code>optional&lt=
;T&gt;</code>s), we chose the former specification. That is, <code>T::opera=
tor&gt;</code> may not even be defined if order for <code>optional&lt;T&gt;=
::operator&lt;</code> to work. This is consistent with a similar choice for=
 <code>std::tuple</code>. For heterogenous relational operations (between <=
code>optional&lt;T&gt;</code> and <code>T</code>), we choose the latter spe=
cification. There is no precedent for mixed relops in the Standard Library,=
 so we do not feel we are making it wrong. The latter specification is more=
 close to the model "<code>optional&lt;T&gt;</code> is like <code>T</code>.=
" For homogenous rel-ops we want to stick to the rules employed by the Stan=
dard.</p>



<h3><a name=3D'rationale.resetting'>Resetting the optional value</a></h3>


<p>Assigning the value of type <code>T</code> to <code>optional&lt;T&gt;</c=
ode> object results in doing two different things based on whether the opti=
onal object is engaged or not. If optional object is engaged, the contained=
 value is assigned a new value. If optional object is disengaged, it become=
s engaged using <code>T</code>'s copy/move constructor. This behavior is ba=
sed on a silent assumption that <code>T</code>'s copy/move constructor is c=
opying a value in a similar way to copy/move assignment. A similar logic ap=
plies to <code>optional&lt;T&gt;</code>'s copy/move assignment, although th=
e situation here is more complicated because we have two engaged/disengaged=
 states to be considered. This means that <code>optional&lt;T&gt;</code>'s =
assignment does not work (does not compile) if <code>T</code> is not assign=
able:</p>

<pre>optional&lt;const int&gt; oi =3D 1;  <em>// ok</em>
oi =3D 2;                      <em>// error</em>=20
oi =3D oi;                     <em>// error</em>=20
oi =3D nullopt;                <em>// ok</em>
</pre>

<p>There is an option to reset the value of optional object without resorti=
ng to <code>T</code>'s assignment:</p>

<pre>optional&lt;const int&gt; oj =3D 1;  <em>// ok</em>
oj.emplace(2);               <em>// ok</em>=20
</pre>

<p>Function <code>emplace</code> disengages the optional object if it is en=
gaged, and then just engages the object anew by copy-constructing the conta=
ined value. It is similar to assignment, except that it is guaranteed not t=
o use <code>T</code>'s assignment and provides only a basic exception saget=
y guarantee. In contrast, assignment may provide a stronger guarantee if <c=
ode>T</code>'s assignment does.</p>

<p>To sumarize, this proposal offers three ways of assigning a new containe=
d value to an optional object:</p>

<pre>optional&lt;int&gt; o;
o =3D make_optional(1);         <em>// copy/move assignment</em>
o =3D 1;                        <em>// assignment from T</em>
o.emplace(1);                 <em>// emplacement</em>=20
</pre>

<p>The first form of assignment is required to make <code>optional</code> a=
 regular object, useable in STL. We need the second form in order to reflec=
t the fact that <code>optional&lt;T&gt;</code> is a wrapper for <code>T</co=
de> and hence it should behave as <code>T</code> as much as possible. Also,=
 when <code>optional&lt;T&gt;</code> is viewed as <code>T</code> with one a=
dditional value, we want the values of <code>T</code> to be directly assign=
able to <code>optional&lt;T&gt;</code>. In addition, we need the second for=
m to allow the interoperability with function <code>std::tie</code> as show=
n above. The third option is required to be able to reset an optional non-a=
ssignable <code>T</code>.</p>

=20
<h3><a name=3D'rationale.emplace'>Tag <code>emplace</code></a></h3>

<p>This proposal provides an 'in-place' constructor that forwards (perfectl=
y) the arguments provided to <code>optional</code>'s constructor into the c=
onstructor of <code>T</code>. In order to trigger this constructor one has =
to use the tag struct <code>emplace</code>. We need the extra tag to disamb=
iguate certain situations, like calling <code>optional</code>'s default con=
structor and requesting <code>T</code>'s default construction:</p>

<pre>optional&lt;Big&gt; ob{emplace, "1"}; <em>// calls Big{"1"} in place (=
no moving)</em>
optional&lt;Big&gt; oc{emplace};      <em>// calls Big{} in place (no movin=
g)</em>
optional&lt;Big&gt; od{};             <em>// creates a disengaged optional<=
/em>
</pre>

<p>The name, suggested by Alberto Ganesh Barbati, is consistent with member=
 functions of containers (and <code>optional</code> itself), which also ind=
icate similar purpose. On the other hand, it may appear uncomfortable that =
<code>emplace</code> becomes overloaded in <code>std</code>: it is now a me=
mber function in many container type as well as a tag. If this is considere=
d a serious issue, the tag could be renamed to <code>in_place</code>.</p>


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

<p>Class template <code>optional</code> imposes little requirements on <cod=
e>T</code>: it has to be either an lvalue reference type, or a complete obj=
ect type satisfying the requirements of <code>Destructible</code>. It is th=
e particular operations on <code>optional&lt;T&gt;</code> that impose requi=
rements on <code>T</code>: <code>optional&lt;T&gt;</code>'s move constructo=
r requires that <code>T</code> is <code>MoveConstructible</code>, <code>opt=
ional&lt;T&gt;</code>'s copy constructor requires that <code>T</code> is <c=
ode>CopyConstructible</code>, and so on. This is because <code>optional&lt;=
T&gt;</code> is a wrapper for <code>T</code>: it should resemble <code>T</c=
ode> as much as possible. If <code>T</code> is <code>EqualityComparable</co=
de> then (and only then) we expect <code>optional&lt;T&gt;</code> to be <co=
de>EqualityComparable</code>. </p>



<h3><a name=3D'rationale.refs'>Optional references</a></h3>


<p>In this revision, optional references are presented as an auxiliary prop=
osal. The intention is that the Committee should have an option to accept o=
ptional values without optional references, if it finds the latter concept =
inacceptable. Users that in generic contexts require to also store optional=
 lvalue references can achieve this effect, even without direct support for=
 optional references, with a bit of meta-programming. </p>

<pre>
template &lt;class T&gt;
struct generic
{
  typedef T type;
};

template &lt;class U&gt;
struct generic&lt;U&amp;&gt;
{
  typedef std::reference_wrapper&lt;U&gt; type;
};

template &lt;class T&gt;
using Generic =3D typename generic&lt;T&gt;::type;

template &lt;class X&gt;
void generic_fun()
{
  std::optional&lt;Generic&lt;X&gt;&gt; op;
  <em>// ...</em>
}
</pre>

<p>Although the behavior of such "emulated" optional references will be sli=
ghtly different than that of "normal" optional references.</p>=20



<h3><a name=3D'rationale.noexcept'>Exception specifications</a></h3>


<p>First draft of this revision required an aggressive usage of conditional=
 <code>noexcept</code> specifications for nearly every, member- or non-memb=
er-, function in the interface. For instance equality comparison was to be =
declared as:</p>

<pre>template &lt;class T&gt;
  bool operator=3D=3D(const optional&lt;T&gt;&amp; lhs, const optional&lt;T=
&gt;&amp rhs)
  noexcept(noexcept(*lhs =3D=3D *rhs));
</pre>

<p>This was based on one of our goals: that we want <code>optional&lt;T&gt;=
</code> to be applicable wherever <code>T</code> is applicable in as many s=
ituations as reasonably possible. One such situation occurs where no-throw =
operations of objects of type <code>T</code> are used to implement a strong=
 exception safety guarantee of some operations. We would like objects of ty=
pe <code>optional&lt;T&gt;</code> to be also useable in such cases. However=
, we do not propose this aggressive conditional no-throw guarantees at this=
 time in order for the proposed library component to adhere to the current =
Library guidelines for conditional <code>noexcept</code>: it is currently o=
nly used in move constructor, move assignment and <code>swap</code>. One ex=
ception to this rule, we think could be made for optional's move constructo=
r and assignment from type <code>T&amp;&amp;</code>, however we still do no=
t propose this at this time in order to avoid controversy.</p>

<p>Constructors and mutating functions that disengage an optional object ar=
e required to be <code>noexcept(true)</code>: they only call <code>T</code>=
's destructor and impose no precondition on optional object's or contained =
value's state. The same applies to the observers that check the disengaged/=
engaged state.</p>

<p>The observers that access the contained value &mdash; <code>operator*</c=
ode> and <code>operator-&gt;</code> &mdash; are not declared as <code>noexc=
ept(true)</code> even though they have no good reason to throw. This is bec=
ause they impose a precondition that optional object shall be engaged, and =
as per observations from N3248<sup>[6]</sup>, library vendors may need to u=
se exceptions to test if the implementation has all the necessary precondit=
ion-checking code inside. These observer functions are still required not t=
o throw exceptions.</p>

<p>In general, operations on optional objects only throw, when operations d=
elegated to the contained value throw.</p>



<h3><a name=3D'rationale.constexpr'>Making <code>optional</code> a literal =
type</a></h3>


<p>We propose that <code>optional&lt;T&gt;</code> be a literal type for tri=
vially destructible <code>T</code>'s.</p>

<pre>constexpr optional&lt;int&gt; oi{5};
static_assert(oi, "");            <em>// ok</em>
static_assert(oi !=3D nullopt, ""); <em>// ok</em>
static_assert(oi =3D=3D oi, "");      <em>// ok</em>
int array[*oi];                   <em>// ok: array of size 5 </em>
</pre>

<p>Making <code>optional&lt;T&gt;</code> a literal-type in general is impos=
sible: the destructor cannot be trivial because it has to execute an operat=
ion that can be conceptually described as:</p>

<pre>
~optional() {
  if (is_engaged()) destroy_contained_value();
}
</pre>

<p>It is still possible to make the destructor trivial for <code>T</code>'s=
 which provide a trivial destructor themselves, and we know an efficient im=
plementation of such <code>optional&lt;T&gt;</code> with compile-time inter=
face &mdash; except for copy constructor and move constructor &mdash; is po=
ssible. Therefore we propose that for trivially destructible <code>T</code>=
's all <code>optional&lt;T&gt;</code>'s constructors, except for move and c=
opy constructors, as well as observer functions are <code>constexpr</code>.=
 The sketch of reference implementation is provided in this proposal.</p>=
=20



<h3><a name=3D'rationale.moved_from'>Moved-from state</a></h3>

<p>When a disengaged optional object is moved from (i.e., when it is the so=
urce object of move constructor or move assignment) its state does not chan=
ge. When an engaged object is moved from, we move the contained value, but =
leave the optional object engaged. A moved-from contained value is still va=
lid (although possibly not specified), so it is fine to consider such optio=
nal object engaged. An alternative approach would be to destroy the contain=
ed value and make the moved-from optional object disengaged. However, we do=
 not propose this for performance reasons.</p>

<p>In contexts, like returning by value, where you need to call the destruc=
tor the second after the move, it does not matter, but in cases where you r=
equest the move explicitly and intend to assign a new value in the next ste=
p, and if <code>T</code> does not provide an efficient move, the chosen app=
roach saves an unnecessary destructor and constructor call:</p>

<pre>optional&lt;array&lt;Big, 1000&gt;&gt; oo =3D ... <em>// array doesn't=
 have efficient move</em>
op =3D std::move(oo);
oo =3D std::move(tmp);
</pre>

<p>The following is even more compelling reason. In this proposal <code>std=
::optional&lt;int&gt;</code> is allowed to be implemented as a <code>Trivia=
llyCopyable</code> type. Therefore, the copy constructor of type <code>std:=
:array&lt;std::optional&lt;int&gt;, 1000&gt;</code> can be implemented usin=
g <code>memcpy</code>. With the additional requirement that <code>optional<=
/code>'s move constructor should not be trivial, we would be preventing the=
 described optimization. </p>

<p>The fact that the moved-from optional is not disengaged may look "uncomf=
ortable" at first, but this is an invalid expectation. The requirements of =
library components expressed in 17.6.5.15 (moved-from state of library type=
s) only require that moved-from objects are in a valid but unspecified stat=
e. We do not need to guarantee anything above this minimum.</p>



<h3><a name=3D'rationale.io'>IO operations</a></h3>


<p>The proposed interface for optional values does not contain IO operation=
s: <code>operator&lt;&lt;</code> and <code>operator&gt;&gt;</code>. While w=
e believe that they would be a useful addition to the interface of optional=
 objects, we also observe that there are some technical obstacles in provid=
ing them, and we choose not to propose them at this time.</p>

<p>One can imagine a couple of ways in which IO-operations for any streamab=
le type <code>T</code> could be expected to work. The differences are mostl=
y the consequence of different conceptual models of optional types, as well=
 as different use cases that programmers may face. Below we list the possib=
le ways of outputting the value of optional object.</p>

<ol>
<li>Output the contained value if engaged; otherwise enter an undefined beh=
aviour (as programmer error).</li>
<li>Output the contained value if engaged; otherwise output nothing.</li>
<li>Output the contained value if engaged; otherwise output some special se=
quence of characters.</li>
<li>Output something like <code>"OPT[<var>v</var>]"</code>, where <code><va=
r>v</var></code> represents the contained value, if engaged; otherwise outp=
ut something like <code>"OPT[]"</code>.</li>
</ol>

<p>The first option is a consequence of the model where <code>optional&lt;T=
&gt;</code> is a <code>T</code> with deferred initialization, but which is =
still initialized before first usage. This is not the model that we advocat=
e, so this behavior of <code>operator&lt;&lt;</code> is rejected. However t=
his behavior can be achieved by accessing the contained value of optional o=
bject on each usage:</p>

<pre>optional&lt;int&gt; oi;
initialize(oi);
cin &gt;&gt; (*oi);
cout &lt;&lt; (*oi);
</pre>


<p>The second option appears useful in certain contexts, where we want opti=
onal objects to indicate some supplementary, often missing, information:</p=
>

<pre>struct FlatNumber {
  unsigned number_;
  optional&lt;char&gt; letter_;
};

ostream&amp; operator&lt;&lt;( ostream&amp; out, FlatNumber n ) {
  return out &lt;&lt; n.number_ &lt;&lt; n.letter_;
}

<em>// outputs "10", "11", "11A", ...</em>=20
</pre>


<p>However, in general the results would be ambiguous. Does output <code>"1=
&nbsp;0"</code> indicate two engaged <code>optional&lt;int&gt;</code>s, or =
three, one of which (which one?) is disengaged, or 77 <code>optional&lt;int=
&gt;</code>s? Or are these perhaps two <code>int</code>s? Also, It is not p=
ossible to implement a consistent <code>operator&gt;&gt;</code> in this cas=
e. It may not be a problem itself, and providing only one operator is not a=
 precedent in the standard (consider <code>std::thread::id</code>); alterna=
tively, <code>operator&gt;&gt;</code> could be implemented inconsistently: =
by simply calling <code>T</code>'s <code>operator&gt;&gt;</code>. =20
</p>  =20

<p>The third choice appears attractive at first glance, but there is no goo=
d representation for the special sequence that would produce no ambiguities=
.. Whatever sequence we choose, it is also a valid representtion of <code>st=
d::string</code>; thus if we need to interpret the special sequence, say <c=
ode>"~~~"</code> as <code>optional&lt;string&gt;</code>, we do not know if =
it is a disengaged object, or engaged one with contained value of <code>"~~=
~"</code>. On the other hand, some people have argued that this ambiguity i=
s worth the usefulness of a simple tool for logging.
</p>

<p>While the fourth choice presented above still comes with some similar am=
biguities, it is posssible to implement a variant thereof that is not ambig=
uous. Such solution has been implemented in Boost.Tuple library<sup>[5]</su=
p>: user has to register a sequence of letters that represent "an opening b=
racket" of the optional object's contained value, and similarly register an=
 another sequence for representing a "closing bracket." This would be the u=
ser's responsibility to make sure that the chosen sequences are unambiguous=
, if default sequences (e.g., <code>"["</code> and <code>"]"</code>) do not=
 suffice. However, this solution is not without certain controversy.</p>

<p>Currently all streamable types in the library have a nice property that =
string representation that is streamed out or read in is similar to the for=
mat of literals in C++ used to initialize variables. Thus, whatever you typ=
e into the console that you intend your program to read, could be equally w=
ell typed directly in the C++ code as a literal &mdash; of course, to certa=
in extent. The text that the program requires of users to read and type is =
simply nice.</p>

<p>This controversy is characteristic not only of <code>optional</code>. Li=
brary components like containers, pairs, tuples face the same issue. At pre=
sent IO operations are not provided for these types. Our preference for <co=
de>optional</code> is to provide an IO solution compatible with this for co=
ntainers, pairs and tuples, therefore at this point we refrain from proposi=
ng a solution for <code>optional</code> alone.</p>



<h3>Type requirements <code>NullableProxy</code></h3>

<p>As already mentioned, the primary purpose of optional object is to check=
 if they contain a value and if so, to provide access to this value. We obs=
erve that a similar functionalit is offered by raw and smart pointers, exce=
pt for the "contains" part: pointers do not contain the value they point to=
.. Nonetheless, optional objects and pointers have enough things in common t=
hat certain class of generic functions can be written that can be used with=
 either. We call the identified concept <code>NullableProxy</code>. Basical=
ly, the concept indicates that a type is a 'proxy' for another type. The op=
erations allowed are: checking if there exists an object that our proxy can=
 indirect us to and the indirection operation. The operations can be summar=
ized by the following use-case:</p>

<pre>
temmplate &lt;class NullableProxy&gt;
void test(NullableProxy&amp;&amp; np)
{
  if (np)              <em>// 'has-object' check</em>
    auto&amp;&amp; obj =3D *np;  <em>// object access</em>
  if (!np) {}          <em>// 'doesn't have object'</em>
}
</pre>

<p>These requirements are sufficient to specify a couple of generic functio=
ns (not proposed), like the one below: </p>

<pre>template &lt;typename NullableProxy&gt;
<em>// enable_if: decltype(*declval&lt;NullableProxy&gt;()) is EqualityComp=
arable</em>
bool equal_pointees( const NullableProxy&amp; x, const NullableProxy&amp; y=
 )
{
  return bool(x) !=3D bool(y) ? false : ( x ? *x =3D=3D *y : true );
}
</pre>

<p>This is exactly the logic for the equality comparison of optional values=
, and could be used as an implementation of <code>optional&lt;T&gt;::operat=
or=3D=3D</code>. A similar algorithm for less-than comparison can be specif=
ied. The third example is function <code>value_or</code> discussed below. A=
nother example is function <code>as_ptr</code> discussed below, for providi=
g raw pointer access to a possibly-null proxied value. </p>

<p>Requirements <code>NullableProxy</code> overlap with requirements <code>=
NullablePointer</code>. Their common part could be extracted to separate re=
quirements, say <code>Nullable</code>, but these requirements are to small =
to be useful alone for anything.</p>

<p>We do not propose to add <code>NullableProxy</code> to Library at this t=
ime, as the usage base may be to small for justifying the change. It may pr=
ove a useful addition in the future.</p>



<h3><a name=3D'rationale.value_or'>Function <code>value_or</code></a></h3>


<p>This function template returns a value stored by the <code>optional</cod=
e> object if it is engaged, and if not, it falls back to the default value =
specified in the second argument. It used to be called <code>get_value_or</=
code> in the previous revisions, but we decided to rename it, as a conseque=
nce of disscussions, so that it is similar to another new member function <=
code>value</code>. This method for specifying default values on the fly rat=
her than tying the default values to the type is based on the observation t=
hat different contexts or usages require different default values for the s=
ame type. For instance the default value for <code>int</code> can be 0 or -=
1. The callee might not know what value the caller considers special, so it=
 returns the lack of the requested value explicitly. The caller may be bett=
er suited to make the choice what special value to use.</p>

<pre>
optional&lt;int&gt; queryDB(std::string);
void setPieceCount(int);
void setMaxCount(int);

setPieceCount( queryDB("select piece_count from ...").value_or(0) );
setMaxCount( queryDB("select max_count from ...").value_or(numeric_limits&l=
t;int&gt;::max()) );
</pre>

<p>The decision to provide this function is controversial itself. As pointe=
d out by Robert Ramey, the goal of the <code>optional</code> is to make the=
 lack of the value explicit. Its syntax forces two control paths; therefore=
 we will typically see an <code>if</code>-statement (or similar branching i=
nstruction) wherever <code>optional</code> is used. This is considered an i=
mprovement in correctness. On the other hand, using the default value appea=
rs to conflict with the above idea. One other argument against providing it=
 is that in many cases you can use a ternary conditional operator instead:<=
/p>

<pre>auto&amp;&amp; cnt =3D queryDB("select piece_count from ...");
setPieceCount(cnt ? *cnt : 0);

auto&amp;&amp; max =3D queryDB("select max_count from ...");
setMaxCount(max ? std::move(*max) : numeric_limits&lt;int&gt;::max());
</pre>

<p>However, in case optional objects are returned by value and immediately =
consumed, the ternary operator syntax requires introducing an lvalue. This =
requires more typing and explicit <code>move</code>. This in turn makes the=
 code less safe because a moved-from lvalue is still accessible and open fo=
r inadvertent misuse.</p>

<p>There are reasons to make it a free-standing function. (1) It can be imp=
lemented by using only the public interface of <code>optional</code>. (2) T=
his function template could be equally well be applied to any type satisfyi=
ng the requirements of <code>NullableProxy</code>. In this proposal, functi=
on <code>value_or</code> is defined as a member function and only for <code=
>optional</code>s. Making a premature generalization would risk standardizi=
ng a function with suboptimal performance/utility. While we know what detai=
led semantics (e.g., the return type) <code>value_or</code> should have for=
 <code>optional</code>, we cannot claim to know the ideal semantics for any=
 <code>NullableProxy</code>. Also, it is not clear to us if this convenienc=
e function is equally useful for pointers, as it is for optional objects. B=
y making <code>value_or</code> a member function we leave the room for this=
 name in namespace <code>std</code> for a possible future generalization.
</p>

<p>The second argument in the function template's signature is not <code>T<=
/code> but any type convertible to <code>T</code>:</p>

<pre>
template &lt;class T, class V&gt;=20
  typename decay&lt;T&gt;::type optional&lt;T&gt;::value_or(V&amp;&amp; <va=
r>v</var>) const&amp;;
template &lt;class T, class V&gt;=20
  typename decay&lt;T&gt;::type optional&lt;T&gt;::value_or(V&amp;&amp; <va=
r>v</var>) &amp;&amp;;
</pre>

<p>This allows for a certain run-time optimization. In the following exampl=
e:</p>

<pre>optional&lt;string&gt; op{"cat"};
string ans =3D op.value_or("dog");
</pre>

<p>Because the optional object is engaged, we do not need the fallback valu=
e and therefore to convert the string literal <code>"dog"</code> into type =
<code>string</code>.</p>

<p>It has been argued that the function should return by constant reference=
 rather than value, which would avoid copy overhead in certain situations:<=
/p>

<pre>void observe(const X&amp; x);

optional&lt;X&gt; ox { <em>/* ... */</em> };
observe( ox.value_or(X{args}) );    <em>// unnecessary copy</em>
</pre>

<p>However, the benefit of the function <code>value_or</code> is only visib=
le when the optional object is provided as a temporary (without the name); =
otherwise, a ternary operator is equally useful:</p>

<pre>optional&lt;X&gt; ox { <em>/* ... */</em> };
observe(ox ? *ok : X{args});            <em>// no copy</em>
</pre>

<p>Also, returning by reference would be likely to render a dangling refere=
nce, in case the optional object is disengaged, because the second argument=
 is typically a temporary:</p>

<pre>
optional&lt;X&gt; ox {nullopt};
auto&amp;&amp; x =3D ox.value_or(X{args});
cout &lt;&lt; x;                             <em> // x is dangling!</em>
</pre>

<p>There is also one practical problem with returning a reference. The func=
tion takes two arguments by reference: the optional object and the default =
value. It can happen that one is deduced as lvalue reference and the other =
as rvalue reference. In such case we would not know what kind of reference =
to return. Returning lvalue reference might prevent move optimization; retu=
rning an rvalue reference might cause an unsafe move from lvalue. By return=
ing by value we avoid these problems by requiring one unnecessary move in s=
ome cases.</p>

<p>We also do not want to return a constant lvalue reference because that w=
ould prevent a copy elision in cases where optional object is returned by v=
alue.</p>

<p>It has also been suggested (by Luc Danton) that function <code>optional&=
lt;T&gt;::value_or&lt;V&gt;</code> should return type <code>decay&lt;common=
_type&lt;T, V&gt;::type&gt;::type</code> rather than <code>decay&lt;T&gt;::=
type</code>. This would avoid certain problems, such as loss of accuracy on=
 arithmetic types:</p>

<pre><em>// not proposed</em>
std::optional&lt;int&gt; op =3D <em>/* ... */</em>;
long gl =3D <em>/* ... */</em>;

auto lossless =3D op.value_or(gl);   <em>// lossless deduced as long rather=
 than int</em>
</pre>

<p>However, we did not find many practical use cases for this extension, so=
 we do not propose is at this time.</p>

<p>Together with function <code>value</code>, <code>value_or</code> makes a=
 set of similarly called functions for accessing the contained value that d=
o not cause an undefined behavior when invoked on a disengaged optional (at=
 the expense of runtime overhead). They differ though, in the return type: =
one returns a value, the other a reference.</p>

<p>One other similar convenience function has been suggested. Sometimes the=
 default value is not given, and computing it takes some time. We only want=
 to compute it, when we know the optional object is disengaged:</p>

<pre>optional&lt;int&gt; oi =3D <em>/* ... */</em>;

if (oi) {
  use(*oi);
}
else {
  int i =3D painfully_compute_default();
  use(i);
}
</pre>

<p>The solution to that situation would be another convenience function whi=
ch rather taking a default value takes a callable object that is capable of=
 computing a default value if needed:</p>

<pre>
use( oi.value_or_call(&amp;painfully_compute_default) );      =20
<em>// or</em>
use( oi.value_or_call([&amp;]{return painfully_compute_default();} );
</pre>

<p>We do not propose this, as we prefer to standardize the existing practic=
e. Also, it is not clear how often the above situations may occur, and the =
tool prove useful. </p>



<h3><a name=3D'rationale.make_optional'>Function <code>make_optional</code>=
</a></h3>


<p>We also propose a helper function <code>make_optional</code>. Its semant=
ics is closer to that of <code>make_pair</code> or <code>make_tuple</code> =
than that of <code>make_shared</code>. You can use it in order for the type=
 of the optional to be deduced:</p>

<pre>int i =3D 1;
auto oi =3D make_optional(i);          <em>// decltype(oi) =3D=3D optional&=
lt;int&gt;</em>
</pre>

<p>This may occasionally be useful when you need to pick the right overload=
 and not type the type of the optional by hand: </p>

<pre>void fun(optional&lt;complex&lt;double&gt;&gt;);
void fun(Current);                   <em>// complex is convertible to Curre=
nt</em>

complex&lt;double&gt; c{0.0, 0.1};
fun(c);                              <em>// ambiguous</em>
fun({c});                            <em>// ambiguous</em>
fun(make_optional(c));               <em>// picks first overload</em>
</pre>

<p>This is not very useful in return statements, as long as the converting =
constructor from <code>T</code> is implicit, because you can always use the=
 brace syntax:</p>

<pre>optional&lt;complex&lt;double&gt;&gt; findC()
{
  complex&lt;double&gt; c{0.0, 0.1};
  return {c};
}
</pre>

<p><code>make_shared</code>-like function does not appear to be useful at a=
ll: it is no different than manually creating a temporary optional object:<=
/p>

<pre><em>// not proposed</em>
fun( make_optional&lt;Rational&gt;(1, 2) );
fun( optional&lt;Rational&gt;{1, 2} );     <em>// same as above</em>
</pre>

<p>It would also not be a good alternative for tagged placement constructor=
, because using it would require type <code>T</code> to be <code>MoveConstr=
uctible</code>:</p>

<pre><em>// not proposed</em>
auto og =3D make_optional&lt;Guard&gt;("arg1"); <em>// ERROR: Guard is not =
MoveConstructible</em>
</pre>

<p>Such solution works for <code>shared_ptr</code> only because its copy co=
nstructor is shallow. One useful variant of <code>shared_ptr</code>-like <c=
ode>make_optional</code> would be a function that either creates an engaged=
 or a disengaged optional based on some boolean condition:</p>

<pre><em>// not proposed</em>
return make_optional_if&lt;Rational&gt;(good(i) &amp;&amp; not_bad(j), i, j=
);

<em>// same as:</em>
if (good(i) &amp;&amp; not_bad(j)) {
  return {i, j};
}
else {
  return nullopt;
}

<em>// same as:</em>
optional&lt;Rational&gt; or =3D nullopt;
if (good(i) &amp;&amp; not_bad(j)) or.emplace(i, j);
return or; <em>// move-construct on return</em>
</pre>

<p>Since this use case is rare, and the function call not that elegant, and=
 a two-liner alternative exists, we do not propose it.</p>



<h3><a name=3D'rationale.as_ptr'>Raw pointer interface</a></h3>


<p>It has been suggested by a couple of people that <code>optional</code> a=
lso provides a function that returns a raw pointer: <code>nullptr</code> if=
 optional is engaged, otherwise a pointer to the contained value. This is s=
imilar to member function <code>get</code> in smart pointers and function <=
code>target</code> in <code>std::function</code>:</p>

<pre>auto optional&lt;T&gt;::as_ptr() -&gt; value_type*
{
  return bool(*this) ? addressof(**this) : nullptr;
}</pre>

<p>It might be convenient when trying to use <code>optional</code> with old=
er libraries that used raw pointers to represent optional values. It was ob=
served, however, that different library components start to diverge from on=
e another in terms of the interface: smart pointers have <code>get</code>, =
<code>function</code> has <code>target</code> it is not clear which name sh=
ould <code>optional</code> use. The semantics of either are different than =
these of <code>optional&lt;T&gt;::as_ptr</code>. Smart pointers store a raw=
 pointer that can be returned. In <code>optional</code> it has to be built.=
 Function <code>target</code> requires specifying a pointer type. It was al=
so suggested that a generic function applicable uniformly to all smart poin=
ters and <code>optional</code> would be a better choice. However, when trea=
ting pointers and <code>optional</code> uniformly, the situation becomes tr=
icky because unlike all pointers, <code>optional</code>  <em>contains</em> =
the object it indirects to and needs to propagate constness when providing =
access to its contained value. </p>


<pre>template &lt;typename NullableProxy&gt;
auto as_ptr( NullableProxy&amp;&amp; x ) -> decltype(addressof(*std::forwar=
d&lt;NullableProxy&gt;(x)))
{
  return bool(x) ? addressof(*std::forward&lt;NullableProxy&gt;(x)) : nullp=
tr;
}
</pre>

<p>The convoluted type may be too tricky for a Library function, and a type=
 traits for <code>NullableProxy</code> may be needed. This appears to be to=
o much changes compared with the limited functionality the function would o=
ffer. We chose not to propose it. Programmers who need it for <code>optiona=
l</code> can easily implement it as indicated above. If the feature turns o=
ut to be necessary, it can be added in the future in a backwards-compatible=
 manner.</p>


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

<p>At some point the following goal was considered for <code>optional</code=
>; it is the property that could informally be called "copy initialization =
forwarding". It is somewhat similar to the one-argument version of perfect =
forwarding constructor; i.e., if a given initializer can be used to copy-in=
itialize objects of type <code>T</code>, it should also be possible to to u=
se it to copy-initialize objects of type <code>optional&lt;T&gt;</code> wit=
h the same samantics as initializing object of type <code>T</code>. This go=
al cannot be achieved in 100% without severely compromising other design go=
als. For instance, we cannot guarantee the following: </p>

<pre>T x =3D {};             <em>// "{}" is the initializer; x is value-ini=
tialized</em>
optional&lt;T&gt; ox =3D {};  <em>// same initializer; contained value not =
initialized</em>

assert (x =3D=3D *ox);    <em>// not guaranteed!</em>
</pre>

<p>Apart from this default initialization case, and a couple of others (con=
cerning initializer-list), "copy initialization forwarding" could be provid=
ed for <code>optional</code>.</p>

<p>Since <code>optional&lt;T&gt;</code> can be thought of as an "almost <co=
de>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>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 tur=
n 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 =3D ptr;           <em>// ILLEGAL</em>=20
}

void implicit_conv( int * ptr ) {
  optional&lt;unique_ptr&lt;int&gt;&gt; v =3D 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 constru=
ctor. We could achieve this by specifying two constructor templates with id=
entical 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;=20
  <em>// enable_if: Constructible&lt;T, U&amp;&amp;&gt; &amp;&amp; !Convert=
ible&lt;U&amp;&amp;, T&gt;</em>
  explicit optional&lt;T&gt;::optional(U&amp;&amp;);
  =20
template &lt;class U&gt;=20
  <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 ex=
istence of such converting constructor would cause unexpected ambiguities i=
n overload resolution. Consider the following scenario. We start from a wor=
king 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 optiona=
l 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 substitut=
ing 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 <cod=
e>string const&amp;</code> than to create a temporary optional object and t=
rigger 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 =3D "hello";
fun(s);                                <em>// copy ctor invoked!</em>
</pre>


<p>This example shows how an implicit conversion can cause an inadvertent a=
nd 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=3D'rationale.initializer_list'>Handling <code>initializer_list<=
/code></a></h3>

<p>Another feature worth considering is a "sequence constructor" (one that =
takes <code>initializer_list</code> as its argument). It would be enabled (=
in <code>enable_if</code> sense) only for these <code>T</code>s that themse=
lf provide a sequence constructor. This would be useful to fully support tw=
o features we already mentioned above (but chose not to propose).</p>

<p>First, our goal of "copy initialization forwarding" for <code>optional</=
code> also needs to address the following usages of <code>initializer_list<=
/code>:
</p>

<pre>vector&lt;int&gt; v =3D {1, 2, 4, 8};
optional&lt;vector&lt;int&gt;&gt; ov =3D {1, 2, 4, 8};

assert (v =3D=3D *ov);
</pre>

<p>This is not only a syntactical convenience. It also avoids subtle bugs. =
When perfect forwarding constructor is implemented naively with one variadi=
c constructor, optional vector initialization may render surprising result:=
</p>

<pre>optional&lt;vector&lt;int&gt;&gt; ov =3D {3, 1};

assert (*ov =3D=3D vector{3, 1});    // FAILS!
assert (*ov =3D=3D vector{1, 1, 1}); // TRUE!
</pre>

<p>However this sequence constructor feature is incompatible with another o=
ne: default constructor creating a disengaged optional. This is because, as=
 outlined in the former example, initializer <code>{}</code>, that looks li=
ke 0-element list, is in fact interpretted as the request for value-initial=
ization (default constructor call). This may hit programmers that use initi=
alizer list in "generic" context:</p>

<pre>
template &lt;class ...A&gt; <em>// enable_if: every A is int</em>
void fwd(const A&&... a)
{
  optional&lt;vector&lt;int&gt;&gt; o =3D {a...};
  assert (bool(o)); <em>// not true for empty a</em>
}
</pre>

<p>If this feature were to be added, we would need to provide an assignment=
 from initializer list and variadic 'emplacement' constructor with the firs=
t forwarded argument being <code>initializer_list</code>:</p>

<pre>ov =3D {1, 2, 4, 8};

allocator&lt;int&gt; a;
optional&lt;vector&lt;int&gt;&gt; ou { emplace, {1, 2, 4, 8}, a };

assert (ou =3D=3D ov);
</pre>


<p>Since we are not proposing neither perfect forwarding constructor, nor t=
he "copy initialization forwarding", we are also not proposing the sequence=
 constructor. However, in this proposal, the following constructs work:</p>

<pre>optional&lt;vector&lt;int&gt;&gt; ov{emplace, {3, 1}};
assert (*ov =3D=3D vector{3, 1});

ov.emplace({3, 1});
assert (*ov =3D=3D vector{3, 1});
</pre>


<h3><a name=3D'rationale.optional_optional'><code>optional&lt;optional&lt;T=
&gt;&gt;</code></a></h3>


<p>The necessity to create a "double" optional explicitly does not occur of=
ten. Such type may appear though in generic contexts where we create <code>=
optional&lt;V&gt;</code> and <code>V</code> only happens to be <code>option=
al&lt;T&gt;</code>. Some special behavior to be observed in this situation =
is the following. When copy-initializing with <code>nullopt</code>, the "ou=
termost" optional is initialized to disengaged state. Thus, changing functi=
on argument from <code>optional&lt;T&gt;</code> to <code>optional&lt;option=
al&lt;T&gt;&gt;</code> will silently break the code in places where the arg=
ument passed to function happens to be of type <code>nullopt_t</code>:</p>

<pre><em>// before change</em>
void fun(optional&lt;T&gt; v) {
  process(v);
}

fun(nullopt); <em>// process() called</em>

<em>// after change</em>
void fun(optional&lt;optional&lt;T&gt;&gt; v) {
  if (v) process(*v);
  else   doSthElse();
}

fun(nullopt); <em>// process() not called!</em>
</pre>

<p>This issue would not arise if <code>nullopt</code> were <code>T</code>-s=
pecific:</p>=20

<pre>
fun(optional&lt;T&gt;::nullopt);            <em>// process() called</em>
fun(optional&lt;optional&lt;T&gt;&gt;::nullopt);  <em>// process() not call=
ed</em>
</pre>


<p>Since <code>T</code>-dependent <code>nullopt</code> is not proposed, in =
order to create an engaged optional containing a disengaged optional, one n=
eeds to use one of the following constructs:</p>

<pre>
optional&lt;optional&lt;T&gt;&gt; ot {emplace};
optional&lt;optional&lt;T&gt;&gt; ou {emplace, nullopt};
optional&lt;optional&lt;T&gt;&gt; ov {optional&lt;T&gt;{}};
</pre>

<p>Also note that <code>make_optional</code> will create a "double" optiona=
l when called with optional argument:</p>

<pre>optional&lt;int&gt; oi;
auto ooi =3D make_optional(oi);
static_assert( is_same&lt;optional&lt;optional&lt;int&gt;&gt;, decltype(ooi=
)&gt;::value, "");
</pre>



<h3><a name=3D'rationale.conditional.init'>Conditional initialization to en=
gaged state</a></h3>


<p>It has been suggested, and in fact implemented in Boost.Optional, that <=
code>optional</code> shall have an another constructor with the first argum=
ent of type <code>bool</code>. The value of this argument should be used to=
 determine whether the object should be disengaged, or engaged using the re=
maining arguments. If we wanted to provide it in <code>optional</code> and =
disampiguate the situations where the contained value is also initialized w=
ith the first argument of type <code>bool</code>, it could be easily done b=
y providing a yet another tag (similar to <code>emplace_t</code>):</p>

<pre>bool doIt =3D false;
tr2::optional&lt;string&gt; opstr1{ tr2::only_if(doIt), 3, 'x' };=20
<em>// disengaged</em>

doIt =3D true;
tr2::optional&lt;string&gt; opstr2{ tr2::only_if(doIt), 3, 'x' };=20
<em>// contained value is "xxx"</em>
</pre>

<p>However, we do not see a practical use case for this usage. Undoubtedly,=
 it spares you from an explicit if-statement and a two-phase initialization=
 of the optional object, but then it appears obvious that at some time you =
need to check the value of <code>doIt</code> anyway, because you do not eve=
n know the state of the optional object. It is at that time that you may as=
 well decide to initialize the contained value:</p>

<pre>optional&lt;string&gt; process( bool doIt )
{
  fun1(doIt);
  optional&lt;string&gt; optstr;
 =20
  if (doIt) { <em> // we ned an if to conditionally call fun2()</em>
    fun2();
    optstr.emplace(3, 'x');
  }
 =20
  return optstr;
}</pre>

<p>Also, even if you create optional as disengaged in the conditional const=
ructor, you still have to compute the values of the arguments that you coul=
d potentially use to initialize the contained value, so the two-phase initi=
alization may look more attractive.
</p>

<p>For these reasons we do not propose such conditional constructor at this=
 point. However, there appears to be no difficulty in adding it if we find =
convincing use cases.</p>



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



<p>Add new header in Table 14 (C++ library headers).</p>

<blockquote class=3D"std">
<table>
<caption>Table 14 &mdash; C++ library headers</caption>
<tbody>
<tr>
<td><code>&lt;algorithm&gt;</code></td> <td><code>&lt;fstream&gt;</code></t=
d> <td><code>&lt;list&gt;</code></td> <td><code>&lt;ratio&gt;</code></td> <=
td><code>&lt;tuple&gt;</code></td>

</tr>
<tr>
<td><code>&lt;array&gt;</code></td> <td><code>&lt;functional&gt;</code></td=
> <td><code>&lt;locale&gt;</code></td> <td><code>&lt;regex&gt;</code></td> =
<td><code>&lt;typeindex&gt;</code></td>
</tr>
<tr>
<td><code>&lt;atomic&gt;</code></td> <td><code>&lt;future&gt;</code></td> <=
td><code>&lt;map&gt;</code></td> <td><code>&lt;set&gt;</code></td> <td><cod=
e>&lt;typeinfo&gt;</code></td>

</tr>
<tr>
<td><code>&lt;bitset&gt;</code></td> <td><code>&lt;initializer_list&gt;</co=
de></td> <td><code>&lt;memory&gt;</code></td> <td><code>&lt;sstream&gt;</co=
de></td> <td><code>&lt;type_traits&gt;</code></td>
</tr>
<tr>
<td><code>&lt;chrono&gt;</code></td> <td><code>&lt;iomanip&gt;</code></td> =
<td><code>&lt;mutex&gt;</code></td> <td><code>&lt;stack&gt;</code></td> <td=
><code>&lt;unordered_map&gt;</code></td>

</tr>
<tr>
<td><code>&lt;codecvt&gt;</code></td> <td><code>&lt;ios&gt;</code></td> <td=
><code>&lt;new&gt;</code></td> <td><code>&lt;stdexcept&gt;</code></td> <td>=
<code>&lt;unordered_set&gt;</code></td>
</tr>
<tr>
<td><code>&lt;complex&gt;</code></td> <td><code>&lt;iosfwd&gt;</code></td> =
<td><code>&lt;numeric&gt;</code></td> <td><code>&lt;streambuf&gt;</code></t=
d> <td><code>&lt;utility&gt;</code></td>

</tr>
<tr>
<td><code>&lt;condition_variable&gt;</code></td> <td><code>&lt;iostream&gt;=
</code></td> <td><ins><code>&lt;optional&gt;</code></ins></td> <td><code>&l=
t;string&gt;</code></td> <td><code>&lt;valarray&gt;</code></td>
</tr>
<tr>
<td><code>&lt;dequeue&gt;</code></td> <td><code>&lt;istream&gt;</code></td>=
 <td><code>&lt;ostream&gt;</code></td> <td><code>&lt;strstream&gt;</code></=
td> <td><code>&lt;vector&gt;</code></td>

</tr>
<tr>
<td><code>&lt;exception&gt;</code></td> <td><code>&lt;iterator&gt;</code></=
td> <td><code>&lt;queue&gt;</code></td> <td><code>&lt;system_error&gt;</cod=
e></td> <td><code>&nbsp;</code></td>
</tr>
<tr>
<td><code>&lt;forward_list&gt;</code></td> <td><code>&lt;limits&gt;</code><=
/td> <td><code>&lt;random&gt;</code></td> <td><code>&lt;thread&gt;</code></=
td> <td><code>&nbsp;</code></td>

</tr>
</tbody>
</table>
</blockquote>


<p>After chapter 20.4 Tuples [tuple], insert a new paragraph. (Chapter [tem=
plate.bitset] (Class template <code>bitset</code>) becomes 20.6.)</p>
 =20
 <blockquote class=3D"std">=20
<h3><a name=3D"optional">20.5 Optional objects <span style=3D"float:right">=
[optional]</span></a></h3>


<h4><a name=3D"optional.general">20.5.1 In general <span style=3D"float:rig=
ht">[optional.general]</span></a></h4>

<p>This subclause describes class template <code>optional</code> that repre=
sents <em>optional objects</em>. An <em>optional object for object types</e=
m> is an object that contains the storage for another object and manages th=
e lifetime of this contained object. The contained object may be initialize=
d after the optional object has been initialized, and may be destroyed befo=
re the optional object has been destroyed. The initialization state of the =
contained object is tracked by the optional object. An <em>optional object =
for lvalue reference types</em> is an object capable of storing the address=
 of another object. The address stored by the optional object can be change=
d or set to a value that does not represent a valid address. </p>

 =20
<h4><a name=3D"optional.synop">20.5.2 Header <kbd>&lt;optional&gt;</kbd> sy=
nopsis <span style=3D"float:right">[optional.synop]</span></a></h4>

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

  // <em><a href=3D"#optional.inplace">20.5.5</a>, In-place construction</e=
m>
  struct emplace_t{};
  constexpr emplace_t emplace{};

  // <em><a href=3D"#optional.nullopt">20.5.6</a>, Disengaged state indicat=
or</em>
  struct nullopt_t{<em>see below</em>};
  constexpr nullopt_t nullopt(<em>unspecified</em>);
 =20
  // <em><a href=3D"#optional.bad_optional_access">20.5.7</a>, class bad_op=
tional_access</em>
  class bad_optional_access;

  // <em><a href=3D"#optional.relops">20.5.8</a>, Relational operators</em>
  template &lt;class T&gt;
    constexpr bool operator=3D=3D(const optional&lt;T&gt;&amp;, const optio=
nal&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator!=3D(const optional&lt;T&gt;&amp;, const optiona=
l&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&lt;(const optional&lt;T&gt;&amp;, const optiona=
l&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&gt;(const optional&lt;T&gt;&amp;, const optiona=
l&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&lt;=3D(const optional&lt;T&gt;&amp;, const opti=
onal&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&gt;=3D(const optional&lt;T&gt;&amp;, const opti=
onal&lt;T&gt;&amp;);

  // <em><a href=3D"#optional.nullops">20.5.9</a>, Comparison with <code>nu=
llopt</code></em>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;=
T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator=3D=3D(nullopt_t, const o=
ptional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&=
gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator!=3D(nullopt_t, const opt=
ional&lt;T&gt;&amp;) noexcept;
  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 opt=
ional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const optional&lt=
;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&lt;=3D(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 opt=
ional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const optional&lt=
;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;=3D(nullopt_t, const =
optional&lt;T&gt;&amp;) noexcept;

  // <em><a href=3D"#optional.comp_with_t">20.5.10</a>, Comparison with T</=
em>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;=
T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;, cons=
t optional&lt;T&gt;&amp;);
  template &lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&=
gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, const =
optional&lt;T&gt;&amp;);
  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&lt;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;, con=
st 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;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;, con=
st optional&lt;T&gt;&amp;);

  // <em><a href=3D"#optional.specalg">20.5.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;=20
    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;=20
    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; mak=
e_optional(T&amp;&amp;);

  // <em><a href=3D"#optional.hash">20.5.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;;
} <em>// namespace experimental</em>
} <em>// namespace std</em>
</pre>
 =20
  <p>A program that necessitates the instantiation of template <code>option=
al</code> for an lvalue reference or rvalue reference type, or for types <c=
ode>emplace_t</code> or <code>nullopt_t</code>, or a possibli cv-qualified =
reference to types <code>emplace_t</code> or <code>nullopt_t</code> is ill-=
formed.</p>

<h4><a name=3D"optional.defs">20.5.3 Definitions <span style=3D"float:right=
">[optional.defs]</span></a></h4>
=20
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>diseng=
aged</em> if it has been default constructed, constructed or assigned with =
a value of type <code>nullopt_t</code>, constructed or assigned with a dise=
ngaged optional object of type <code>optional&lt;T&gt;</code>.
  </p>

  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>engage=
d</em> if it has=20
  been modified with member function <code>emplace</code>, constructed with=
 a value of type <code>T</code>, assigned a value of type <code>T</code>, c=
opy-constructed from or assigned with an engaged optional object of type <c=
ode>optional&lt;T&gt;</code>.=20
  </p>
 =20
  <!--
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>diseng=
aged</em> if it has been default constructed, constructed or assigned with =
a value of type <code>nullopt_t</code>, constructed or assigned with a dise=
ngaged optional object of type <code>optional&lt;U&gt;</code>, with <code>U=
</code> being equal or not to <code>T</code>.
  </p>
 =20
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>engage=
d</em> if it has=20
  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>, c=
opy-constructed from or assigned with an engaged optional object of type <c=
ode>optional&lt;U&gt;</code>, where <code>U</code> is same as or convertibl=
e to <code>T</code>.=20
  </p>
  -->

  <p>Being engaged or disengaged is part of the optional object's state.</p=
>
 =20
  <!-- <p>An <em>optional object for object type</em> is an instance of cla=
ss <code>optional&lt;T&gt;</code> where <code>T</code> is of object type (3=
..9).</p>
 =20
  <p>An <em>optional object for lvalue reference type</em> is an instance o=
f class <code>optional&lt;T&gt;</code> where <code>T</code> is of lvalue re=
ference type.</p>-->
 =20

 =20
<h4><a name=3D"optional.object">20.5.4 <code>optional</code> for object typ=
es <span style=3D"float:right">[optional.object]</span></a></h4>

 =20
<pre>
namespace std {
namespace experimental {

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

    // <em><a href=3D"#optional.object.ctor">20.5.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(emplace_t, A=
rgs&amp;&amp;...);
    template &lt;class U, class... Args&gt;
      constexpr explicit optional(emplace_t, initializer_list&lt;U&gt;, Arg=
s&amp;&amp;...);

    // <em><a href=3D"#optional.object.dtor">20.5.4.2</a>, destructor</em>
    ~optional();

    // <em><a href=3D"#optional.object.assign">20.5.4.3</a>, assignment</em=
>
    optional&amp; operator=3D(nullopt_t) noexcept;<!--
    optional&amp; operator=3D(const T&amp;);
    optional&amp; operator=3D(T&amp;&amp;);-->
    optional&amp; operator=3D(const optional&amp;);
    optional&amp; operator=3D(optional&amp;&amp;) noexcept(<em>see below</e=
m>);
    template &lt;class U&gt; optional&amp; operator=3D(U&amp;&amp;);<!--
    template &lt;class U&gt; optional&amp; operator=3D(const optional&lt;U&=
gt;&amp;);
    template &lt;class U&gt; optional&amp; operator=3D(optional&lt;U&gt;&am=
p;&amp;);
    template &lt;class U&gt; optional&amp; operator=3D(initializer_list&lt;=
U&gt;);-->
    template &lt;class... Args&gt; optional&amp; emplace(Args&amp;&amp;...)=
;
    template &lt;class U, class... Args&gt;
      optional&amp; emplace(initializer_list&lt;U&gt;, Args&amp;&amp;...);

    // <em><a href=3D"#optional.object.swap">20.5.4.4</a>, swap</em>
    void swap(optional&) noexcept(<em>see below</em>);

    // <em><a href=3D"#optional.object.observe">20.5.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>
  };

} // <em>namespace experimental</em>
} // <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 t=
he optional object. Implementations are not permitted to use additional sto=
rage, such as dynamic memory, to allocate its contained value.  The contain=
ed value shall be allocated in a region of the <code>optional&lt;T&gt;</cod=
e> storage suitably aligned for the type <code>T</code>. Initializing the c=
ontained value shall put the optional object into engaged state. Destroying=
 the contained value shall put the optional object into disengaged state.</=
p>

<p>Members <code><var>init</var></code> and <code><var>val</var></code> are=
 provided for exposition only. Implementations need not provide those membe=
rs. <code><var>init</var></code> indicates whether the <code>optional</code=
> object's contained value has been initialized (and not yet destroyed); <c=
ode><var>val</var></code> points to (a possibly uninitialized) contained va=
lue.
  </p>

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

<p>Throughout this subclause term <em>direct-non-list-initialization</em> i=
s used to denote a direct-initialization that is not list-initialization.</=
p>



<h5><a name=3D"optional.object.ctor">20.5.4.1 Constructors <span style=3D"f=
loat:right">[optional.object.ctor]</span></a></h5>

 =20
  <p class=3D"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=3D"attribute">
  <dt>Effects:</dt> <dd><p>Constructs a disengaged <code>optional</code> ob=
ject.</p></dd>
  <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D false</code>.</p=
></dd>
  <dt>Remarks:</dt> <dd><p>No <code>T</code> object referenced is initializ=
ed. For every object type <code>T</code> these constructors shall be <code>=
constexpr</code> constructors (7.1.5).</p></dd>
  </dl>

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

  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue as if direct-non-list-initializing an object of type <code>T</code> wit=
h the expression <code>*<var>rhs</var></code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) =3D=3D bool(=
*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;::optional(optional&lt;T&gt; &amp;&amp; <var>rhs</=
var>) noexcept(<em>see below</em>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue as if direct-non-list-initializing an object of type <code>T</code> wit=
h the expression <code>std::move(*<var>rhs</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) =3D=3D bool(=
*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</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>

 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;::optional(const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object by =
direct-non-list-initializing the contained value with the expression <code>=
<var>v</var></code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</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>const=
expr</code> constructor.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;::optional(T&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object by =
direct-non-list-initializing the contained value with the expression <code>=
std::move(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</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>const=
expr</code> constructor.</p></dd>
  </dl>
 =20
<!-- =20
  <p class=3D"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;::option=
al(U&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an engaged <code>optional</code> ob=
ject; initializes the contained value as if constructing an object of type =
<code>T</code> with argument <code>std::forward&lt;U&gt;(<var>v</var>)</cod=
e>.</p></dd>

    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code> an=
d <code>*(*this)</code> is equivalent to <code><var>v</var></code> if conve=
rted to <code>T</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload resolution unless <code>OnlyExplicitlyConstructible(T, U&amp;&amp;)<=
/code> is <code>true</code>. The second function shall not participate in t=
he overload resolution unless <code>is_convertible&lt;U&amp;&amp;, T&gt;::v=
alue</code> is <code>true</code>. The expression inside <code>noexcept</cod=
e> is equivalent to:</p> <pre>noexcept(T(std::declval&lt;U&amp;&amp;&gt;())=
)</pre></dd>
  </dl>
 =20
 =20
  <p class=3D"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;  optio=
nal&lt;T&gt;::optional(const optional&lt;U&gt;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue 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>) =3D=3D fa=
lse</code> then <code>bool(*this) =3D=3D false</code>; otherwise <code>bool=
(*this) =3D=3D true</code> 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>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload 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>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(optio=
nal&lt;U&gt;&amp;&amp; <var>rhs</var>);<br>template &lt;class U&gt;  option=
al&lt;T&gt;::optional(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue 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>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload resolution unless <code>OnlyExplicitlyConstructible(T, U&amp;&amp;)<=
/code> is <code>true</code>. The second function shall not participate in t=
he overload resolution unless <code>is_convertible&lt;U&amp;&amp;, T&gt;::v=
alue</code> is <code>true</code>.</dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(initi=
alizer_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>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue as if constructing an object of type <code>T</code> with argument <code=
><var>il</var></code></p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload resolution unless <code>OnlyExplicitlyConstructible(T, initializer_l=
ist&lt;U&gt;)</code> is <code>true</code>. The second function shall not pa=
rticipate in the overload resolution unless <code>is_convertible&lt;initial=
izer_list&lt;U&gt;, T&gt;::value</code> is <code>true</code>.</dd>
  </dl>
-->
 =20
  <p class=3D"function">
  <code>template &lt;class... Args&gt; constexpr explicit optional(emplace_=
t, Args&amp;&amp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an engaged <code>optional</code> ob=
ject. Initializes the contained value as if constructing an object of type =
T with the arguments <code>std::forward&lt;Args&gt;(<var>args</var>)...</co=
de>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>T</code>'s constructor selected for t=
he initialization is a <code>constexpr</code> constructor, this constructor=
 shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>


  <p class=3D"function">
  <code>template &lt;class U, class... Args&gt;<br>
  explicit optional(emplace_t, initializer_list&lt;U&gt; <var>il</var>, Arg=
s&amp;&amp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></d=
d>
    <dt>Effects:</dt> <dd><p>Constructs an engaged <code>optional</code> ob=
ject. Initializes the contained value as if constructing an object of type =
T with the arguments <code><var>il</var>, std::forward&lt;Args&gt;(<var>arg=
s</var>)...</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</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;, =
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 t=
he initialization is a <code>constexpr</code> constructor, this constructor=
 shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>


<h5><a name=3D"optional.object.dtor">20.5.4.2 Destructor  <span style=3D"fl=
oat:right">[optional.object.dtor]</span></a></h5>
 =20
  <p class=3D"function">=20
  <code>optional&lt;T&gt;::~optional();</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>If <code>is_trivially_destructible&lt;T&gt;::v=
alue !=3D true</code> and <code>bool(*this) =3D=3D true</code>, calls <code=
><var>val</var>-&gt;T::~T()</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>is_trivially_destructible&lt;T&gt;::v=
alue =3D=3D true</code> then this destructor shall be a trivial destructor.=
</p></dd>
  </dl>
 =20
 =20
 =20
<h5><a name=3D"optional.object.assign">20.5.4.3 Assignment  <span style=3D"=
float:right">[optional.object.assign]</span></a></h5>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(nullopt_t) no=
except;</code>
  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> 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>bool(*this) =3D=3D false</code>.<=
/p></dd>
  </dl>
 =20
  <!--
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(const T&amp; =
<var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> assign=
s <code><var>rhs</var></code> to the contained value; otherwise constructs =
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>bool(*this) =3D=3D true</code> an=
d <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 du=
ring the call to <code>T</code>'s copy constructor, no effect. If an except=
ion is thrown during the call to <code>T</code>'s copy assignment, the stat=
e of its contained value is as defined by the exception safety guarantee of=
 <code>T</code>'s copy assignment.</p></dd>
  </dl>

 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(T&amp;&amp; <=
var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> assign=
s <code>std::move(<var>rhs</var>)</code> to the contained value; otherwise =
constructs 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>bool(*this) =3D=3D true</code> an=
d <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 du=
ring 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</c=
ode>'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>
 =20
  -->
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(const optiona=
l&lt;T&gt;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D 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><code>bool(<var>rhs</var>) =3D=3D bool(=
*this)</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 un=
changed. 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 constructor.</p=
></dd>
  </dl>
 =20
=20
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(optional&lt;T=
&gt;&amp;&amp; <var>rhs</var>) noexcept(<em>see below</em>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D=
 true</code>, assigns <code>std::move(*<var>rhs</var>)</code> to the contai=
ned value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) =3D=3D 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 && is_n=
othrow_move_constructible&lt;T&gt;::value</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 un=
changed. 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 <c=
ode><var>*val</var></code> and <code>*rhs.val</code> is determined by excep=
tion safety guarantee of <code>T</code>'s move assignment.</p></dd>

  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(U&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&gt;::value</co=
de> 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>bool(*this) =3D=3D true</code> assign=
s <code>std::forward&lt;U&gt;(<var>v</var>)</code> to the contained value; =
otherwise constructs 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>bool(*this) =3D=3D true</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 du=
ring the call to <code>T</code>'s constructor, the state of <code><var>v</v=
ar></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 as=
signment.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload=
 resolution unless  <code>is_same&lt;typename remove_reference&lt;U&gt;::ty=
pe, T&gt;::value</code> is  <code>true</code>.</dd>
  </dl>
  <p>[<i>Note:</i> The reson to provide such generic assignment and then co=
nstraining it so that effectively <code>T</code> =3D=3D <code>U</code> is t=
o guarantee that assignment of the form <code>o =3D {}</code> is unambiguou=
s. &mdash;<i>end note</i>]</p>

  <!--
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(U&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&gt;::value</co=
de> 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>bool(*this) =3D=3D true</code> assign=
s <code>std::forward&lt;U&gt;(<var>v</var>)</code> to the contained value; =
otherwise constructs 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>bool(*this) =3D=3D true</code> an=
d <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 du=
ring the call to <code>T</code>'s constructor, the state of <code><var>v</v=
ar></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 as=
signment.</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 o=
f class template <code>optional</code>.</dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(const optional&lt;U&gt;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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&a=
mp;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D 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>) =3D=3D fa=
lse</code> then <code>bool(*this) =3D=3D false</code>; otherwise <code>bool=
(*this) =3D=3D true</code> 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 un=
changed. If an exception is thrown during the call to <code>T</code>'s cons=
tructor, no effect. If an exception is thrown during the call to <code>T</c=
ode>'s  assignment, the state of i<code><var>*val</var></code> is defined b=
y the exception safety guarantee of <code>T</code>'s  assignment.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D=
 true</code>, assigns <code>std::move(*<var>rhs</var>)</code> to the contai=
ned value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) =3D=3D fa=
lse</code> then <code>bool(*this) =3D=3D false</code>; otherwise <code>bool=
(*this) =3D=3D true</code> and <code>*(*this)</code> is equivalent to the v=
alue <code>*<var>rhs</var></code> had initially if converted to <code>T</co=
de>.</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 un=
changed. If an exception is thrown during the call to <code>T</code>'s cons=
tructor, no effect. If an exception is thrown during the call to <code>T</c=
ode>'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>
 =20

  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(initializer_list&lt;U&gt; <var>il</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;&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>.</p></=
dd>
     <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> assig=
ns <code><var>il</var></code> to the contained value; otherwise constructs =
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>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever <code>T::T(initializer_list&lt;U&gt;)<=
/code> or <code>T::operator=3D(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 d=
uring 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;&g=
t;::value</code> is <code>true</code> and <code>is_assignable&lt;T, initial=
izer_list&lt;U&gt;&gt;::value</code> is <code>true</code>.</dd>
  </dl>
-->
 =20
  <p class=3D"function">
  <code>template &lt;class... Args&gt; optional&lt;T&gt;&amp; optional&lt;T=
&gt;::emplace(Args&amp;&amp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"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 =3D nullopt</code>. Then ini=
tializes 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>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever expression <code>T(std::forward&lt;Arg=
s&gt;(<var>args</var>)...)</code> throws.</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>
 =20
 =20
   <p class=3D"function">
  <code>template &lt;class U, class... Args&gt; optional&lt;T&gt;&amp; opti=
onal&lt;T&gt;::emplace(initializer_list&lt;U&gt; <var>il</var>, Args&amp;&a=
mp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></d=
d>
    <dt>Effects:</dt> <dd><p>Calls <code>*this =3D nullopt</code>. Then ini=
tializes 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>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever expression <code>T(<var>il</var>, std:=
:forward&lt;Args&gt;(<var>args</var>)...)</code> throws.</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;, =
Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
  </dl>


 =20
<h5><a name=3D"optional.object.swap">20.5.4.4 Swap <span style=3D"float:rig=
ht">[optional.object.swap]</span></a></h5>


  <p class=3D"function">
  <code>void optional&lt;T&gt;::swap(optional&lt;T&gt;& rhs) noexcept(<em>s=
ee below</em>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>T</code> shall be swappable for lvalues=
 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> =3D=3D false && <var>=
rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</var> =3D=
=3D true && <var>rhs.init</var> =3D=3D false</code>, constructs the contain=
ed 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>i=
nit</var>, <var>rhs.init</var>)</code>. If <code><var>init</var> =3D=3D fal=
se && <var>rhs.init</var> =3D=3D true</code>, constructs the contained valu=
e 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> =3D=3D true && =
<var>rhs.init</var> =3D=3D true</code>, calls <code>swap(*(*this), *<var>rh=
s</var>)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever expressions <code>swap(declval&lt;T&am=
p;&gt;(), declval&lt;T&amp;&gt;())</code> and <code>T{move(declval&lt;T&amp=
;&amp;&gt;())}</code> 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 && noexc=
ept(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 un=
changed. 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> i=
s determined by the exception safety quarantee of <code>swap</code> for lva=
lues of <code>T</code>. If an exception is thrown durning 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 quarantee of <=
code>T</code>'s move constructor.</p></dd>

  </dl>

 =20
 =20
<h5><a name=3D"optional.object.observe">20.5.4.5 Observers  <span style=3D"=
float:right">[optional.object.observe]</span></a></h5>


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

  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</p></dd=
>
    <dt>Returns:</dt> <dd><p><code>val</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 w=
ith overloaded unary <code>operator&amp;</code>, the first function shall b=
e a <code>constexpr</code> function.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T const&amp; optional&lt;T&gt;::operator*() const;<br>T&a=
mp; optional&lt;T&gt;::operator*();</code>

  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</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>
 =20
 =20
  <p class=3D"function">
  <code>constexpr explicit optional&lt;T&gt;::operator bool() noexcept;</co=
de>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code><var>init</var></code></p></dd>
    <dt>Remarks:</dt> <dd><p>this function shall be a <code>constexpr</code=
> function.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T const&amp; optional&lt;T&gt;::value() const;<br>T&amp; =
optional&lt;T&gt;::value();</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code><var>*val</var></code>, if <code>bool(*t=
his)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p><code>bad_optional_access</code> if <code>!*thi=
s</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall be a <code>constexpr<=
/code> function.</p></dd>

  </dl>
 =20
 =20

  <p class=3D"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>
 =20
  <dl class=3D"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>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization of the return value throws.</p></dd=
>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> =3D=3D true<=
/code> and exception is thrown durning the call to <code>T</code>'s constru=
ctor, the value of <code><var>init</var></code> and <code><var>v</var></cod=
e> remains unchanged and the state of <code><var>*val</var></code> is deter=
mined by the exception safety quarantee of the selected <code>T</code>'s co=
nstructor. Otherwise, when exception is thrown durning the call to <code>T<=
/code>'s constructor, the value of <code><var>*this</var></code> remains un=
changed and the state of <code><var>v</var></code> is determined by the exc=
eption safety quarantee of the selected <code>T</code>'s constructor</p></d=
d>
    <dt>Remarks:</dt> <dd><p>If the selected <code>T</code>'s constructor i=
s a <code>constexpr</code> constructor, this function shall be a <code>cons=
texpr</code> function.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; T optional&lt;T&gt;::value_or(U&amp;&amp; =
<var>v</var>) &amp;&amp;;</code>
  </p>
 =20
  <dl class=3D"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>Whatever the execution of the selected <code>T<=
/code>'s constructor selected for the initialization of the return value th=
rows.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> =3D=3D true<=
/code> and exception is thrown durning the call to <code>T</code>'s constru=
ctor, the value of <code><var>init</var></code> and <code><var>v</var></cod=
e> remains unchanged and the state of <code><var>*val</var></code> is deter=
mined by the exception safety quarantee of the <code>T</code>'s constructor=
.. Otherwise, when exception is thrown durning the call to <code>T</code>'s =
constructor, the value of <code><var>*this</var></code> remains unchanged a=
nd the state of <code><var>v</var></code> is determined by the exception sa=
fety quarantee of the selected <code>T</code>'s constructor</p></dd>
  </dl>


 =20
<h4><a name=3D"optional.inplace">20.5.5 In-place construction  <span style=
=3D"float:right">[optional.inplace]</span></a></h4>


  <p class=3D"function">
  <code>struct emplace_t{}; <br>constexpr emplace_t emplace{};</code>
  </p>

  <p>The struct <code>emplace_t</code> is a disengaged structure type used =
as a unique type to disambiguate constructor and function overloading. Spec=
ifically, <code>optional&lt;T&gt;</code> has a constructor with <code>empla=
ce_t</code> as the first argument followed by an argument pack; this indica=
tes 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>

 =20
<h4><a name=3D"optional.nullopt">20.5.6 Disengaged state indicator  <span s=
tyle=3D"float:right">[optional.nullopt]</span></a></h4>


  <p class=3D"function">
  <code>struct nullopt_t{<em>see below</em>}; <br>constexpr nullopt_t nullo=
pt(<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> objec=
ts. In particular, <code>optional&lt;T&gt;</code> has a constructor with <c=
ode>nullopt_t</code> as single argument; this indicates that a disengaged o=
ptional object shall be constructed.
  </p>
 =20
  <p>Type <code>nullopt_t</code> shall not have a default constructor. It s=
hall be a literal type. Constant <code>nullopt</code> shall be initialized =
with argument of literal type.</p>
 =20
=20
 =20
<h4><a name=3D"optional.bad_optional_access">20.5.7 Class <code>bad_optiona=
l_access</code>  <span style=3D"float:right">[optional.bad_optional_access]=
</span></a></h4>
=20
<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 t=
hrown as exceptions to report the situation where an attempt is made to acc=
ess the value of a disengaged optional object.</p>

  <p class=3D"function">=20
  <code>bad_optional_access(const string&amp; what_arg);</code>
  </p>
 =20
  <dl class=3D"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()) =3D=
=3D 0</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">=20
  <code>bad_optional_access(const char* what_arg);</code>
  </p>
 =20
  <dl class=3D"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) =3D=3D 0</c=
ode>.</p></dd>
  </dl>

=20
<h4><a name=3D"optional.relops">20.5.8 Relational operators  <span style=3D=
"float:right">[optional.relops]</span></a></h4>
=20

  <p class=3D"function">=20
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const option=
al&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code>
  </p>
 =20
  <dl class=3D"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) !=3D bool(y)</code>, <code>fals=
e</code>; otherwise if <code>bool(x) =3D=3D false</code>, <code>true</code>=
; otherwise <code>*x =3D=3D *y</code>.</p></dd>
  <dt>Remarks:</dt> <dd><p>Instantiations of this function template for whi=
ch <code>*x =3D=3D *y</code> is a core constant expression, shall be <code>=
constexpr</code> functions.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>!(x =3D=3D y)</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <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>=20
  </p>

  <dl class=3D"attribute">
  <dt>Requires:</dt> <dd><p><code>T</code> shall meet the requirements of <=
code>LessThanComparable</code>.</p></dd>
  <dt>Returns:</dt> <dd><p>If <code>(!y)</code>, <code>false</code>; otherw=
ise, if <code>(!x)</code>, <code>true</code>; otherwise <code>*x &lt; *y</c=
ode>.</p></dd>
  <dt>Remarks:</dt> <dd><p>Instantiations of this function template for whi=
ch <code>*x &lt; *y</code> is a core constant expression, shall be <code>co=
nstexpr</code> functions.</p></dd>
  </dl>

 =20
  <p class=3D"function">
  <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>=20
  </p>

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

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

  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>!(x < y)</code>.</p></dd>
  </dl>
=20

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


  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;<br>template &lt;class T&g=
t; constexpr bool operator!=3D(nullopt_t, const optional&lt;T&gt;&amp; x) n=
oexcept;</code>
  </p>

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


  <p class=3D"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>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>false</code>.</p></dd>
  </dl>

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

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

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

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

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

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

=20
<h4><a name=3D"optional.comp_with_t">20.5.10 Comparison with <code>T</code>=
 <span style=3D"float:right">[optional.comp_with_t]</span></a></h4>


  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const option=
al&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> =3D=3D =
<var>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;=
 <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> =3D=3D *=
<var>x</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> !=3D <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> !=3D *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
  <!-- -->
 =20
  <p class=3D"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>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt; <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt; *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <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>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &gt; <v=
ar>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt; *<v=
ar>x</var> : false</code>.</p></dd>
  </dl>
 =20
  <!-- -->
=20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var>  &gt;=
=3D <var>v</var> : false</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt;=3D =
*<var>x</var> : false</code>.</p></dd>
  </dl>
  =20
  =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt;=3D=
 <var>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt;=3D =
*<var>x</var> : true</code>.</p></dd>
  </dl>
 =20

=20
<h4><a name=3D"optional.specalg">20.5.11 Specialized algorithms <span style=
=3D"float:right">[optional.specalg]</span></a></h4>


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

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


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

  <dl class=3D"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>
 =20
 =20
  <p class=3D"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=3D"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 safe=
ty as <code>T</code>'s move-constructor.</p></dd>
  </dl>-->
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt;<br>
    &nbsp;&nbsp;constexpr optional&lt;typename decay&lt;T&gt;::type&gt; mak=
e_optional(T&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code>optional&lt;typename decay&lt;T&gt;::typ=
e&gt;(std::forward&lt;T&gt;(<var>v</var>))</code>.</p></dd>
  </dl>
 =20
 =20
<h4><a name=3D"optional.hash">20.5.12 Hash support <span style=3D"float:rig=
ht">[optional.hash]</span></a></h4>


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

  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p>the template specilaization <code>hash&lt;T&g=
t;</code> shall meet the requirements of class template <code>hash</code> (=
20.9.12).
      The template specilaization <code>hash&lt;optional&lt;T&gt;&gt;</code=
> shall meet the requirements of class template <code>hash</code>.=20
      For an object <code>o</code> of type <code>optional&lt;T&gt;</code>, =
if <code>bool(o) =3D=3D true</code>,=20
      <code>hash&lt;optional&lt;T&gt;&gt;()(o)</code> shall evaluate to the=
 same value as <code>hash&lt;T&gt;()(*o)</code>.</p></dd>=20
  </dl>
 =20
</blockquote>




<h2><a name=3D"optional_ref">Auxiliary proposal &mdash; optional references=
</a></h2>



<p>We propose optional references as an auxiliary proposal. This is to give=
 the Committee the freedom to make the decision to accept or not optional r=
eferences independently of the decision to accept optional values. </p>


<h3><a name=3D"optional_ref.overview">Overview</a></h3>

<p>Optional references are surprising to many people because they do not ap=
pear to add any more functionality than pointers do. There exist though a c=
ouple of arguments in favour optional references:</p>

<ul>
<li><code>optional&lt;T&gt;</code> can be used in generic code, were <code>=
T</code> can be either a reference or an object. </li>
<li>It is slightly easier to pass arguments to functions, beacause addresso=
f operator is not required. </li>
<li>Raw pointers historically add confusion in the sense that it is not cle=
ar whether we should delete the value they point to or not, as well as whet=
her we should expect <code>nullptr</code> or not. With optional references =
the answer to these questions is obvious.</li>
</ul>


<p>The interface for optional references is somewhat limited compared to th=
at for optional values.</p>

<pre>int i =3D 1;
int j =3D 2;
optional&lt;int&amp;&gt; ora;                <em>// disengaged optional ref=
erence to int</em>
optional&lt;int&amp;&gt; orb =3D i;            <em>// contained reference r=
efers to object i</em>

*orb =3D 3;                          <em>// i becomes 3</em>
ora =3D j;                           <em>// ERROR: optional refs do not hav=
e assignment from T</em>
ora =3D optional&lt;int&amp;&gt;{j};           <em>// contained reference r=
efers to object j</em>
ora =3D {j};                         <em>// same as line above</em>
orb =3D ora;                         <em>// rebinds orb to refers to the sa=
me object as ora</em>
*orb =3D 7;                          <em>// j becomes 7</em>
ora.emplace(j);                    <em>// OK: contained reference refers to=
 object j</em>
ora.emplace(i);                    <em>// OK: contained reference now refer=
s to object i</em>
ora =3D nullopt;                     <em>// OK: ora becomes disengaged</em>
</pre>

<p>In some aspects optional lvalue references act like raw pointers: they a=
re rebindable, may become disengaged, and do not have special powers (such =
as extending the life-time of a temporary). In other aspects they act like =
C++ references: they do not provide pointer arithmetic, operations like com=
parisons, hashing are performed on referenced objects. </p>

<pre>hash&lt;int&gt; hi;
hash&lt;optional&lt;int&amp;&gt;&gt; hoi;

int i =3D 0;
int j =3D 0;
optional&lt;int&amp;&gt; ori =3D i;            <em>// orj and ori refer to =
two different objects</em>
optional&lt;int&amp;&gt; orj =3D j;            <em>// but with the same val=
ue</em>

assert (hoi(ori) =3D=3D hi(i));       =20
assert (hoi(ori) =3D=3D hoi(orj));     <em>// because we are hashing the va=
lues</em>
assert (ori =3D=3D orj);               <em>// because we are comparing the =
values</em>
</pre>

<p>Because optional references are easily implementable  with a raw pointer=
, we require that almost no operation on optional references (except <code>=
value</code> and <code>value_or</code>) throws exceptions. </p>

<p>Optional references do not popagate constness to the object they indirec=
t to:</p>

<pre>int i =3D 9;
const optional&lt;int&amp;&gt; mi =3D i;
int&amp; r =3D *mi;                      <em>// OK: decltype(*mi) =3D=3D in=
t&amp;</em>
=20
optional&lt;const int&amp;&gt; ci =3D i;
int&amp; q =3D *ci;                      <em>// ERROR: decltype(*ci) =3D=3D=
 const int&amp;</em>
</pre>

<p>Note also the peculiar way in which function <code>make_optional</code> =
can create optional references:</p>

<pre>int i =3D 1;
auto oi =3D make_optional(i);          <em>// decltype(oi) =3D=3D optional&=
lt;int&gt;</em>
auto ri =3D make_optional(ref(i));     <em>// decltype(ri) =3D=3D optional&=
lt;int&amp;&gt;</em>
</pre>



<h3><a name=3D"optional_ref.use_cases">Practical use cases</a></h3>


<p>While used significantly less often, optional references are useful in c=
ertain cases. For instance, consider that you need to find a possibly missi=
ng element in some sort of a collection, and if it is found, change it. The=
 following is a possible implementation of such function that will help wit=
h the find.</p>

<pre>
optional&lt;int&amp;&gt; find_biggest( vector&lt;int&gt;& vec )
{
  optional&lt;int&amp;&gt; biggest;=20
  for (int & val : vec) {
    if (!biggest || *biggest &lt; val) {
      biggest.emplace(val);
    }
  }
  return biggest;
}=20
</pre>

<p>This could be alternatively implemented using a raw pointer; however, op=
tional reference makes it clear that the caller will not be owing the objec=
t. For another example, consider that we want a function to modify the pass=
ed object (<code>storeHere</code> in the example) as one of its responsibil=
ities, but we sometimes cannot provide the object, but we do want the funct=
ion to perform other responsibilities. The following is the possible implem=
entation</p>

<pre>template &lt;typename T&gt;
T getValue( optional&lt;T&gt; newVal =3D nullopt, optional&lt;T&amp;&gt; st=
oreHere =3D nullopt )
{
  if (newVal) {
    cached =3D *newVal;
   =20
    if (storeHere) {
      *storeHere =3D *newVal; <em>// LEGAL: assigning T to T</em>
    }     =20
  }
  return cached;     =20
}
</pre>

<p>Again, this can also be implemented with pointers; however, with optiona=
l references the ownership of the memory is clear. Additionally, we do not =
have to use the indirection operator:</p>

<pre>
static int global =3D 0;
const int newVal =3D 2;
return getValue(newVal, global);
</pre>


<h3><a name=3D'optional_ref.rationale.assign'>Assignment for optional refer=
ences</a></h3>

<p>The semantics of optional references' copy assignment turned out to be v=
ery controversial. This is because whatever semantics for such assignment i=
s chosen, it is confusing to many programmers. An optional reference can be=
 seen as a reference with postponed initialization. In this case, assignmen=
t (to engaged optional reference) is expected to have deep copy semantics: =
it should assign value to the referred object. On the other hand, an option=
al reference can be seen as a pointer with different syntax. In this case t=
he assignment (to engaged optional reference) should change the reference, =
so that it refers to the new object. Neither of these models appears more v=
alid than the other. On the other hand, the majority of people insist that =
optional should be copy-assignable. We choose somewhat arbitralrily to prov=
ide a rebinding semantics for <code>std::optional</code>. This is to follow=
 the practice adapted by <code>std::reference_wrapper</code> and <code>boos=
t::optional</code>. In consequence, optional references are not value seman=
tic types: <code>operator=3D=3D</code> compares something else than copy as=
signment and constructor are copying. This should not be surprising, though=
, for a component that is called a "reference".</p>

<p>The other semantics can be implemented by using the following idiom:</p>

<pre>void assign_norebind(optional&lt;T&amp;&gt;&amp; optref, T&amp; obj)
{
  if (optref) *optref =3D obj;
  else        optref.emplace(obj);
}
</pre>

<h3><a name=3D'optional_ref.rationale.rref_binding'>No rvalue binding for o=
ptional references</a></h3>

<p>Optional referencs cannot provide an essential feature of native referen=
ces: extending the life-time of temporaries (rvalues). Temporaries can be b=
ound to (1) rvalue references and to (2) lvalue references to const. In ord=
er to avoid dangling reference problems we need to prevent either type of b=
infing to optional references. In order to prevent the former, we disallow =
optional rvalue references altogether. We are not aware of any practical us=
e case for such entities. Since optional lvalue references to const appear =
useful, we avoid the rvalue binding problem by requiring implementations to=
 "poison" rvalue reference constructors for optional lvalue references to c=
onst. This may appear surprising as it is inconsistent with normal (non-opt=
ional) reference behavior:</p>

<pre>const int&amp; nr =3D int{1};           <em>// ok</em>
optional&lt;const int&&gt; or =3D int{1}; <em>// error: int&amp;&amp; ctor =
deleted</em>
</pre>





<h3><a name=3D"optional_ref.wording">Wording</a></h3>


<p>The wording for optional references is relative to the wording for optio=
nal values form the main proposal. It assumes the wording for optional valu=
es has already been applied.</p>

<p>In [optional.synop] add declaration of template specialization <code>opt=
ional&lt;T&amp;&gt;</code>:</p>

 <blockquote class=3D"std">
 <h4>20.5.2 Header <kbd>&lt;optional&gt;</kbd> synopsis <span style=3D"floa=
t:right">[optional.synop]</span></h4>

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

<ins>  // <em><a href=3D"#optional.lref">20.5.5</a>, <code>optional</code> =
for lvalue reference types</em>
  template &lt;class T&gt; class optional&lt;T&amp;&gt;;</ins>

  // <em><a href=3D"#optional.inplace"><del>20.5.5</del><ins>20.5.6</ins></=
a>, In-place construction</em>
  struct emplace_t{};
  constexpr emplace_t emplace{};

 </pre>
 </blockquote>
=20
 <p>Next, in the same clause change:</p>
=20
 <blockquote class=3D"std">
  <pre>// <em><a href=3D"#optional.comp_with_t"><del>20.5.10</del><ins>20.5=
..11</ins></a>, Comparison with T</em>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;=
T&gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const optiona=
l&lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const optiona=
l&lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;, cons=
t optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;,=
 const optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;,=
 const optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&=
gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const optional&=
lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const optional&=
lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, const =
optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, c=
onst optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, c=
onst optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&=
gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;(const optional&=
lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;(const optional&=
lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  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 T&amp;, c=
onst optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp;, c=
onst optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const option=
al&lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const option=
al&lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;, con=
st optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;=
, const optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;=
, const optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&=
gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const optional&=
lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const optional&=
lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, const =
optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, c=
onst optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, c=
onst optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const option=
al&lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const option=
al&lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;, con=
st optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;=
, const optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;=
, const optional&lt;const T&amp;&gt;&amp;);</ins>
 =20
  // <em><a href=3D"#optional.specalg"><del>20.5.11</del><ins>20.5.12</ins>=
</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;=20
    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;=20
    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; mak=
e_optional(T&amp;&amp;);
 =20
  // <em><a href=3D"#optional.hash"><del>20.5.12</del><ins>20.5.13</ins></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>template &lt;class T&gt; struct hash&lt;optional&lt;T&amp;&gt;&gt;;<=
/ins>
  </pre>
 </blockquote>
=20
  <p>In the same clause, in the last sentence change:</p>

<blockquote class=3D"std">
 <p>A program that necessitates the instantiation of template <code>optiona=
l</code> for an <del>lvalue reference or </del>rvalue reference type, or fo=
r types <code>emplace_t</code> or <code>nullopt_t</code>, or a possibly cv-=
qualified reference to types <code>emplace_t</code> or <code>nullopt_t</cod=
e> is ill-formed.</p>
</blockquote>

<p>After clause 2.5.4 [optional.object] insert clause 20.5.5 [optional.lref=
] (clause [optional.inplace] becomes 20.5.6) </p>
<blockquote class=3D"stdins">
<h4><a name=3D"optional.lref">20.5.5 <code>optional</code> for lvalue refer=
ence types <span style=3D"float:right">[optional.lref]</span></a></h4>


<pre>
namespace std {
namespace experimental {

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

    // <em><a href=3D"#optional.lref.ctor">20.5.5.1</a>, construction/destr=
uction</em>
    constexpr optional() noexcept;
    constexpr optional(nullopt_t) noexcept;
    constexpr optional(T&amp;) noexcept;
    constexpr optional(T&amp;&amp;) =3D delete;
    constexpr optional(const optional&amp;) noexcept;<!--
    template &lt;class U&gt; constexpr optional(const optional&lt;U&amp;&gt=
;&amp;) noexcept;-->
    constexpr explicit optional(emplace_t, T&amp;) noexcept;
    constexpr explicit optional(emplace_t, T&amp;&amp;) =3D delete;
    ~optional() =3D default;

    // <em><a href=3D"#optional.lref.mutate">20.5.5.2</a>, mutation</em>
    optional&amp; operator=3D(nullopt_t) noexcept;<!--
    optional&amp; operator=3D(const optional&amp;) noexcept;
    optional&amp; operator=3D(optional&amp;&amp;) noexcept;-->
    template &lt;class U&gt; optional&amp; operator=3D(U&amp;&amp;) noexcep=
t;
    template &lt;class U&gt; optional&amp; operator=3D(U&amp;&amp;) noexcep=
t =3D delete;
    optional&amp; emplace(T&amp;) noexcept;
    optional&amp; emplace(T&amp;&amp;) =3D delete;

    // <em><a href=3D"#optional.lref.swap">20.5.5.3</a>, swap</em>
    void swap(optional&amp; rhs) noexcept;

    // <em><a href=3D"#optional.lref.observe">20.5.5.4</a>, observers</em>
    constexpr T* operator-&gt;() const;
    constexpr T&amp; operator*() const;
    constexpr explicit operator bool() const noexcept;
    constexpr T&amp; value() const;
    template &lt;class U&gt; constexpr typename decay&lt;T&gt;::type value_=
or(U&amp;&amp;) const&amp;;

  <var>private:</var>
    <var>T* ref;  //</var> <em>exposition only</em>
  };

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

  <p>Engaged instances of <code>optional&lt;T&gt;</code> where <code>T</cod=
e> is of lvalue reference type, refer to objects of type <code>std::remove_=
reference&lt;T&gt;::type</code>, but their life-time is not connected to th=
e life-time of the referred to object. Destroying or disengageing the optio=
nal object does not affect the state of the referred to object.</p>
 =20
  <p>Member <code><var>ref</var></code> is provided for exposition only. Im=
plementations need not provide this member. If <code><var>ref</var> =3D=3D =
nullptr</code>, optional object is disengaged; otherwise <code><var>ref</va=
r></code> points to a valid object.
  </p>

  <p>In several places in this Clause the expression <code>static_addressof=
(<var>v</var>)</code> is used. This expression is defined as follows. If ty=
pe <code>typename decay&lt;decltype(<var>v</var>)&gt;::type</code> is a use=
r-defined class type with overloaded <code>operator-&gt;</code>:
  </p>
 =20
  <pre>addressof(<var>v</var>)</pre>
 =20
  <p>otherwise:</p>
 =20
  <pre>&amp;(<var>v</var>)</pre>
 =20
 =20
=20
=20
=20
<h5><a name=3D"optional.lref.ctor">20.5.5.1 Construction and destruction <s=
pan style=3D"float:right">[optional.lref.ctor]</span></a></h5>


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

  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Constructs a disengaged <code>optional</code> =
object by initializing <code><var>ref</var></code> with <code>nullptr</code=
>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D false</code>.<=
/p></dd>
    <dt>Remarks:</dt> <dd><p>These constructors shall be <code>constexpr</c=
ode> constructors (7.1.5).</p></dd>
  </dl>

  <p class=3D"function">
  <code>constexpr optional&lt;T&amp;&gt;::optional(T&amp; <var>v</var>) noe=
xcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Constructs an engaged <code>optional</code> ob=
ject by initializing <code><var>ref</var></code> with <code>static_addresso=
f(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true &amp;&amp=
; static_addressof(*(*this)) =3D=3D static_addressof(<var>v</var>)</code>.<=
/p></dd>
    <dt>Remarks:</dt> <dd><p>Unless <code>T</code> is a user-defined class =
type with overloaded <code>operator-&gt;</code>, this constructor shall be =
a <code>constexpr</code> constructor.</p></dd>
  </dl>=20
 =20
 =20
  <p class=3D"function">
  <code>constexpr optional&lt;T&amp;&gt;::optional(const optional&amp; <var=
>rhs</var>) noexcept;<!--<br>template &lt;class U&gt; optional&lt;T&amp;&gt=
;::optional(const optional&lt;U&amp;&gt;&amp; <var>rhs</var>) noexcept;--><=
/code>
  </p>

 =20
  <dl class=3D"attribute">
    <!--<dt>Requires:</dt> <dd><p><code>is_base_of&lt;T, U&gt;::value =3D=
=3D true</code>, and <code>is_convertible&lt;U&amp;, T&amp;&gt;::value</cod=
e> is <code>true</code>.</p></dd>-->
    <dt>Effects:</dt> <dd><p>If <code><var>rhs</var></code> is disengaged, =
initializes <code><var>ref</var></code> with <code>nullptr</code>; otherwis=
e, constructs an engaged object by initializing <code><var>ref</var></code>=
 with <code>static_addressof(*<var>rhs</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) =3D=3D tr=
ue</code>, then <code>bool(*this) =3D=3D true &amp;&amp; static_addressof(*=
(*this)) =3D=3D static_addressof(*<var>rhs</var>)</code>; otherwise <code>b=
ool(*this) =3D=3D false</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This constructor shall be a <code>constexpr</c=
ode> constructor.</p></dd>
  </dl>  =20
 =20
  <p class=3D"function">
  <code>constexpr explicit optional&lt;T&amp;&gt;::optional(emplace_t, T&am=
p; <var>v</var>) noexcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Constructs an engaged <code>optional</code> ob=
ject by initializing <code><var>ref</var></code> with <code>static_addresso=
f(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true &amp;&amp=
; static_addressof(*(*this)) =3D=3D static_addressof(<var>v</var>)</code>.<=
/p></dd>
    <dt>Remarks:</dt> <dd><p>Unless <code>T</code> is a user-defined class =
type with overloaded <code>operator-&gt;</code>, this constructor shall be =
a <code>constexpr</code> constructor.</p></dd>
  </dl>=20
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;::~optional() =3D default;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>No effect. This destructor shall be a trivial =
destructor.</p></dd>
  </dl> =20
 =20
 =20
<h5><a name=3D"optional.lref.mutate">20.5.5.2 Mutation <span style=3D"float=
:right">[optional.lref.mutate]</span></a></h5>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;&amp; optional&lt;T&amp;&gt;::operator=3D(nul=
lopt_t) noexcept;</code>
  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code>nullptr</code>. If <code><var>ref</var></code> was non-null ini=
tially, the object it referred to is unaffected.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D false</code>.<=
/p></dd>
  </dl><!--
=20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;&amp; optional&lt;T&amp;&gt;::operator=3D(con=
st optional&amp; <var>v</var>) noexcept;<br>optional&lt;T&amp;&gt;&amp; opt=
ional&lt;T&amp;&gt;::operator=3D(optional&amp;&amp; <var>v</var>) noexcept;=
</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code><var>v</var>.<var>ref</var></code>. If <code><var>ref</var></co=
de> was non-null initially, the object it referred to is unaffected.</p></d=
d>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>(!*this) ? (!<var>v</var>) : bool=
(<var>v</var>) &amp;&amp; static_addressof(*(*this)) =3D=3D static_addresso=
f(*<var>v</var>)</code>.</p></dd>
  </dl>-->
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&amp;&gt;&amp; optional&lt;T&=
amp;&gt;::operator=3D(U &amp;&amp; <var>v</var>) noexcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code><var>v</var>.<var>ref</var></code>. If <code><var>ref</var></co=
de> was non-null initially, the object it referred to is unaffected.</p></d=
d>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>(!*this) ? (!<var>v</var>) : bool=
(<var>v</var>) &amp;&amp; static_addressof(*(*this)) =3D=3D static_addresso=
f(*<var>v</var>)</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This function shall not participate in overloa=
d resolution unless <code>is_same&lt;typename decay&lt;U&gt;::type, optiona=
l&lt;T&amp;&gt;&gt;::value</code> is <code>true</code>. </dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&amp;&gt;&amp; optional&lt;T&=
amp;&gt;::operator=3D(U &amp;&amp; <var>v</var>) noexcept =3D delete;</code=
>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Remarks:</dt> <dd><p>This function shall not participate in overloa=
d resolution unless <code>is_same&lt;typename decay&lt;U&gt;::type, optiona=
l&lt;T&amp;&gt;&gt;::value</code> is <code>false</code>. </dd>
  </dl>
 =20
 =20
  <p>[<i>Note:</i> The reson to provide these two overloads is to enable no=
tation <code>oj =3D {}</code> and <code>oj =3D {j}</code> and copy/move ass=
ignment, but to prevent notation <code>oj =3D j</code>, where <code>oj</cod=
e> is of type <code>optional&lt;T&amp;&gt;</code> and <code>j</code> is of =
type <code>T</code>. &mdash;<i>end note</i>]</p>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;&amp; optional&lt;T&amp;&gt;::emplace(T&amp; =
<var>v</var>) noexcept;</code>
  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code>static_addressof(<var>v</var>)</code>. If <code>*this</code> wa=
s engaged before the call the object it referred to is not affected.</p></d=
d>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true &amp;&amp=
; static_addressof(*(*this)) =3D=3D static_addressof(<var>v</var>)</code>.<=
/p></dd>
  </dl>
=20


<h5><a name=3D"optional.lref.swap">20.5.5.3 Swap <span style=3D"float:right=
">[optional.lref.observe]</span></a></h5>

  <p class=3D"function">
  <code>void optional&lt;T&amp;&gt;::swap(optional&amp; <var>rhs</var>) noe=
xcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Calls <code>swap(<var>ref</var>, <var>rhs</var=
>.<var>ref</var>)</code>.</p></dd>
  </dl>

 =20
 =20
 =20
<h5><a name=3D"optional.lref.observe">20.5.5.4 Observers <span style=3D"flo=
at:right">[optional.lref.observe]</span></a></h5>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T* optional&lt;T&amp;&gt;::operator-&gt;() const;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</p></dd=
>
    <dt>Returns:</dt> <dd><p><code><var>ref</var></code>.</p></dd>
    <dt>Throws:</dt> <dd><p>nothing.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T&amp; optional&lt;T&amp;&gt;::operator*() const;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</p></dd=
>
    <dt>Returns:</dt> <dd><p><code>*<var>ref</var></code></p></dd>
    <dt>Throws:</dt> <dd><p>nothing.</p></dd>
  </dl>
 =20
 =20
    <p class=3D"function">
  <code>constexpr T&amp; optional&lt;T&amp;&gt;::value() const;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code>*<var>ref</var></code>, if <code>bool(*t=
his) =3D=3D true</code>.</p></dd>
    <dt>Throws:</dt> <dd><p><code>bad_optional_access</code> if <code>bool(=
*this) =3D=3D false</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This function shall be a constexpr function.</=
p></dd>
  </dl>

 =20
  <p class=3D"function">
  <code>constexpr explicit optional&lt;T&amp;&gt;::operator bool() noexcept=
;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code><var>ref</var> !=3D nullptr</code></p></=
dd>
  </dl> =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; constexpr typename decay&lt;T&gt;::type op=
tional&lt;T&gt;::value_or(U&amp;&amp; <var>v</var>) const&amp;;</code>
  </p>
 =20
  <dl class=3D"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;ty=
pename decay&lt;T&gt;::type&gt;(std::forward&lt;U&gt;(<var>v</var>))</code>=
..</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization of the return value throws.</p></dd=
>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> =3D=3D true<=
/code> and exception is thrown durning the call to <code>T</code>'s constru=
ctor, the value of <code>*this</code> and <code><var>v</var></code> remains=
 unchanged. Otherwise, when exception is thrown durning the call to <code>T=
</code>'s constructor, the value of <code><var>*this</var></code> remains u=
nchanged and the state of <code><var>v</var></code> is determined by the ex=
ception safety quarantee of the selected <code>T</code>'s constructor</p></=
dd>
    <dt>Remarks:</dt> <dd><p>If the selected <code>T</code>'s constructor i=
s a <code>constexpr</code> constructor, this function shall be a <code>cons=
texpr</code> function.</p></dd>
  </dl>
 =20

</blockquote>

<p>In subclause [optional.comp_with_t] change: </p>

<blockquote class=3D"std">
<h4><a name=3D"optional.comp_with_t"><del>20.5.10</del><ins>20.5.11</ins> C=
omparison with <code>T</code> <span style=3D"float:right">[optional.comp_wi=
th_t]</span></a></h4>


  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const option=
al&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>template=
 &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;T&amp;&gt;=
&amp; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;=
class T&gt; constexpr bool operator=3D=3D(const optional&lt;const T&amp;&gt=
;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> =3D=3D =
<var>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;=
 <var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class =
T&gt; constexpr bool operator=3D=3D(const T&amp; <var>v</var>, const option=
al&lt;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt;=
 constexpr bool operator=3D=3D(const T&amp; <var>v</var>, const optional&lt=
;const T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> =3D=3D *=
<var>x</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>template &=
lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&amp;&gt;&amp=
; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;clas=
s T&gt; constexpr bool operator!=3D(const optional&lt;const T&amp;&gt;&amp;=
 <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> !=3D <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class T&=
gt; constexpr bool operator!=3D(const T&amp; <var>v</var>, const optional&l=
t;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt; con=
stexpr bool operator!=3D(const T&amp; <var>v</var>, const optional&lt;const=
 T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> !=3D *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
  <!-- -->
 =20
  <p class=3D"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><ins>template &=
lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&amp;&gt;&amp=
; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;clas=
s T&gt; constexpr bool operator&lt;(const optional&lt;const T&amp;&gt;&amp;=
 <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt; <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class T&=
gt; constexpr bool operator&gt;(const T&amp; <var>v</var>, const optional&l=
t;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt; con=
stexpr bool operator&gt;(const T&amp; <var>v</var>, const optional&lt;const=
 T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt; *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <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>);<br><ins>template &=
lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&amp;&gt;&amp=
; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;clas=
s T&gt; constexpr bool operator&gt;(const optional&lt;const T&amp;&gt;&amp;=
 <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &gt; <v=
ar>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class T&=
gt; constexpr bool operator&lt;(const T&amp; <var>v</var>, const optional&l=
t;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt; con=
stexpr bool operator&lt;(const T&amp; <var>v</var>, const optional&lt;const=
 T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt; *<v=
ar>x</var> : false</code>.</p></dd>
  </dl>
 =20
  <!-- -->
=20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>templat=
e &lt;class T&gt; constexpr bool operator&gt;=3D(const optional&lt;T&amp;&g=
t;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &l=
t;class T&gt; constexpr bool operator&gt;=3D(const optional&lt;const T&amp;=
&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var>  &gt;=
=3D <var>v</var> : false</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class=
 T&gt; constexpr bool operator&lt;=3D(const T&amp; <var>v</var>, const opti=
onal&lt;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&g=
t; constexpr bool operator&lt;=3D(const T&amp; <var>v</var>, const optional=
&lt;const T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt;=3D =
*<var>x</var> : false</code>.</p></dd>
  </dl>
  =20
  =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>templat=
e &lt;class T&gt; constexpr bool operator&lt;=3D(const optional&lt;T&amp;&g=
t;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &l=
t;class T&gt; constexpr bool operator&lt;=3D(const optional&lt;const T&amp;=
&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt;=3D=
 <var>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class=
 T&gt; constexpr bool operator&gt;=3D(const T&amp; <var>v</var>, const opti=
onal&lt;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&g=
t; constexpr bool operator&gt;=3D(const T&amp; <var>v</var>, const optional=
&lt;const T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt;=3D =
*<var>x</var> : true</code>.</p></dd>
  </dl>

</blockquote>

<p>In clause [optional.specalg] change: </p>


<blockquote class=3D"std">
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt;<br>
    &nbsp;&nbsp;constexpr optional&lt;<del><em>V</em></del><ins>typename de=
cay&lt;T&gt;::type</ins>&gt; make_optional(T&amp;&amp; <var>v</var>);</code=
>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt>=20
    <dd><p><code>optional&lt;<del><em>V</em></del><ins>typename decay&lt;T&=
gt;::type</ins>&gt;(std::forward&lt;T&gt;(<var>v</var>))</code><ins>, <br>w=
here <code><var>V</var></code> is defined as <code><em>X</em>&amp;</code> i=
f <code>T</code> equals <code>reference_wrapper&lt;X&gt;</code>; otherwise =
<code><var>V</var></code> is <code>typename decay&lt;T&gt;::type</code></in=
s>.</p></dd>
  </dl>

</blockquote>

<p>In clause [optional.hash] add: </p>

<blockquote class=3D"std">
<h4><a name=3D"optional.hash"><del>20.5.12</del><ins>20.5.13</ins> Hash sup=
port <span style=3D"float:right">[optional.hash]</span></a></h4>


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

  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p>the template specilaization <code>hash&lt;T&g=
t;</code> shall meet the requirements of class template <code>hash</code> (=
20.9.12).
      The template specilaization <code>hash&lt;optional&lt;T&gt;&gt;</code=
> shall meet the requirements of class template <code>hash</code>.=20
      For an object <code>o</code> of type <code>optional&lt;T&gt;</code>, =
if <code>bool(o) =3D=3D true</code>,=20
      <code>hash&lt;optional&lt;T&gt;&gt;()(o)</code> shall evaluate to the=
 same value as <code>hash&lt;T&gt;()(*o)</code>.</p></dd>=20
  </dl>
 =20
   <p class=3D"function">
  <ins><code>template &lt;class T&gt; struct hash&lt;optional&lt;T&amp;&gt;=
&gt;;</code></ins>
  </p>

  <dl class=3D"attribute">
    <dt><ins>Requires:</ins></dt> <dd><p><ins>the template specilaization <=
code>hash&lt;T&gt;</code> shall meet the requirements of class template <co=
de>hash</code> (20.9.12).
      The template specilaization <code>hash&lt;optional&lt;T&amp;&gt;&gt;<=
/code> shall meet the requirements of class template <code>hash</code>.=20
      For an object <code>o</code> of type <code>optional&lt;T&amp;&gt;</co=
de>, if <code>bool(o) =3D=3D true</code>,=20
      <code>hash&lt;optional&lt;T&amp;&gt;&gt;()(o)</code> shall evaluate t=
o the same value as <code>hash&lt;T&gt;()(*o)</code>.</ins></p></dd>=20
  </dl>
</blockquote>


<h2><a name=3D'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 o=
f this proposal can be found at <a href=3D"https://github.com/akrzemi1/Opti=
onal/">https://github.com/akrzemi1/Optional/</a>. Below we demonstrate how =
one can implement <code>optional</code>'s <code>constexpr</code> constructo=
rs 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 {
namespace experimental {

#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& v)  <em>// value ctor</em>
    : value_{v} {}

  ~optional_storage() =3D 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& v) : initialized_{true}, storage_{v} {}

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

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

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



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


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

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

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

<p>People in discussion group "ISO C++ Standard - Future Proposals" provide=
d numerous insightful suggestions: Vladimir Batov (who described and suppor=
ted the perfect forwarding constructor), Nevin Liber, Ville Voutilainen, Ri=
chard Smiths, Dave Abrahams, Chris Jefferson, Jeffrey Yasskin, Nikolay Ivch=
enkov, Matias Capeletto, Olaf van der Spek, Vincent Jacquet, Kazutoshi Sato=
da, Vicente J. Botet Escriba, R&oacute;bert D&aacute;vid, Vincent Jacquet, =
Luc Danton, 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 ove=
rloaded.</p>


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


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

<li>Fernando Cacciola, Boost.Optional library (<a href=3D'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=3D'http=
://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx'>http://msdn.microsoft.co=
m/en-us/library/1t3y8s4s.aspx</a></li>

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

<li>Jaakko J&auml;rvi, Boost Tuple Library (<a href=3D"http://www.boost.org=
/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html">http://www.boost.or=
g/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=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/=
papers/2011/n3248.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2=
011/n3248.pdf</a>)</li>

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

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



 =20

</body>
</html>

------=_Part_1783_16865104.1360935848759--

.


Author: =?UTF-8?Q?Andrzej_Krzemie=C5=84ski?= <akrzemi1@gmail.com>
Date: Fri, 15 Feb 2013 05:46:14 -0800 (PST)
Raw View
------=_Part_349_2402407.1360935974052
Content-Type: multipart/alternative;
 boundary="----=_Part_350_6931475.1360935974055"

------=_Part_350_6931475.1360935974055
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi everyone,
We have updated the proposal based on recent feedback. I enclose the latest=
=20
draft. It is also available at:
http://kojot.sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html
Changes include:

   1. Semantics of mixed relops for irregular types (thanks to Tony Van=20
   Eerd)
   2. Removed postconditions that would not be possible to achieve for=20
   irregular types (thanks to Tony Van Eerd)
   3. operator-> and most of operations on optional refs are not constexpr=
=20
   if our type has overloaded operator& (because there is no way to impleme=
nt=20
   a constexpr addressof) (thanks to Nicol Bolas)
   4. Added function value() that throws on disengaged optionals (thanks to=
=20
   Daniel Kr=FCgler)
  =20
We would appreciate your feedback, especially on the standardese.

Regards,
&rzej

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



------=_Part_350_6931475.1360935974055
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Hi everyone,<br>We have updated the proposal based on recent feedback. I en=
close the latest draft. It is also available at:<br><a href=3D"http://kojot=
..sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html">http://kojot.sg=
gw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html</a><br>Changes incl=
ude:<br><ol><li>Semantics of mixed relops for irregular types (thanks to To=
ny Van Eerd)<br></li><li>Removed postconditions that would not be possible =
to achieve for irregular types (thanks to Tony Van Eerd)</li><li>operator-&=
gt; and most of operations on optional refs are not constexpr if our type h=
as overloaded operator&amp; (because there is no way to implement a constex=
pr addressof) (thanks to Nicol Bolas)</li><li>Added function value() that t=
hrows on disengaged optionals (thanks to Daniel Kr=FCgler)<br></li></ol><p>=
We would appreciate your feedback, especially on the standardese.</p><p>Reg=
ards,<br>&amp;rzej<br></p>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_350_6931475.1360935974055--
------=_Part_349_2402407.1360935974052
Content-Type: text/html; charset=US-ASCII; name=tr2.optional.proposal.html
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment; filename=tr2.optional.proposal.html
X-Attachment-Id: 273e7425-33af-4e9c-a6e8-fcb5790f270e
Content-ID: <273e7425-33af-4e9c-a6e8-fcb5790f270e>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/htm=
l4/strict.dtd">
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html;charset=3DUS-ASCII">

<style type=3D"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;=20
  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 (Rev=
ision 2)</title>
<script src=3D"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.mi=
n.js" type=3D"text/javascript"> </script>
<script type=3D"text/javascript">$(function() {
    var next_id =3D 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 =3D node[0];
        while (cur) {
            if (cur.id) return curid;
            if (cur.tagName =3D=3D 'A' && cur.name)
                return cur.name;
            cur =3D 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 =3D [$("<ol/>")];
    $(':header').not('h1').each(function() {
        var header =3D $(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 =3D {H2: 0, H3: 1, H4: 2, H5: 3, H6: 4};
        var level =3D levels[this.tagName];
        if (typeof level =3D=3D '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 =3D=3D level) {
            // Add a <ol> to the previous level's last <li> and push
            // it into the array.
            var ol =3D $('<ol/>')
            toc[toc.length - 1].children().last().append(ol);
            toc.push(ol);
        }
        var header_text =3D header.text();
        toc[toc.length - 1].append(
            $('<li/>').append($('<a href=3D"#' + find_id(header) + '"/>')
                              .text(header_text)));
    });
    $('#toc').append(toc[0]);
})
</script>
</head>

<body>


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

<h1><a name=3D'title'>A proposal to add a utility class to represent option=
al objects (Revision&nbsp;3)</a></h1>

<p>
ISO/IEC JTC1 SC22 WG21 D3527
</p>

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

</address>


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

<p>Class template <code>optional&lt;T&gt;</code> proposed here is a type th=
at 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 curren=
tly stored, and if so, to access it. The interface is based on Fernando Cac=
ciola's Boost.Optional library<sup>[2]</sup>, shipping since March, 2003, a=
nd widely used. It requires no changes to core language, and breaks no exis=
ting code.</p>


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

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


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

<p>Changes since <a href=3D'http://www.open-std.org/jtc1/sc22/wg21/docs/pap=
ers/2012/n3406.html'>N3406=3D12-0096</a>:</p>
<ul>
  <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 t=
he 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 membe=
r function, renamed to <code>value_or</code>.</li>
  <li>Added member function <code>value</code> &mdash; an alternative to <c=
ode>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> an=
d <code>T</code> are now allowed.</li>
  <li>Removed reference implementation. We now only provide the implementat=
ion of the parts that we consider non-trivial.</li>
</ul>

<p>Changes since <a href=3D'http://www.open-std.org/jtc1/sc22/wg21/docs/pap=
ers/2005/n1878.htm'>N1878=3D05-0138</a>:</p>
<ul>
  <li>Revised wording; the changes are now relative to <a href=3D'http://ww=
w.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 opt=
ional.</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>, varia=
dic 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=3D'motivation'>Motivation and scope</a></h2>



<h3><a name=3D'motivation.missingval'>Missing return values</a></h3>


<p>It sometimes happens that a function that is declared to return values o=
f type <code>T</code> may not have a value to return. For one instance, con=
sider function <code>double sqrt(double)</code>. It is not defined for nega=
tive numbers. <em>The only way</em> function <b>sqrt</b> can find itself in=
 the situation where it cannot produce a value is when it is passed an inva=
lid argument, and it is possible for the caller to check if the argument is=
 valid. Therefore, such cases are dealt with by stating a function <em>prec=
ondition</em>. Violating the precondition may either result in undefined be=
havior, or (as it is the case for <code>std::vector::at</code>) may be defi=
ned to throw an exception. However, there are cases where we cannot know wh=
ether a function is able to return a value before we call it. For instance:=
=20

</p>

<pre>char c =3D stream.getNextChar();
int x =3D DB.execute("select x_coord from coordinates where point_id =3D 11=
2209");
</pre>

<p>In the first case, one could argue that <code>stream</code> could provid=
e another function for checking if the end of stream has been reached. But =
for the latter there is no easy way to make sure that the requested informa=
tion exists in the database before we request for it. Also, throwing an exc=
eption is not a good solution, because there is nothing unusual or erroneou=
s about not having found a record in the DB.
</p>



<h3><a name=3D'motivation.optargs'>Optional function arguments</a></h3>


<p>Sometimes it is useful to indicate that we are unable to, or do not want=
 to, provide one of the function arguments:</p>

<pre>void execute( function&lt;void(int)&gt; fa, function&lt;void(int)&gt; =
fb )
{
  int i =3D computeI();
  fa(i); <em>// implicit assumption that fa !=3D nullptr</em>
  if (fb) {
    fb(i);
  }
}
</pre>

<p>
Here, <code>fb</code> is an <em>optional</em> argument: it may simply repre=
sent no function. It typically requires an if-statement in function body. I=
t is often argued that such design can be replaced by providing two functio=
n overloads; however, this also is problematic: it adds certain duplication=
; may make the code less clear; the number of overloads increases expotenti=
ally with the number of optional parameters. <code>std::function</code> is =
already an optional type, however, other types in general are not; for inst=
ance, <code>std::pair&lt;int, std::string&gt;</code>.
</p>



<h3><a name=3D'motivation.trackinit'>Indicating a <em>null-state</em></a></=
h3>


<p>Some types are capable of storing a special <em>null-state</em> or <em>n=
ull-value</em>. It indicates that no 'meaningful' value has been assigned t=
o the object yet. It is then possible to query the object whether it contai=
ns a null-state, typically by invoking a contextual conversion to type <cod=
e>bool</code> or by checking for equality with <code>nullptr</code>. Exampl=
es of such types include: <code>std::unique_ptr</code> or <code>std::functi=
on</code>. This is convenient for instance when implementing a <q>lazy init=
ialization</q> optimization:

</p>

<pre>class Car=20
{
  mutable mutex m_;
  mutable unique_ptr&lt;const Engine&gt; engine_;
 =20
public:
  const Engine&amp; engine() const
  {
    lock_guard&lt;mutex&gt; _(m_);
    if (engine_ =3D=3D nullptr) {
      engine_.reset( new engine{engineParams()} );
    }
    return *engine_;
  }
};</pre>

<p>Other types, on the other hand, do not provide this capability. For inst=
ance, type <code>int</code> does not have a special value that would signal=
 the null-state. Any value of <code>int</code> is a valid value. The same g=
oes for user defined types: e.g., <code>Rational&lt;int&gt;</code> that imp=
lements a rational number by holding two <code>int</code>&rsquo;s represent=
ing a numerator and a denumerator.</p>

<p>For a similar example, consider this function for finding biggest <code>=
int</code>.</p>

<pre>
int find_biggest( const vector&lt;int&gt;& vec )
{
  int biggest; <em>// =3D what initial value??</em>
  for (int val : vec) {
    if (biggest &lt; val) {
      biggest =3D val;
    }
  }
  return biggest;
}=20
</pre>

<p>
What initial value should we assign to <code>biggest</code>? Not 0, because=
 the vector may hold negative values. We can use <code>std::numeric_limits&=
lt;int&gt;::min()</code>. But what if we want to make our function generic?=
 <code>std::numeric_limits</code> will not be specialized for any possible =
type. And what if the vector contains no elements? Shall we return minimum =
possible value? But how will the caller know if the size of the vector was =
zero, or if it was one with the minimum possible value?
</p>



<h3><a name=3D'motivation.guards'>Manually controlling the lifetime of scop=
e guards</a></h3>


<p>
Consider that you have to run three actions in the fixed order:
</p>
<pre>
void runAction1( Resource1 & );
void runAction2( Resource1 &, Resource2 & );
void runAction3( Resource2 & );
</pre>

<p>Of course, the two resources need to be acquired before using them and r=
eleased once we are done. This is how we would write it conceptually, if we=
 could assume that none of the operations ever fails or throws exceptions:<=
/p>

<pre>
Result run3Actions( Parameter param ) <i>// BAD!!</i>
{
  Resource1 res1;
  Resource2 res2;

  res1.acquire(param);       <i>// res1 scope</i>
  runAction1( res1 );        <i>//</i>
  res2.acquire(param);       <i>//    // res2 scope</i>
  runAction2( res1, res2 );  <i>//    //</i>
  res1.release();            <i>//    //</i>
  runAction3( res2 );        <i>      //</i>
  res2.release();            <i>      //</i>
}
</pre>

<p>Note that the scopes of the two resources <em>overlap.</em> we cannot cl=
early divide our function into nested scopes that correspond to lifetimes o=
f the resources. This example represents a real-life situation if you imagi=
ne that  <tt>runAction2</tt> is a critical operation, and <tt>runAction3</t=
t> (and perhaps <tt>runAction1</tt>) is logging, which we can even skip if =
it fails. </p>

<p>Of course, this code is unacceptable, because it does not take into acco=
unt that  acquire operations, and the three actions may fail. So, we want t=
o rewrite <tt>run3Actions</tt> using RAII idiom. Especially, that the libra=
ry author that provides the interface to the two resources also knows that =
RAII is probably always what we want and the only interface he provides are=
 scope guards:=20

</p>

<pre>
Result run3Actions( Parameter param ) <i>// selfish</i>
{
  Resource1Guard res1{param};
  Resource2Guard res2{param};

  runAction1( res1 );
  runAction2( res1, res2 );  =20
  runAction3( res2 );
}
</pre>

<p>This solution is somewhat elegant: you first acquire all resources that =
you will need, and once you are sure you have them all, you just run the ac=
tions. But it has one problem. Someone else will also need to use resources=
 we are acquiring. If they need it at the moment, but we own the resource r=
ight now, they will be locked, or thrown a refusal exception &mdash; they w=
ill be disturbed and delayed. Using a resource is a critical part of the pr=
ogram, and we should be only acquiring them for as short a period as possib=
le, for the sake of efficiency: not only our program's but of the entire op=
erating system. In our example above, we hold the ownership of <tt>res2</tt=
> while executing <tt>runAction1</tt>, which does not require the resource.=
 Similarly, we unnecessarily hold <tt>res1</tt> when calling <tt>runAction3=
</tt>. </p>

<p>So, how about this?</p>

<pre>
Result run3Actions( Parameter param ) <i>// slow and risky</i>
{
  {
    Resource1Guard res1{param};
    runAction1( res1 );
  }
  {
    Resource1Guard res1{param};
    Resource2Guard res2{param};
    runAction2( res1, res2 ); =20
  } =20
  {
    Resource2Guard res2{param};
    runAction3( res2 );
  }=20
}
</pre>

<p>It does not have the above-mentioned problem, but it introduces a differ=
ent one. Now we have to acquire the same resource two times in a row for tw=
o subsequent actions. Resource acquisition is critical, may lock us, may sl=
ow us down, may fail; calling it four times rather than two is wrong, espec=
ially that acquiring the same resource twice may give us different resource=
 properties in each acquisition, and it may not work for our actions.</p>=
=20

<h3><a name=3D'motivation.skipinit'>Skipping the expensive initialization</=
a></h3>

<p>Not all types provide a cheap default construction, even if they are sma=
ll in size. If we want to put such types in certain containers, we are risk=
ing paying the price of expensive default construction even if we want to a=
ssign a different value a second later.

</p>

<pre>ExpensiveCtor array[100]; <em>// 100 default constructions</em>
std::array&lt;ExpensiveCtor, 100&gt; arr; <em>// 100 default constructions<=
/em>
std::vector&lt;ExpensiveCtor&gt; vec(100); <em>// 100 default constructions=
</em>

</pre>

<h3><a name=3D'motivation.practise'>Current techniques for solving the prob=
lems</a></h3>


<p>The above mentioned problems are usually dealt with in C++ in three ways=
:</p>
<ol>
  <li>Using ad-hoc special values, like <code>EOF</code>, -1, 0, <code>nume=
ric_limits&lt;T&gt;::min()</code>.</li>

  <li>Using additional flags, like <code>pair&lt;T, bool&gt;</code>.</li>
  <li>Using pointers even if the object would have been passed or returned =
by value if it wouldn't be possibly absent.</li>
</ol>

<p>Technique (1) suffers from the lack of uniformity. Special value, like <=
code>EOF</code> is not something characteristic of the type (<code>int</cod=
e>), but of the particular function. Other function may need to use a diffe=
rent special value. The notable example is the standard function <code>atoi=
</code>, where 0 may indicate either an unsuccessful conversion, or a succe=
ssful conversion to value 0. And again, sometimes it is not even possible t=
o designate a value with a special meaning, because all values of a given t=
ype are equally likely to appear; for instance:

</p>

<pre>bool accepted =3D DB.execute("select accepted from invitations where i=
nvitation_id =3D 132208");</pre>

<p>Technique (2) is problematic because it requires both members of <code>p=
air</code> to be initialized. But one of the reasons we are not returning a=
 'normal' value from a function or do not want to initialize the object jus=
t yet, is because we did not know how to initialize it. In case of optional=
 <code>int</code> we could give it a rubbish value or leave it uninitialize=
d, but user defined types, including resources, may not provide a default c=
onstructor, and require some effort to perform initialization.</p>

<p>Technique (3) is general. It uses value <code>nullptr</code> to indicate=
 the special value. Users can unequivocally test if the value is indeed abs=
ent and being formally undefined behavior to access an object pointed to by=
 a null pointer. It also does not require initializing the object unnecessa=
rily. But it comes with different problems. Automatic objects created insid=
e functions cannot be returned by pointer. Thus it often requires creating =
objects in free store which may be more expensive than the expensive initia=
lization itself. It also requires manual memory management problems. The la=
tter problem can be dealt with by using <code>std::unique_ptr</code>, but t=
his is still a pointer, and we must abandon value semantics. Now we return =
a smart pointer: we have shallow copy and shallow comparison.</p>



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


<p>This proposal depends on library proposal <a href=3D"http://www.open-std=
..org/jtc1/sc22/wg21/docs/papers/2012/n3471.html">N3471</a>: it requires tha=
t Standard Library components <code>move</code>, <code>forward</code> and m=
ember functions of <code>initializer_list</code> are <code>constexpr</code>=
.. The paper has already been incorporated into the Working Draft of the Sta=
ndard <a href=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3=
485.pdf">N3485</a>. This proposal also depends on language proposal <a href=
=3D"www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2439.htm">N2439</a> (=
Rvalue references for <code>*this</code>). While the latter feature proposa=
l has been incorporated into C++11, we are not aware of any compiler implem=
entation. There is a risk that if compiler vendors do not implement it, the=
y 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=3D"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=3D'overview'>Overview of <code>optional</code></a></h2>


<p>The primary purpose of <code>optional&lt;T&gt;</code>'s interface is to =
be able to answer the quesiotn "do you contain a value of type <code>T</cod=
e>?", and iff the answer is "yes", to provide access to the contained value=
.. Conceptually, <code>optional</code> can be illustrated by the following s=
tructure.</p>

<pre>template &lt;typename T&gt;
struct optional
{
  bool is_initialized_;
  typename aligned_storage&lt;sizeof(T), alignof(T)&gt;::type storage_;
};</pre>

<p>Flag <code>is_initialized_</code> stores the information if <code>option=
al</code> has been assigned a value of type <code>T</code>. <code>storage_<=
/code> is a raw fragment of memory (allocated within <code>optional</code> =
object) capable of storing an object of type <code>T</code>. Objects in thi=
s storage are created and destroyed using placement <code>new</code> and ps=
eudo destructor call as member functions of <code>optional</code> are execu=
ted and based on the value of flag <code>is_initialized_</code>.</p>=20

<p>Alternatively, one can think of <code>optional&lt;T&gt;</code> as <code>=
pair&lt;bool, T&gt;</code>, with one important difference: if <code>first</=
code> is <code>false</code>, member <code>second</code> has never been even=
 initialized, even with default constructor or value-initialization.</p>

<p>The basic usage of <code>optional&lt;T&gt;</code> can be illustrated wit=
h the following example.</p>

<pre>optional&lt;int&gt; str2int(string);    <em>// converts int to string =
if possible</em>

int get_int_form_user()
{
  string s;

  for (;;) {
    cin &gt;&gt; s;
    optional&lt;int&gt; o =3D str2int(s); <em>// 'o' may or may not contain=
 an int</em>
    if (o) {                      <em>// does optional contain a value?</em=
>
      return *o;                  <em>// use the value</em>
    }
  }
}
</pre>


<h3><a name=3D'overview.interface'>Interface of <code>optional</code></a></=
h3>

<p>Default construction of <code>optional&lt;T&gt;</code> creates an an obj=
ect that stores no value of type <code>T</code>. No default constructor of =
<code>T</code> is called. <code>T</code> doesn't even need to be <code>Defa=
ultConstructible</code>. We say that thus created optional object is <em>di=
sengaged</em>. We use a pair of somewhat arbitrary names, 'engaged' and 'di=
sengaged'. A default-constructed optional object is initialized (the lifeti=
me of <code>optional&lt;T&gt;</code> has started), but it is disengaged (th=
e lifetime of the contained object has not yet started). Trying to access t=
he value of <code>T</code> in this state causes undefined behavior. The onl=
y thing we can do with a disengaged optional object is to query whether it =
is engaged, copy it, compare it with another <code>optional&lt;T&gt;</code>=
, or <em>engage</em> it.</p>

<pre>optional&lt;int&gt; oi;                 <em>// create disengaged objec=
t</em>
optional&lt;int&gt; oj =3D nullopt;       <em>// alternative syntax</em>
oi =3D oj;                          <em>// assign disengaged object</em>
optional&lt;int&gt; ok =3D oj;            <em>// ok is disengaged</em>

if (oi)  assert(false);           <em>// 'if oi is engaged...'</em>
if (!oi) assert(true);            <em>// 'if oi is disengaged...'</em>

if (oi !=3D nullopt) assert(false); <em>// 'if oi is engaged...'</em>
if (oi =3D=3D nullopt) assert(true);  <em>// 'if oi is disengaged...'</em>

assert(oi =3D=3D ok);                 <em>// two disengaged optionals compa=
re equal</em>
</pre>

<p>Tag <code>nullopt</code> represents the disengaged state of an optional =
object. This reflects our conceptual model of optional objects: <code>optio=
nal&lt;T&gt;</code> can be thought of as a <code>T</code> with one addition=
al value <code>nullopt</code>. Being disengaged is just another value of <c=
ode>T</code>. Thus, <code>optional&lt;unsigned&gt;</code> can be thought of=
 as a type with possible range of values <code>{nullopt, 0, 1, ...}</code>.=
 Value <code>nullopt</code> is always picked by the default constructor.</p=
>


<p>We can create engaged optional objects or engage existing optional objec=
ts by using converting constructor (from type <code>T</code>) or by assigni=
ng a value of type <code>T</code>.

<pre>optional&lt;int&gt; ol{1};              <em>// ol is engaged; its cont=
ained value is 1</em>
ok =3D 2;                           <em>// ok becomes engaged; its containe=
d value is 2</em>
oj =3D ol;                          <em>// oj becomes engaged; its containe=
d value is 1</em>

assert(oi !=3D ol);                 <em>// disengaged !=3D engaged</em>
assert(ok !=3D ol);                 <em>// different contained values</em>
assert(oj =3D=3D ol);                 <em>// same contained value</em>
assert(oi &lt; ol);                  <em>// disengaged &lt; engaged</em>
assert(ol &lt; ok);                  <em>// less by contained value</em>
</pre>

<p>Copy constructor and copy assignment of <code>optional&lt;T&gt;</code> c=
opies both the engaged/disangaged flag and the contained value, if it exist=
s. </p>

<pre>optional&lt;int&gt; om{1};              <em>// om is engaged; its cont=
ained value is 1</em>
optional&lt;int&gt; on =3D om;            <em>// on is engaged; its contain=
ed value is 1</em>
om =3D 2;                           <em>// om is engaged; its contained val=
ue is 2</em>
assert (on !=3D om);                <em>// on still contains 3. They are no=
t pointers</em>
</pre>

<p>We access the contained value using the indirection operator.</p>

<pre>int i =3D *ol;                      <em>// i obtains the value contain=
ed in ol</em>
assert(i =3D=3D 1);
*ol =3D 9;                          <em>// the object contained in ol becom=
es 9</em>
assert(*ol =3D=3D 9);
</pre>

<p>We also provide consistent <code>operator-&gt;</code>. Even though <code=
>optional</code> provides operators <code>-&gt;</code> and <code>*</code> i=
t is not a pointer. Unlike pointers, it has full value semantics: deep copy=
 construction, deep copy assignmet, deep equality and less-than comparison,=
 and constness propagation (from optional object to the contained value).</=
p>

<pre>int p =3D 1;
optional&lt;int&gt; op =3D p;
assert(*op =3D=3D 1);
p =3D 2;                        =20
assert(*op =3D=3D 1);                 <em>// value contained in op is separ=
ated from p</em>
</pre>

<p>The typical usage of <code>optional</code> requires an if-statement.</p>

<pre>if (ol)                     =20
  process(*ol);                   <em>// use contained value if present</em=
>
else
  processNil();                   <em>// proceed without contained value</e=
m>
 =20
if (!om)  =20
  processNil();
else =20
  process(*om);    =20
</pre>

=20
<p>If the action to be taken for disengaged <code>optional</code> is to pro=
ceed with the default value, we provide a convenient idiom: </p>=20

<pre>process(ol.value_or(0));      <em>// use 0 if ol is disengaged</em>
</pre>

<p>Sometimes the initialization from <code>T</code> may not do. If we want =
to skip the copy/move construction for <code>T</code> because it is too exp=
ensive or simply not available, we can call an 'emplacing' constructor, or =
function <code>emplace</code>.</p>

<pre>optional&lt;Guard&gt; oga;                     <em>// Guard is non-cop=
yable (and non-moveable)</em>    =20
optional&lt;Guard&gt; ogb(emplace, "res1");    <em>// initialzes the contai=
ned value with "res1" </em>          =20
optional&lt;Guard&gt; ogc(emplace);            <em>// default-constructs th=
e contained value</em>

oga.emplace("res1");                     <em>// initialzes the contained va=
lue with "res1" </em>=20
oga.emplace();                           <em>// destroys the contained valu=
e and </em>
                                         <em>// default-constructs the new =
one</em>
</pre>

<p>There are two ways to <em>disengage</em> a perhaps engaged optional obje=
ct:</p>

<pre>ok =3D nullopt;                      <em>// if ok was engaged calls T'=
s dtor</em>
oj =3D {};                           <em>// assigns a temporary disengaged =
optional</em>
oga =3D nullopt;                     <em>// OK: disengage the optional Guar=
d</em>
ogb =3D {};                          <em>// ERROR: Guard is not Moveable</e=
m>
</pre>


<p>Optional propagates constness to its contained value:</p>

<pre>const optional&lt;int&gt; c =3D 4;=20
int i =3D *c;                        <em>// i becomes 4</em>
*c =3D i;                            <em>// ERROR: cannot assign to const i=
nt&amp;</em>
</pre>



<h3><a name=3D'overview.except.safety'>Exception safety</a></h3>


<p>Type <code>optional&lt;T&gt;</code> is a wrapper over <code>T</code>, th=
us its exception safety guarantees depend on exception safety guarantees of=
 <code>T</code>. We expect (as is the case for the entire C++ Standard Libr=
ary) that destructor of <code>T</code> does not throw exceptions. Copy assi=
gnment of <code>optional&lt;T&gt;</code> provides the same exception guaran=
tee as copy assignment of <code>T</code> and copy constructor of <code>T</c=
ode> (i.e., the weakest of the two). Move assignment of <code>optional&lt;T=
&gt;</code> provides the same exception guarantee as move assignment of <co=
de>T</code> and move constructor of <code>T</code>. Member function <code>e=
mplace</code> provides basic guarantee: if exception is thrown, <code>optio=
nal&lt;T&gt;</code> becomes disengaged, regardless of its prior state. </p>



<h3><a name=3D'overview.usecases'>Advanced use cases</a></h3>


<p>With <code>optional&lt;T&gt;</code> problems described in Motivation sec=
tion can be solved as follows. For lazy initialization:</p>


<pre>class Car=20
{
  mutable mutex m_;
  mutable optional&lt;const Engine&gt; engine_;
  mutable optional&lt;const int&gt; mileage_
 =20
public:
  const Engine&amp; engine() const
  {
    lock_guard&lt;mutex&gt; _(m_);
    if (engine_ =3D=3D nullopt) engine_.emplace( engineParams() );
    return *engine_;
  }
 =20
  const int&amp; mileage() const
  {
    lock_guard&lt;mutex&gt; _(m_);
    if (!mileage_) mileage_ =3D initMileage();
    return *mileage_;
  }
};</pre>

<p>The algorithm for finding the greatest element in vector can be written =
as: </p>


<pre>
optional&lt;int&gt; find_biggest( const vector&lt;int&gt;& vec )
{
  optional&lt;int&gt; biggest;  <em>// initialized to not-an-int</em>
  for (int val : vec) {
    if (!biggest || *biggest &lt; val) {
      biggest =3D val;
      <em>// or: biggest.emplace(val);</em>
    }
  }
  return biggest;
}=20
</pre>


<p>Missing return values are naturally modelled by optional values:</p>

<pre>optional&lt;char&gt; c =3D stream.getNextChar();
optional&lt;int&gt; x =3D DB.execute("select ...");

storeChar( c.value_or('\0') );
storeCount( x.value_or(-1) );
</pre>

<p>Optional arguments can be implemented as follows:<p>

<pre>template &lt;typename T&gt;
T getValue( optional&lt;T&gt; newVal =3D nullopt )
{
  if (newVal) {
    cached =3D *newVal;    =20
  }
  return cached;     =20
}
</pre>

<p>Manually controlling the life-time of guard-like objects can be achieved=
 by emplacement operations and <code>nullopt</code> assignment:</p>

<pre>{
  optional&lt;Guard&gt; grd1{emplace, "res1", 1};   <em>// guard 1 initiali=
zed</em>
  optional&lt;Guard&gt; grd2;

  grd2.emplace("res2", 2);                     <em>// guard 2 initialized</=
em>
  grd1 =3D nullopt;                              <em>// guard 1 released</e=
m>

}                                              <em>// guard 2 released (in =
dtor)</em>
</pre>

<p>It is possible to use <code>tuple</code> and <code>optional</code> to em=
ulate multiple return valuse for types without default constructor:</p>

<pre>tuple&lt;Date, Date, Date&gt; getStartMidEnd();
void run(Date const&amp;, Date const&amp;, Date const&amp;);
<em>// ...</em>

optional&lt;Date&gt; start, mid, end;           <em>// Date doesn't have de=
fault ctor (no good default date)</em>

tie(start, mid, end) =3D getStartMidEnd();
run(*start, *mid, *end);=20
</pre>



<h3><a name=3D'overview.value_ptr_comparison'>Comparison with <code>value_p=
tr</code></a></h3>

<p><a href=3D'http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3339=
..pdf'>N3339</a><sup>[7]</sup> proposes a smart pointer template <code>value=
_ptr</code>. In short, it is a smart pointer with deep copy semantics. It h=
as a couple of features in common with <code>optional</code>: both contain =
the notion of optionality, both are deep-copyable. Below we list the most i=
mportant differences. </p>

<p><code>value_ptr</code> requires that the pointed-to object is allocated =
in the free store. This means that the <code>sizeof(value_ptr&lt;T&gt;)</co=
de> is fixed irrespective of <code>T</code>. <code>value_ptr</code> is 'pol=
ymorphic': object of type <code>value_ptr&lt;T&gt;</code> can point to an o=
bject of type <code>DT</code>, derived from <code>T</code>. The deep copy p=
reserves the dynamic type. <code>optional</code> requires no free store all=
ocation: its creation is more efficient; it is not "polymorphic".</p>

<p>Relational operations on <code>value_ptr</code> are shallow: only addres=
ses are compared. Relational operations on <code>optional</code> are deep, =
based on object's value. In general, <code>optional</code> has a well defin=
ed value: being disengaged and the value of contained value (if it exists);=
 this value is expressed in the semantics of equality operator. This makes =
<code>optional</code> a full value-semantic type. Comparison of <code>value=
_ptr</code> does not have this property: copy semantics are incompatible wi=
th equality comparison semantics: a copy-constructed <code>value_ptr</code>=
 does not compare equal to the original. <code>value_ptr</code> is not a va=
lue semantic type.=20
</p>



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


<p>The very minimum, ascetic interface for optional objects &mdash; apart f=
rom copy/move &mdash; could consist of two constructors and two functions:<=
/p>

<pre><em>// not proposed</em>
int i =3D 9;
optional&lt;int&gt; oi{engaged, i};          <em>// create engaged</em>
optional&lt;int&gt; oj{disengaged};          <em>// create disengaged</em>
if (oi.is_engaged()) {                 <em>// contains value?</em>
  oi.get_value();                      <em>// access the value</em>
}
</pre>

<p>The reason we provide different and richer interface is motivated by use=
rs' convenience, performance improvements, secondary goals we want to achei=
ve, and the attempt to standardize the existing practice.</p>

<p>Nearly every function in the interface of <code>optional</code> has rise=
n some controversies. Different people have different expectations and diff=
erent concerns and it is not possible to satisfy all conflicting requiremen=
ts. Yet, we believe that <code>optional</code> is so universally useful, th=
at it is worth standardizing it even at the expense of introducing a contor=
versial interface. The current proposal reflects our arbitrary choice of ba=
lance between unambiguousity, genericity and flexibility of the interface. =
The interface is based on Fernanndo Cacciola's <a href=3D'http://www.boost.=
org/doc/libs/1_48_0/libs/optional/doc/html/index.html'>Boost.Optional</a> l=
ibrary,<sup>[2]</sup> and the users' feedback. The library has been widely =
accepted, used and even occasionally recommended ever since.=20
</p>



<h3><a name=3D'rationale.model'>Conceptual model for <code>optional&lt;T&gt=
;</code></a></h3>


<p>Optional objects serve a number of purposes and a couple of conceptual m=
odels can be provided to answer the question what <code>optional&lt;T&gt;</=
code> really is and what interface it should provide. The three most common=
 models are:</p>

<ol>
  <li>Just a <code>T</code> with deferred initialization (and additional in=
terface to check if the object has already been initialized).</li>
  <li>A discriminated union of types <code>nullopt_t</code> and <code>T</co=
de>.</li>
  <li>A container of <code>T</code>'s with the maximum size of 1.</li>
</ol>

<p>While (1) was the first motivation for <code>optional</code>, we do not =
choose to apply this model, because type <code>optional&lt;T&gt;</code> wou=
ld not be a value semantic type: it would not model concept <code>Regular</=
code> (if C++ had concepts). In particular, it would be not clear whather b=
eing engaged or disengaged is part of the object's state. Programmers who w=
ish to adapt this view, and don't mind the mentioned difficulties, can stil=
l use <code>optional</code> this way:</p>

<pre>optional&lt;int&gt; oi;
initializeSomehow(oi);

int i =3D (*oi);
use(*oi);
(*oi) =3D 2;
cout &lt;&lt; (*oi);
</pre>

<p>Note that this usage does not even require to check for engaged state, i=
f one is sure that the object is engaged. One just needs to use indirection=
 operator consistently anywhere one means to use the initialized value. </p=
>

<p>Model (2) treats <code>optional&lt;T&gt;</code> as etier a value of type=
 <code>T</code> or value <code>nullopt</code>, allocated in the same storag=
e, along with the way of determining which of the two it is. The interface =
in this model requires operations such as comparison to <code>T</code>, com=
parison to <code>nullopt</code>, assignment and creation from either. It is=
 easy to determine what the value of the optional object is in this model: =
the type it stores (<code>T</code> or <code>nullopt_t</code>) and possibly =
the value of <code>T</code>. This is the model that we propose.</p>

<p>Model (3) treats <code>optional&lt;T&gt;</code> as a special case contai=
ner. This begs for a container-like interface: <code>empty</code> to check =
if the object is disengaged, <code>emplace</code> to engage the object, and=
 <code>clear</code> to disengage it. Also, the value of optional object in =
this model is well defined: the size of the container (0 or 1) and the valu=
e of the element if the size is 1. This model would serve our pupose equall=
y well. The choice between models (2) and (3) is to a certain degree arbitr=
ary. One argument in favour of (2) is that it has been used in practise for=
 a while in Boost.Optional.</p>

<p>Additionally, within the affordable limits, we propose the view that <co=
de>optional&lt;T&gt;</code> just extends the set of the values of <code>T</=
code> by one additional value <code>nullopt</code>. This is reflected in in=
itialization, assignment, ordering, and equality comparison with both <code=
>T</code> and <code>nullopt</code>.</p>

<pre>optional&lt;int&gt; oi =3D 0;
optional&lt;int&gt; oj =3D 1;
optional&lt;int&gt; ok =3D nullopt;

oi =3D 1;
oj =3D nullopt;
ok =3D 0;

oi =3D=3D nullopt;
oj =3D=3D 0;
ok =3D=3D 1;
</pre>


<h3><a name=3D'rationale.initialization_variants'>Initialization of <code>o=
ptional&lt;T&gt;</code></a></h3>


<p>In cases <code>T</code> is a value semantic type capable of storing <em>=
n</em> distinct values, <code>optional&lt;T&gt;</code> can be seen as an ex=
tended <code>T</code> capable of storing <em>n</em> + 1 values: these that =
<code>T</code> stores and <code>nullopt</code>. Any valid initialization sc=
heme must provide a way to put an optional object to any of these states. I=
n addition, some <code>T</code>s (like scope guards) are not <code>MoveCons=
tructible</code> and their optional variants still should constructible wit=
h any set of arguments that work for <code>T</code>. Two models have been i=
dentified as feasible.</p>

<p>The first requires that you initialize either by providing an already co=
nstructed <code>T</code> on the tag <code>nullopt</code>.</p>

<pre>string s{"STR"};

optional&lt;string&gt; os{s};                   <em> // requires Copyable&l=
t;T&gt;</em>
optional&lt;string&gt; ot =3D s;                  <em> // requires Copyable=
&lt;T&gt;</em>
optional&lt;string&gt; ou{"STR"};               <em> // requires Movable&lt=
;T&gt;</em>
optional&lt;string&gt; ov =3D string{"STR"};      <em> // requires Movable&=
lt;T&gt;</em>

optional&lt;string&gt; ow;                      <em> // disengaged</em>
optional&lt;string&gt; ox{};                    <em> // disengaged</em>
optional&lt;string&gt; oy =3D {};                 <em> // disengaged</em>
optional&lt;string&gt; oz =3D optional&lt;string&gt;{}; <em> // disengaged<=
/em>
optional&lt;string&gt; op{nullopt};             <em> // disengaged</em>
optional&lt;string&gt; oq =3D {nullopt};          <em> // disengaged</em>
</pre>

<p>In order to avoid calling move/copy constructor of <code>T</code>, we us=
e a 'tagged' placement constructor: </p>

<pre>optional&lt;Guard&gt; og;                       <em> // disengaged</em=
>
optional&lt;Guard&gt; oh{};                     <em> // disengaged</em>
optional&lt;Guard&gt; oi{emplace};              <em> // calls Guard{} in pl=
ace</em>
optional&lt;Guard&gt; oj{emplace, "arg"};       <em> // calls Guard{"arg"} =
in place</em>
</pre>

<p>The in-place constructor is not strictly necessary. It could be dropped =
because one can always achieve the same effect with a two-liner:</p>

<pre>optional&lt;Guard&gt; oj;                       <em> // start disengag=
ed</em>
oj.emplace("arg");                        <em> // now engage</em>
</pre>


<p>Notably, there are two ways to create a disengaged optional object: eith=
er by using the default constructor or by calling the 'tagged constructor' =
that takes <code>nullopt</code>. One of these could be safely removed and <=
code>optional&lt;T&gt;</code> could still be initialized to any state. </p>

<p>An alternative and also comprehensive initialization scheme is to have a=
 variadic perfect forwarding constructor that just forwards any set of argu=
ments to the constructor of the contained object of type <code>T</code>:

<pre><em>// not proposed</em>

optional&lt;string&gt; os{"STR"};               <em> // calls string{"STR"}=
 in place </em>
optional&lt;string&gt; ot{2, 'a'};              <em> // calls string{2, 'a'=
} in place</em>
optional&lt;string&gt; ou{};                    <em> // calls string{} in p=
lace</em>
optional&lt;string&gt; or;                      <em> // calls string{} in p=
lace</em>

optional&lt;Guard&gt; og;                       <em> // calls Guard{} in pl=
ace</em>
optional&lt;Guard&gt; oh{};                     <em> // calls Guard{} in pl=
ace</em>
optional&lt;Guard&gt; ok{"arg"};                <em> // calls Guard{"arg"} =
in place</em>

optional&lt;vector&lt;int&gt;&gt; ov;                 <em> // creates 0-ele=
ment vector</em>
optional&lt;vector&lt;int&gt;&gt; ow{};               <em> // creates 0-ele=
ment vector</em>
optional&lt;vector&lt;int&gt;&gt; ox{5, 6};           <em> // creates 5-ele=
ment vector</em>
optional&lt;vector&lt;int&gt;&gt; oy{{5, 6}};         <em> // creates 2-ele=
ment vector</em>
</pre>

<p>In order to create a disengaged optional object a special tag needs to b=
e used: either <code>nullopt</code> or <code>T</code>-based:</p>

<pre>optional&lt;int&gt; oi =3D nullopt;               <em> // disengaged <=
/em>
optional&lt;int&gt; oj =3D optional&lt;int&gt;::nullopt;<em> // disengaged<=
/em>
</pre>

<p>The latter, perfect forwarding variant, has an obvious advantage: whatev=
er you can initialize <code>T</code> with, you can also use it to initializ=
e <code>optional&lt;T&gt;</code> with the same semantics. It becomes even m=
ore useful in copy-initialization contexts like returning values from funct=
ions or passing arguments to functions:</p>

<pre>void putch(optional&lt;char&gt; oc);
putch('c');
putch({});       // char '\0'
putch(nullopt);  // no char
</pre>

<p> However, there are also certain problems with this model. First, there =
are exceptions to perfect forwarding: the tag <code>nullopt</code> is not f=
orwarded. Also, arguments of type <code>optional&lt;T&gt;</code> are not. T=
his becomes visible if <code>T</code> has such constructor:</p>

<pre>struct MyType
{
  MyType(optional&lt;MyType&gt;, int =3D 0);
  <em>// ... </em>
};
</pre>

<p>Also, in general, it is impossible to perfect-forward <code>initializer_=
list</code> as the special deduction rules are involved. Second problem is =
that <code>optional</code>'s default constructor creates an engaged object =
and therby triggers the value-initialization of <code>T</code>. It can be a=
rgued that this is only a psychological argument resulting from the fact th=
at other <code>std</code> components behave this way: containers, smart poi=
nters, <code>function</code>, and only because perfect forwarding was not a=
vailable at the time they were designed. However we anticipate that this ex=
pectation (valid or not) could cause bugs in programs. Consider the followi=
ng example:</p>

<pre>optional&lt;char&gt; readChar(Stream str)
{
  optional&lt;char&gt; ans;

  if (str.buffered()) {
    ans =3D str.readBuffered();
    clearBuffer();
  }
  else if (!str.end()) {
    ans =3D str.readChar();
  }

  return ans;
}=20
</pre>

<p>This is so natural to expect that <code>ans</code> is not engaged until =
we decide how we wan to engage it, that people will write this code. And in=
 the effect, if they cannod read the character, they will return an engaged=
 optional object with value <code>'\0'</code>. For these reasons we choose =
to propose the former model.</p>



<h3><a name=3D'rationale.default_ctor'>The default constructor</a></h3>


<p>This proposal provides a default constructor for <code>optional&lt;T&gt;=
</code> that creates a disengaged optional. We find this feature convenient=
 for a couple of reasons. First, it is because this behaviour is intuitive =
as shown in the above example of function <code>readChar</code>. It avoids =
a certain kind of bugs. Also, it satisfies other expectations. If I declare=
 <code>optional&lt;T&gt;</code>  as a non-static member, without any initia=
lizer, I may expect it is already initialized to the most natural, disengag=
ed, state regardless of whether <code>T</code> is <code>DefaultConstructibl=
e</code> or not. Also when declaring a global object, one could expect that=
 default constructor would be initialized during static-initialization (thi=
s proposal guarantees that). One could argue that the tagged constructor co=
uld be used for that puropse: </p>

<pre>
optional&lt;int&gt; global =3D nullopt;

struct X
{
  optional&lt;M&gt; m =3D nullopt;
};
</pre>

<p>However, sometimes not providing the tag may be the result of an inadver=
tent omission rather than concious decision. Because of our default constru=
ctor semantics we have to reject the initialization scheme that uses a perf=
ect forwarding constructor. Even if this is fine, one could argue that we d=
o not need a default constructor if we have a tagged constructor. We find t=
his redundancy convenient. For instance, how do you resize a <code>vector&l=
t;optional&lt;T&gt;&gt;</code> if you do not have the default constructor? =
You could type:</p>

<pre>vec.resize(size, nullopt);</pre>

<p>However, that causes first the creation of disengaged optional, and then=
 copying it multiple times. The use of copy constructor may incur run-time =
overhead and not be available for non-copyable <code>T</code>s. Also, it wo=
uld be not possible to use subscript operator in maps that hold optional ob=
jects.</p>

<p>Also, owing to this constructor, <code>optional</code> has a nice side-e=
ffect feature: it can make "almost <code>Regular</code>" types fully <code>=
Regular</code> if the lack of default constructor is the only thing they ar=
e missing. For instance consider type <code>Date</code> for representing ca=
lendar days: it is copyable movable, comparable, but is not <code>DefaultCo=
nstructible</code> because there is no meaningful default date. However, <c=
ode>optional&lt;Date&gt;</code> is <code>Regular</code> with a meaningful n=
ot-a-date state created by default.</p>



<h3><a name=3D'rationale.converting_ctor'>Converting constructor (from <cod=
e>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 =3D 1; <em>// works</em></pre>

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

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

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

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

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

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

<p>It has been argued that the constructor from <code>T</code> should be ma=
de explicit. It is not trivial to decide whether <code>T</code> should be c=
onvertiblle to <code>optional&lt;T&gt;</code>. This is not a clear situatio=
n where value of one type is stored in another type with greater resolution=
, or the situation where the same abstract value is stored in a type with d=
ifferent internal representation. On the other hand, given our conceptual m=
odel, <code>optional&lt;T&gt;</code> can store all values of <code>T</code>=
, so it is possible to apply a "lossless conversion". We decided to provide=
 the conversion, in order to (1) adhere to our conceptual model, and (2) to=
 enable the above convenience for function argument passing. The implicit c=
onversion naturally implies that <code>optional&lt;T&gt;</code>'s can be co=
mpared with <code>T</code>'s. This is discussed further down. </p>

<p>At some point we considered the possibility to make this constructor con=
ditionally explicit: make it explicit if <code>T</code> has an explicit cop=
y/move constructor, and make it non-explicit if <code>T</code> has a normal=
, non-explicit constructor. In the end, we find explicit copy constructor s=
o unusual that we do not find it worthwile to addressing it at the expense =
of compliicating the design.</p>

<h3><a name=3D'rationale.bool_conversion'>Contextual conversion to <code>bo=
ol</code> for checking engaged state</a></h3>


<p>Objections have been risen to this decision. When using <code>optional&l=
t;bool&gt;</code>, contextual conversion to <code>bool</code> (used for che=
cking the engaged state) might be confused with accessing the stored value.=
 while such mistake is possible, it is not precedent in the standard: types=
 <code>bool*</code>, <code>unique_ptr&lt;bool&gt;</code>, <code>shared_ptr&=
lt;bool&gt;</code> suffer from the same potential problem, and it was never=
 considered a show-stopper. Some have suggested that a special case in the =
interface should be made for <code>optional&lt;bool&gt;</code> specializati=
on. This was however rejected because it would break the generic use of <co=
de>optional</code>. </p>

<p>Some have also suggested that a member function like <code>is_initialize=
d</code> would more clearly indicate the intent than explicit conversion to=
 <code>bool</code>. However, we believe that the latter is a well establish=
ed idiom in C++ comunity as well as in the C++ Standard Library, and <code>=
optional</code> appears so fundamental a type that a short and familiar not=
ation appears more appropriate. It also allows us to combine the constructi=
on and checking for being engaged in a condition:
=09</p>

<pre>if (optional&lt;char&gt; ch =3D readNextChar()) {
  // ...
}
</pre>


<h3><a name=3D'rationale.nullopt'>Using tag <code>nullopt</code> for indica=
ting disengaged state</a></h3>

<p>The proposed interface uses special tag <code>nullopt</code> to indicate=
 disengaged <code>optional</code> state. It is used for construction, assig=
nment and relational operations. This might rise a couple of objections. Fi=
rst, it introduces redundancy into the interface:</p>

<pre>optional&lt;int&gt; opt1 =3D nullopt;=20
optional&lt;int&gt; opt2 =3D {};=20

opt1 =3D nullopt;
opt2 =3D {};

if (opt1 =3D=3D nullopt) ...
if (!opt2) ...
if (opt2 =3D=3D optional&lt;int&gt;{}) ...
</pre>

<p>On the other hand, there are usages where the usage of <code>nullopt</co=
de> cannot be replaced with any other convenient notation:</p>

<pre>void run(complex&lt;double&gt; v);
void run(optional&lt;string&gt; v);

run(nullopt);              <em>// pick the second overload</em>
run({});                   <em>// ambiguous</em>

if (opt1 =3D=3D nullopt) ...   <em>// fine</em>
if (opt2 =3D=3D {}) ...        <em>// illegal</em>

bool is_engaged( optional&lt;int&gt; o)
{
  return bool(o);          <em>// ok, but unclear</em>
  return o !=3D nullopt;     <em>// familiar</em>
}
</pre>

<p>While some situations would work with <code>{}</code> syntax, using <cod=
e>nullopt</code> makes the programmer's intention more clear. Compare these=
:</p>

<pre>optional&lt;vector&lt;int&gt;&gt; get1() {
  return {};
}

optional&lt;vector&lt;int&gt;&gt; get2() {
  return nullopt;
}

optional&lt;vector&lt;int&gt;&gt; get3() {
  return optional&lt;vector&lt;int&gt;&gt;{};
}
</pre>

<p>The usage of <code>nullopt</code> is also a consequence of the adapted m=
odel for optional: a discriminated union of <code>T</code> and <code>nullop=
t_t</code>. Also, a similar redundancy in the interface already exists in a=
 number of components in the standard library: <code>unique_ptr</code>, <co=
de>shared_ptr</code>, <code>function</code> (which use literal <code>nullpt=
r</code> for the same purpose); in fact, type requirements <code>NullablePo=
inter</code> require of types this redundancy. </p>

<p>Name "nullopt" has been chosen because it clearly indicates that we are =
interested in creating a null (disengaged) <code>optional&lt;T&gt;</code> (=
of unspecified type <code>T</code>). Other short names like "null", "naught=
", "nothing" or "none" (used in Boost.Optional library) were rejected becau=
se they were too generic: they did not indicate unambiguously that it was <=
code>optional&lt;T&gt;</code> that we intend to create. Such a generic tag =
<code>nothing</code> could be useful in many places (e.g., in types like <c=
ode>variant&lt;nothing_t, T, U&gt;</code>), but is outside the scope of thi=
s proposal.</p>
=20

<p>Note also that the definition of tag struct <code>nullopt</code> is more=
 complicated than that of other, similar, tags: it has explicitly deleted d=
efault constructor. This is in order to enable the reset idiom (<code>opt2 =
=3D {};</code>), which would otherwise not work because of ambiguuity when =
deducing the right-hand side argument.</p>


<h3><a name=3D'rationale.no_nullptr'>Why not <code>nullptr</code></a></h3>


<p>One could argue that since we have keyword <code>nullptr</code>, which a=
lready indicates a 'null-state' for a number of Standard Library types, not=
 necessarily pointers (class template <code>function</code>), it could be e=
qually well used for <code>optional</code>. In fact, the previous revision =
of this proposal did propose <code>nullptr</code>, however there are certai=
n difficulties that arise when the null-pointer literal is used.</p>

<p>First, the interface of <code>optional</code> is already criticized for =
resembling too much the interface of a (raw or smart) pointer, which incorr=
ectly suggests external heap storage and shallow copy and comparison semant=
ics. The "ptr" in "nullptr" would only increase this confusion. While <code=
>std::function</code> is not a pointer either, it also does not provide a c=
onfusing <code>operator-&gt;</code>, or equality comparison, and in case it=
 stores a function pointer it does shallow copying.</p>

<p>Second, using literal <code>nullptr</code> in <code>optional</code> woul=
d make it impossible to provide some of the natural and expected initializa=
tion and assignment semantics for types that themselves are nullable:</p>

<ul>
<li><code>optional&lt;int*&gt;</code>, </li>
<li><code>optional&lt;const char*&gt;</code>, </li>
<li><code>optional&lt;M C::*&gt;</code>, </li>
<li><code>optional&lt;function&lt;void(int)&gt;&gt;</code>, </li>
<li><code>optional&lt;NullableInteger&gt;</code>,</li>
<li><code>optional&lt;nullptr_t&gt;</code>.</li>
</ul>

<p>Should the following initialization render an engaged or a disengaged op=
tional?</p>

<pre>optional&lt;int*&gt; op =3D nullptr;</pre>

<p>One could argue that if we want to initialize an engaged optional we sho=
uld indicate that explicitly: </p>

<pre>optional&lt;int*&gt; op{emplace, nullptr};</pre>

<p>But this argument would not work in general. One of the goals of the des=
ign of <code>optional</code> is to allow a seamless "optionalization" of fu=
nction arguments. That is, given the folowing function signature:</p>

<pre>void fun(T v) {
  process(v);
}
</pre>

<p>It should be possible to change the signature and the implementation to:=
</p>

<pre>void fun(optional&lt;T&gt; v) {
  if (v) process(*v);
  else   doSthElse();
}
</pre>

<p>and expect that all the places that call function <code>fun</code> are n=
ot affected. But if <code>T</code> happens to be <code>int*</code> and we o=
ccasionally pass value <code>nullptr</code> to it, we will silently change =
the intended behavior of the refactoring: because it will not be the pointe=
r that we null-initialize anymore but a disengaged optional.</p>

<p>Note that this still does not save us from the above problem with refact=
oring function <code>fun</code> in case where <code>T</code> happens to be =
<code>optional&lt;U&gt;</code>, but we definately limit the amount of surpr=
ises.</p>

<p>In order to avoid similar problems with tag <code>nullopt</code>, instan=
tiating template <code>optional</code> with types <code>nullopt_t</code> an=
d <code>emplace_t</code> is prohibitted.</p>

<p>There exist, on the other hand, downsides of introducing a special token=
 in place of <code>nullptr</code>. The number of ways to indicate the 'null=
-state' for different library components will grow: you will have <code>NUL=
L</code>, <code>nullptr</code>, <code>nullopt</code>. New C++ programmers w=
ill ask "which of these should I use now?" What guidelines should be provid=
ed? Use only <code>nullptr</code> for pointers? But does it mean that we sh=
ould use <code>nullopt</code> for <code>std::function</code>? Having only o=
ne way of denoting null-state, would make the things easier, even if "ptr" =
suggests a pointer. </p>


<h3><a name=3D'rationale.t_based_none'>Why not a tag dependent on <code>T</=
code>? </a></h3>


<p>It has been suggested that instead of 'typeless' <code>nullopt</code> a =
tag nested in class <code>optional</code> be used instead:</p>

<pre>optional&lt;int&gt; oi =3D optional&lt;int&gt;::nullopt;
</pre>

<p>This has several advantages. Namespace <code>std</code> is not polluted =
with an additional <code>optional</code>-specific name. Also, it resolves c=
ertain ambiguities when types like <code>optional&lt;<code>optional&lt;T&gt=
;</code>&gt;</code> are involved:</p>

<pre>optional&lt;optional&lt;int&gt;&gt; ooi =3D optional&lt;int&gt;::nullo=
pt;           <em>// engaged</em>
optional&lt;optional&lt;int&gt;&gt; ooj =3D optional&lt;optional&lt;int&gt;=
&gt;::nullopt; <em>// disengaged</em>
</pre>

<pre>void fun(optional&lt;string&gt;);
void fun(optional&lt;int&gt;);

fun(optional&lt;string&gt;::nullopt); <em>// unambiguous: a typeless nullop=
t would not do</em>=20
</pre>

<p>Yet, we choose to propose a typeless tag because we consider the above p=
roblems rare and a typeless tag offers a very short notation in other cases=
:</p>

<pre>optional&lt;string&gt; fun()
{
  optional&lt;int&gt oi =3D nullopt;  <em>// no ambiguity</em>
  oi =3D nullopt;                <em>// no ambiguity</em>
  <em>// ...</em>
  return nullopt;              <em>// no ambiguity</em>
}
</pre>

<p>If the typeless tag does not work for you, you can always use the follow=
ing construct, although at the expense of invoking a (possibly elided) move=
 constructor:</p>=20

<pre>optional&lt;optional&lt;int&gt;&gt; ooi =3D optional&lt;int&gt;{};    =
       <em>// engaged</em>
optional&lt;optional&lt;int&gt;&gt; ooj =3D optional&lt;optional&lt;int&gt;=
&gt;{}; <em>// disengaged</em>
</pre>

<pre>void fun(optional&lt;string&gt;);
void fun(optional&lt;int&gt;);

fun(optional&lt;string&gt;{}); <em>// unambiguous</em>=20
</pre>

<h3><a name=3D'rationale.access'>Accessing the contained value</a></h3>


<p>It was chosen to use indirection operator because, along with explicit c=
onversion to <code>bool</code>, it is a very common pattern for accessing a=
 value that might not be there: </p>
<pre>if (p) use(*p);</pre>

<p>This pattern is used for all sort of pointers (smart or dumb), and it cl=
early indicates the fact that the value may be missing and that we return a=
 reference rather than a value.  The indirection operator has risen some ob=
jections because it may incorrectly imply that <code>optional</code> is a (=
possibly smart) pointer, and thus provides shallow copy and comparison sema=
ntics. All library components so far use indirection operator to return an =
object that is not part of the pointer's/iterator's value. In contrast, <co=
de>optional</code> indirects to the part of its own state. We do not consid=
er it a problem in the design; it is more like an unprecedented usage of in=
direction operator.  We believe that the cost of potential confusion is ove=
rweighed by the benefit of an easy to grasp and intuitive interface for acc=
essing the contained value. </p>

<p>We do not think that providing an implicit conversion to <code>T</code> =
would be a good choice. First, it would require different way of checking f=
or the empty state; and second, such implicit conversion is not perfect and=
 still requires other means of accessing the contained value if we want to =
call a member function on it.</p>

<p>Using the indirection operator for a disengaged object is an undefined b=
ehavior. This behavior offers maximum runtime performance. In addition to i=
ndirection operator, we provide member function <code>value</code> that ret=
urns a reference to to the contained value if one exists or throws an excep=
tion (derived from <code>logic_error</code>) otherwise:</p>

<pre>void interact()
{
  std::string s;
  cout &lt;&lt; "enter number ";
  cin &gt;&gt; s;
  optional&lt;int&gt; oi =3D str2int(s);
 =20
  try {
    process_int(oi.value());
  }
  catch(bad_optional_access const&amp;) {
    cout &lt;&lt; "this was not a number";
  }
}</pre>



<h3><a name=3D'rationale.relops'>Relational operators</a></h3>


<p>One of the design goals of <code>optional</code> is that objects of type=
 <code>optional&lt;T&gt;</code> should be valid elements in STL containers =
 and usable with STL algorithms (at least if objects of type <code>T</code>=
 are). Equality comparison is essential for <code>optional&lt;T&gt;</code> =
to model concept <code>Regular</code>. C++ does not have concepts, but bein=
g regular is still essential for the type to be effectively used with STL. =
Ordering is essential if we want to store optional values in ordered associ=
ative containers. A number of ways of including the disengaged state in com=
parisons have been suggested. The ones proposed, have been crafted such tha=
t the axioms of equivalence and strict weak ordering are preserved: disenga=
ged <code>optional&lt;T&gt;</code> is simply treated as an additional and u=
nique value of <code>T</code> equal only to itself; this value is always co=
mpared as less than any value of <code>T</code>:</p>

<pre>optional&lt;unsigned&gt; o0{0};
optional&lt;unsigned&gt; o1{1};
optional&lt;unsigned&gt; oN{nullopt};

assert (oN &lt; o0);
assert (o0 &lt; o1);
assert (!(oN  &lt; oN));
assert (!(o1 &lt; o1));

assert (oN !=3D o0);
assert (o0 !=3D o1);
assert (oN =3D=3D oN);
assert (o0 =3D=3D o0);
</pre>

<p>Given that both <code>nullopt_t</code> and <code>T</code> are implicitly=
 convertible to <code>optional&lt;T&gt;</code>, this implies the existence =
and semantics of mixed comparison between <code>optional&lt;T&gt;</code> an=
d <code>T</code>, as well as between <code>optional&lt;T&gt;</code> and <co=
de>nullopt_t</code>:</p>

<pre>assert (oN =3D=3D nullopt);
assert (o0 !=3D nullopt);
assert (oN !=3D 1);
assert (o1 =3D=3D 1);

assert (oN &lt; 1);
assert (o0 &gt; nullopt);
</pre>

<p>Although it is difficult to imagine any practical use case of ordering r=
elation between <code>optional&lt;T&gt;</code> and <code>nullopt_t</code>, =
we still provide it for completness's sake</p>

<p>The mixed relational operators, especially these representing order, bet=
ween <code>optional&lt;T&gt;</code> and <code>T</code> have been accused of=
 being dangerous. In code examples like the following, it may be unclear if=
 the author did not really intend to compare two <code>T</code>'s. </p>

<pre>auto count =3D get_optional_count();
if (count &lt; 20) {}                        <em>// or did you mean: *count=
 &lt; 20 ?</em>
if (count =3D=3D nullopt || *count &lt; 20) {}   <em>// verbose, but unambi=
guous</em>
</pre>

<p>Given that <code>optional&lt;T&gt;</code> is comparable and implicitly c=
onstructible from <code>T</code>, the mixed comparison is there already. We=
 would have to artificially create the mixed overloads only for them to cau=
se controlled compilation errors. A consistent approach to prohibiting mixe=
d relational operators would be to also prohibit the convesion from <code>T=
</code> or to also prohibit homogenous relational operators for <code>optio=
nal&lt;T&gt;</code>; we do not want to do either, for other reasons discuss=
ed in this proposal. Also, mixed relational operations are available in Boo=
st.Optional and were found useful by the users. Mixed operators come as som=
ething natural when we consider the model "<code>T</code> with one addition=
al value".</p>

<p>For completeness sake, we also provide ordering relations between <code>=
optional&lt;T&gt;</code> and <code>nullopt_t</code>, even though we see no =
practical use case for them:</p>

<pre>bool test(optional&lt;int&gt; o)
{
  assert (o &gt;=3D nullopt);    <em>// regardless of o's state</em>
  assert (!(o &lt; nullopt));  <em>// regardless of o's state</em>
  assert (nullopt &lt;=3D o);    <em>// regardless of o's state</em>=20
  return (o &gt; nullopt);     <em>// o !=3D nullopt is cleaner</em>
}</pre>

<p>This is similar to comparing values of type <code>unsigned int</code> wi=
th 0:</p>

<pre>bool test(unsigned int i)
{
  assert (i &gt;=3D 0);          <em>// regardless of i's state</em>
  assert (0 &lt;=3D i);          <em>// regardless of i's state</em>
  return (i &gt; 0);           <em>// i !=3D 0 is cleaner</em>
}</pre>


<p>There exist two ways of implementing <code>operator&gt;</code> for optio=
nal objects:</p>

<pre>bool operator&gt;(const optional&lt;T&gt;&amp; x, const optional&lt;T&=
gt;&amp; y)
{
  return (!x) ? false : (!y) ? true : *x &gt; *y;  <em>// use T::operator&g=
t;</em>
}

bool operator&gt;(const optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&a=
mp; y)
{
  return y &lt; x;                                 <em>// use optional&lt;T=
&gt;::operator&lt;</em>
}
</pre>

<p>In case <code>T::operator&gt;</code> and <code>T::operator&lt;</code> ar=
e defined consistently, both above implementations are equivalent. If the t=
wo operators are not consistent, the  choice of implementation makes a diff=
erence. For homogenous relational operations (between two <code>optional&lt=
;T&gt;</code>s), we chose the former specification. That is, <code>T::opera=
tor&gt;</code> may not even be defined if order for <code>optional&lt;T&gt;=
::operator&lt;</code> to work. This is consistent with a similar choice for=
 <code>std::tuple</code>. For heterogenous relational operations (between <=
code>optional&lt;T&gt;</code> and <code>T</code>), we choose the latter spe=
cification. There is no precedent for mixed relops in the Standard Library,=
 so we do not feel we are making it wrong. The latter specification is more=
 close to the model "<code>optional&lt;T&gt;</code> is like <code>T</code>.=
" For homogenous rel-ops we want to stick to the rules employed by the Stan=
dard.</p>



<h3><a name=3D'rationale.resetting'>Resetting the optional value</a></h3>


<p>Assigning the value of type <code>T</code> to <code>optional&lt;T&gt;</c=
ode> object results in doing two different things based on whether the opti=
onal object is engaged or not. If optional object is engaged, the contained=
 value is assigned a new value. If optional object is disengaged, it become=
s engaged using <code>T</code>'s copy/move constructor. This behavior is ba=
sed on a silent assumption that <code>T</code>'s copy/move constructor is c=
opying a value in a similar way to copy/move assignment. A similar logic ap=
plies to <code>optional&lt;T&gt;</code>'s copy/move assignment, although th=
e situation here is more complicated because we have two engaged/disengaged=
 states to be considered. This means that <code>optional&lt;T&gt;</code>'s =
assignment does not work (does not compile) if <code>T</code> is not assign=
able:</p>

<pre>optional&lt;const int&gt; oi =3D 1;  <em>// ok</em>
oi =3D 2;                      <em>// error</em>=20
oi =3D oi;                     <em>// error</em>=20
oi =3D nullopt;                <em>// ok</em>
</pre>

<p>There is an option to reset the value of optional object without resorti=
ng to <code>T</code>'s assignment:</p>

<pre>optional&lt;const int&gt; oj =3D 1;  <em>// ok</em>
oj.emplace(2);               <em>// ok</em>=20
</pre>

<p>Function <code>emplace</code> disengages the optional object if it is en=
gaged, and then just engages the object anew by copy-constructing the conta=
ined value. It is similar to assignment, except that it is guaranteed not t=
o use <code>T</code>'s assignment and provides only a basic exception saget=
y guarantee. In contrast, assignment may provide a stronger guarantee if <c=
ode>T</code>'s assignment does.</p>

<p>To sumarize, this proposal offers three ways of assigning a new containe=
d value to an optional object:</p>

<pre>optional&lt;int&gt; o;
o =3D make_optional(1);         <em>// copy/move assignment</em>
o =3D 1;                        <em>// assignment from T</em>
o.emplace(1);                 <em>// emplacement</em>=20
</pre>

<p>The first form of assignment is required to make <code>optional</code> a=
 regular object, useable in STL. We need the second form in order to reflec=
t the fact that <code>optional&lt;T&gt;</code> is a wrapper for <code>T</co=
de> and hence it should behave as <code>T</code> as much as possible. Also,=
 when <code>optional&lt;T&gt;</code> is viewed as <code>T</code> with one a=
dditional value, we want the values of <code>T</code> to be directly assign=
able to <code>optional&lt;T&gt;</code>. In addition, we need the second for=
m to allow the interoperability with function <code>std::tie</code> as show=
n above. The third option is required to be able to reset an optional non-a=
ssignable <code>T</code>.</p>

=20
<h3><a name=3D'rationale.emplace'>Tag <code>emplace</code></a></h3>

<p>This proposal provides an 'in-place' constructor that forwards (perfectl=
y) the arguments provided to <code>optional</code>'s constructor into the c=
onstructor of <code>T</code>. In order to trigger this constructor one has =
to use the tag struct <code>emplace</code>. We need the extra tag to disamb=
iguate certain situations, like calling <code>optional</code>'s default con=
structor and requesting <code>T</code>'s default construction:</p>

<pre>optional&lt;Big&gt; ob{emplace, "1"}; <em>// calls Big{"1"} in place (=
no moving)</em>
optional&lt;Big&gt; oc{emplace};      <em>// calls Big{} in place (no movin=
g)</em>
optional&lt;Big&gt; od{};             <em>// creates a disengaged optional<=
/em>
</pre>

<p>The name, suggested by Alberto Ganesh Barbati, is consistent with member=
 functions of containers (and <code>optional</code> itself), which also ind=
icate similar purpose. On the other hand, it may appear uncomfortable that =
<code>emplace</code> becomes overloaded in <code>std</code>: it is now a me=
mber function in many container type as well as a tag. If this is considere=
d a serious issue, the tag could be renamed to <code>in_place</code>.</p>


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

<p>Class template <code>optional</code> imposes little requirements on <cod=
e>T</code>: it has to be either an lvalue reference type, or a complete obj=
ect type satisfying the requirements of <code>Destructible</code>. It is th=
e particular operations on <code>optional&lt;T&gt;</code> that impose requi=
rements on <code>T</code>: <code>optional&lt;T&gt;</code>'s move constructo=
r requires that <code>T</code> is <code>MoveConstructible</code>, <code>opt=
ional&lt;T&gt;</code>'s copy constructor requires that <code>T</code> is <c=
ode>CopyConstructible</code>, and so on. This is because <code>optional&lt;=
T&gt;</code> is a wrapper for <code>T</code>: it should resemble <code>T</c=
ode> as much as possible. If <code>T</code> is <code>EqualityComparable</co=
de> then (and only then) we expect <code>optional&lt;T&gt;</code> to be <co=
de>EqualityComparable</code>. </p>



<h3><a name=3D'rationale.refs'>Optional references</a></h3>


<p>In this revision, optional references are presented as an auxiliary prop=
osal. The intention is that the Committee should have an option to accept o=
ptional values without optional references, if it finds the latter concept =
inacceptable. Users that in generic contexts require to also store optional=
 lvalue references can achieve this effect, even without direct support for=
 optional references, with a bit of meta-programming. </p>

<pre>
template &lt;class T&gt;
struct generic
{
  typedef T type;
};

template &lt;class U&gt;
struct generic&lt;U&amp;&gt;
{
  typedef std::reference_wrapper&lt;U&gt; type;
};

template &lt;class T&gt;
using Generic =3D typename generic&lt;T&gt;::type;

template &lt;class X&gt;
void generic_fun()
{
  std::optional&lt;Generic&lt;X&gt;&gt; op;
  <em>// ...</em>
}
</pre>

<p>Although the behavior of such "emulated" optional references will be sli=
ghtly different than that of "normal" optional references.</p>=20



<h3><a name=3D'rationale.noexcept'>Exception specifications</a></h3>


<p>First draft of this revision required an aggressive usage of conditional=
 <code>noexcept</code> specifications for nearly every, member- or non-memb=
er-, function in the interface. For instance equality comparison was to be =
declared as:</p>

<pre>template &lt;class T&gt;
  bool operator=3D=3D(const optional&lt;T&gt;&amp; lhs, const optional&lt;T=
&gt;&amp rhs)
  noexcept(noexcept(*lhs =3D=3D *rhs));
</pre>

<p>This was based on one of our goals: that we want <code>optional&lt;T&gt;=
</code> to be applicable wherever <code>T</code> is applicable in as many s=
ituations as reasonably possible. One such situation occurs where no-throw =
operations of objects of type <code>T</code> are used to implement a strong=
 exception safety guarantee of some operations. We would like objects of ty=
pe <code>optional&lt;T&gt;</code> to be also useable in such cases. However=
, we do not propose this aggressive conditional no-throw guarantees at this=
 time in order for the proposed library component to adhere to the current =
Library guidelines for conditional <code>noexcept</code>: it is currently o=
nly used in move constructor, move assignment and <code>swap</code>. One ex=
ception to this rule, we think could be made for optional's move constructo=
r and assignment from type <code>T&amp;&amp;</code>, however we still do no=
t propose this at this time in order to avoid controversy.</p>

<p>Constructors and mutating functions that disengage an optional object ar=
e required to be <code>noexcept(true)</code>: they only call <code>T</code>=
's destructor and impose no precondition on optional object's or contained =
value's state. The same applies to the observers that check the disengaged/=
engaged state.</p>

<p>The observers that access the contained value &mdash; <code>operator*</c=
ode> and <code>operator-&gt;</code> &mdash; are not declared as <code>noexc=
ept(true)</code> even though they have no good reason to throw. This is bec=
ause they impose a precondition that optional object shall be engaged, and =
as per observations from N3248<sup>[6]</sup>, library vendors may need to u=
se exceptions to test if the implementation has all the necessary precondit=
ion-checking code inside. These observer functions are still required not t=
o throw exceptions.</p>

<p>In general, operations on optional objects only throw, when operations d=
elegated to the contained value throw.</p>



<h3><a name=3D'rationale.constexpr'>Making <code>optional</code> a literal =
type</a></h3>


<p>We propose that <code>optional&lt;T&gt;</code> be a literal type for tri=
vially destructible <code>T</code>'s.</p>

<pre>constexpr optional&lt;int&gt; oi{5};
static_assert(oi, "");            <em>// ok</em>
static_assert(oi !=3D nullopt, ""); <em>// ok</em>
static_assert(oi =3D=3D oi, "");      <em>// ok</em>
int array[*oi];                   <em>// ok: array of size 5 </em>
</pre>

<p>Making <code>optional&lt;T&gt;</code> a literal-type in general is impos=
sible: the destructor cannot be trivial because it has to execute an operat=
ion that can be conceptually described as:</p>

<pre>
~optional() {
  if (is_engaged()) destroy_contained_value();
}
</pre>

<p>It is still possible to make the destructor trivial for <code>T</code>'s=
 which provide a trivial destructor themselves, and we know an efficient im=
plementation of such <code>optional&lt;T&gt;</code> with compile-time inter=
face &mdash; except for copy constructor and move constructor &mdash; is po=
ssible. Therefore we propose that for trivially destructible <code>T</code>=
's all <code>optional&lt;T&gt;</code>'s constructors, except for move and c=
opy constructors, as well as observer functions are <code>constexpr</code>.=
 The sketch of reference implementation is provided in this proposal.</p>=
=20



<h3><a name=3D'rationale.moved_from'>Moved-from state</a></h3>

<p>When a disengaged optional object is moved from (i.e., when it is the so=
urce object of move constructor or move assignment) its state does not chan=
ge. When an engaged object is moved from, we move the contained value, but =
leave the optional object engaged. A moved-from contained value is still va=
lid (although possibly not specified), so it is fine to consider such optio=
nal object engaged. An alternative approach would be to destroy the contain=
ed value and make the moved-from optional object disengaged. However, we do=
 not propose this for performance reasons.</p>

<p>In contexts, like returning by value, where you need to call the destruc=
tor the second after the move, it does not matter, but in cases where you r=
equest the move explicitly and intend to assign a new value in the next ste=
p, and if <code>T</code> does not provide an efficient move, the chosen app=
roach saves an unnecessary destructor and constructor call:</p>

<pre>optional&lt;array&lt;Big, 1000&gt;&gt; oo =3D ... <em>// array doesn't=
 have efficient move</em>
op =3D std::move(oo);
oo =3D std::move(tmp);
</pre>

<p>The following is even more compelling reason. In this proposal <code>std=
::optional&lt;int&gt;</code> is allowed to be implemented as a <code>Trivia=
llyCopyable</code> type. Therefore, the copy constructor of type <code>std:=
:array&lt;std::optional&lt;int&gt;, 1000&gt;</code> can be implemented usin=
g <code>memcpy</code>. With the additional requirement that <code>optional<=
/code>'s move constructor should not be trivial, we would be preventing the=
 described optimization. </p>

<p>The fact that the moved-from optional is not disengaged may look "uncomf=
ortable" at first, but this is an invalid expectation. The requirements of =
library components expressed in 17.6.5.15 (moved-from state of library type=
s) only require that moved-from objects are in a valid but unspecified stat=
e. We do not need to guarantee anything above this minimum.</p>



<h3><a name=3D'rationale.io'>IO operations</a></h3>


<p>The proposed interface for optional values does not contain IO operation=
s: <code>operator&lt;&lt;</code> and <code>operator&gt;&gt;</code>. While w=
e believe that they would be a useful addition to the interface of optional=
 objects, we also observe that there are some technical obstacles in provid=
ing them, and we choose not to propose them at this time.</p>

<p>One can imagine a couple of ways in which IO-operations for any streamab=
le type <code>T</code> could be expected to work. The differences are mostl=
y the consequence of different conceptual models of optional types, as well=
 as different use cases that programmers may face. Below we list the possib=
le ways of outputting the value of optional object.</p>

<ol>
<li>Output the contained value if engaged; otherwise enter an undefined beh=
aviour (as programmer error).</li>
<li>Output the contained value if engaged; otherwise output nothing.</li>
<li>Output the contained value if engaged; otherwise output some special se=
quence of characters.</li>
<li>Output something like <code>"OPT[<var>v</var>]"</code>, where <code><va=
r>v</var></code> represents the contained value, if engaged; otherwise outp=
ut something like <code>"OPT[]"</code>.</li>
</ol>

<p>The first option is a consequence of the model where <code>optional&lt;T=
&gt;</code> is a <code>T</code> with deferred initialization, but which is =
still initialized before first usage. This is not the model that we advocat=
e, so this behavior of <code>operator&lt;&lt;</code> is rejected. However t=
his behavior can be achieved by accessing the contained value of optional o=
bject on each usage:</p>

<pre>optional&lt;int&gt; oi;
initialize(oi);
cin &gt;&gt; (*oi);
cout &lt;&lt; (*oi);
</pre>


<p>The second option appears useful in certain contexts, where we want opti=
onal objects to indicate some supplementary, often missing, information:</p=
>

<pre>struct FlatNumber {
  unsigned number_;
  optional&lt;char&gt; letter_;
};

ostream&amp; operator&lt;&lt;( ostream&amp; out, FlatNumber n ) {
  return out &lt;&lt; n.number_ &lt;&lt; n.letter_;
}

<em>// outputs "10", "11", "11A", ...</em>=20
</pre>


<p>However, in general the results would be ambiguous. Does output <code>"1=
&nbsp;0"</code> indicate two engaged <code>optional&lt;int&gt;</code>s, or =
three, one of which (which one?) is disengaged, or 77 <code>optional&lt;int=
&gt;</code>s? Or are these perhaps two <code>int</code>s? Also, It is not p=
ossible to implement a consistent <code>operator&gt;&gt;</code> in this cas=
e. It may not be a problem itself, and providing only one operator is not a=
 precedent in the standard (consider <code>std::thread::id</code>); alterna=
tively, <code>operator&gt;&gt;</code> could be implemented inconsistently: =
by simply calling <code>T</code>'s <code>operator&gt;&gt;</code>. =20
</p>  =20

<p>The third choice appears attractive at first glance, but there is no goo=
d representation for the special sequence that would produce no ambiguities=
.. Whatever sequence we choose, it is also a valid representtion of <code>st=
d::string</code>; thus if we need to interpret the special sequence, say <c=
ode>"~~~"</code> as <code>optional&lt;string&gt;</code>, we do not know if =
it is a disengaged object, or engaged one with contained value of <code>"~~=
~"</code>. On the other hand, some people have argued that this ambiguity i=
s worth the usefulness of a simple tool for logging.
</p>

<p>While the fourth choice presented above still comes with some similar am=
biguities, it is posssible to implement a variant thereof that is not ambig=
uous. Such solution has been implemented in Boost.Tuple library<sup>[5]</su=
p>: user has to register a sequence of letters that represent "an opening b=
racket" of the optional object's contained value, and similarly register an=
 another sequence for representing a "closing bracket." This would be the u=
ser's responsibility to make sure that the chosen sequences are unambiguous=
, if default sequences (e.g., <code>"["</code> and <code>"]"</code>) do not=
 suffice. However, this solution is not without certain controversy.</p>

<p>Currently all streamable types in the library have a nice property that =
string representation that is streamed out or read in is similar to the for=
mat of literals in C++ used to initialize variables. Thus, whatever you typ=
e into the console that you intend your program to read, could be equally w=
ell typed directly in the C++ code as a literal &mdash; of course, to certa=
in extent. The text that the program requires of users to read and type is =
simply nice.</p>

<p>This controversy is characteristic not only of <code>optional</code>. Li=
brary components like containers, pairs, tuples face the same issue. At pre=
sent IO operations are not provided for these types. Our preference for <co=
de>optional</code> is to provide an IO solution compatible with this for co=
ntainers, pairs and tuples, therefore at this point we refrain from proposi=
ng a solution for <code>optional</code> alone.</p>



<h3>Type requirements <code>NullableProxy</code></h3>

<p>As already mentioned, the primary purpose of optional object is to check=
 if they contain a value and if so, to provide access to this value. We obs=
erve that a similar functionalit is offered by raw and smart pointers, exce=
pt for the "contains" part: pointers do not contain the value they point to=
.. Nonetheless, optional objects and pointers have enough things in common t=
hat certain class of generic functions can be written that can be used with=
 either. We call the identified concept <code>NullableProxy</code>. Basical=
ly, the concept indicates that a type is a 'proxy' for another type. The op=
erations allowed are: checking if there exists an object that our proxy can=
 indirect us to and the indirection operation. The operations can be summar=
ized by the following use-case:</p>

<pre>
temmplate &lt;class NullableProxy&gt;
void test(NullableProxy&amp;&amp; np)
{
  if (np)              <em>// 'has-object' check</em>
    auto&amp;&amp; obj =3D *np;  <em>// object access</em>
  if (!np) {}          <em>// 'doesn't have object'</em>
}
</pre>

<p>These requirements are sufficient to specify a couple of generic functio=
ns (not proposed), like the one below: </p>

<pre>template &lt;typename NullableProxy&gt;
<em>// enable_if: decltype(*declval&lt;NullableProxy&gt;()) is EqualityComp=
arable</em>
bool equal_pointees( const NullableProxy&amp; x, const NullableProxy&amp; y=
 )
{
  return bool(x) !=3D bool(y) ? false : ( x ? *x =3D=3D *y : true );
}
</pre>

<p>This is exactly the logic for the equality comparison of optional values=
, and could be used as an implementation of <code>optional&lt;T&gt;::operat=
or=3D=3D</code>. A similar algorithm for less-than comparison can be specif=
ied. The third example is function <code>value_or</code> discussed below. A=
nother example is function <code>as_ptr</code> discussed below, for providi=
g raw pointer access to a possibly-null proxied value. </p>

<p>Requirements <code>NullableProxy</code> overlap with requirements <code>=
NullablePointer</code>. Their common part could be extracted to separate re=
quirements, say <code>Nullable</code>, but these requirements are to small =
to be useful alone for anything.</p>

<p>We do not propose to add <code>NullableProxy</code> to Library at this t=
ime, as the usage base may be to small for justifying the change. It may pr=
ove a useful addition in the future.</p>



<h3><a name=3D'rationale.value_or'>Function <code>value_or</code></a></h3>


<p>This function template returns a value stored by the <code>optional</cod=
e> object if it is engaged, and if not, it falls back to the default value =
specified in the second argument. It used to be called <code>get_value_or</=
code> in the previous revisions, but we decided to rename it, as a conseque=
nce of disscussions, so that it is similar to another new member function <=
code>value</code>. This method for specifying default values on the fly rat=
her than tying the default values to the type is based on the observation t=
hat different contexts or usages require different default values for the s=
ame type. For instance the default value for <code>int</code> can be 0 or -=
1. The callee might not know what value the caller considers special, so it=
 returns the lack of the requested value explicitly. The caller may be bett=
er suited to make the choice what special value to use.</p>

<pre>
optional&lt;int&gt; queryDB(std::string);
void setPieceCount(int);
void setMaxCount(int);

setPieceCount( queryDB("select piece_count from ...").value_or(0) );
setMaxCount( queryDB("select max_count from ...").value_or(numeric_limits&l=
t;int&gt;::max()) );
</pre>

<p>The decision to provide this function is controversial itself. As pointe=
d out by Robert Ramey, the goal of the <code>optional</code> is to make the=
 lack of the value explicit. Its syntax forces two control paths; therefore=
 we will typically see an <code>if</code>-statement (or similar branching i=
nstruction) wherever <code>optional</code> is used. This is considered an i=
mprovement in correctness. On the other hand, using the default value appea=
rs to conflict with the above idea. One other argument against providing it=
 is that in many cases you can use a ternary conditional operator instead:<=
/p>

<pre>auto&amp;&amp; cnt =3D queryDB("select piece_count from ...");
setPieceCount(cnt ? *cnt : 0);

auto&amp;&amp; max =3D queryDB("select max_count from ...");
setMaxCount(max ? std::move(*max) : numeric_limits&lt;int&gt;::max());
</pre>

<p>However, in case optional objects are returned by value and immediately =
consumed, the ternary operator syntax requires introducing an lvalue. This =
requires more typing and explicit <code>move</code>. This in turn makes the=
 code less safe because a moved-from lvalue is still accessible and open fo=
r inadvertent misuse.</p>

<p>There are reasons to make it a free-standing function. (1) It can be imp=
lemented by using only the public interface of <code>optional</code>. (2) T=
his function template could be equally well be applied to any type satisfyi=
ng the requirements of <code>NullableProxy</code>. In this proposal, functi=
on <code>value_or</code> is defined as a member function and only for <code=
>optional</code>s. Making a premature generalization would risk standardizi=
ng a function with suboptimal performance/utility. While we know what detai=
led semantics (e.g., the return type) <code>value_or</code> should have for=
 <code>optional</code>, we cannot claim to know the ideal semantics for any=
 <code>NullableProxy</code>. Also, it is not clear to us if this convenienc=
e function is equally useful for pointers, as it is for optional objects. B=
y making <code>value_or</code> a member function we leave the room for this=
 name in namespace <code>std</code> for a possible future generalization.
</p>

<p>The second argument in the function template's signature is not <code>T<=
/code> but any type convertible to <code>T</code>:</p>

<pre>
template &lt;class T, class V&gt;=20
  typename decay&lt;T&gt;::type optional&lt;T&gt;::value_or(V&amp;&amp; <va=
r>v</var>) const&amp;;
template &lt;class T, class V&gt;=20
  typename decay&lt;T&gt;::type optional&lt;T&gt;::value_or(V&amp;&amp; <va=
r>v</var>) &amp;&amp;;
</pre>

<p>This allows for a certain run-time optimization. In the following exampl=
e:</p>

<pre>optional&lt;string&gt; op{"cat"};
string ans =3D op.value_or("dog");
</pre>

<p>Because the optional object is engaged, we do not need the fallback valu=
e and therefore to convert the string literal <code>"dog"</code> into type =
<code>string</code>.</p>

<p>It has been argued that the function should return by constant reference=
 rather than value, which would avoid copy overhead in certain situations:<=
/p>

<pre>void observe(const X&amp; x);

optional&lt;X&gt; ox { <em>/* ... */</em> };
observe( ox.value_or(X{args}) );    <em>// unnecessary copy</em>
</pre>

<p>However, the benefit of the function <code>value_or</code> is only visib=
le when the optional object is provided as a temporary (without the name); =
otherwise, a ternary operator is equally useful:</p>

<pre>optional&lt;X&gt; ox { <em>/* ... */</em> };
observe(ox ? *ok : X{args});            <em>// no copy</em>
</pre>

<p>Also, returning by reference would be likely to render a dangling refere=
nce, in case the optional object is disengaged, because the second argument=
 is typically a temporary:</p>

<pre>
optional&lt;X&gt; ox {nullopt};
auto&amp;&amp; x =3D ox.value_or(X{args});
cout &lt;&lt; x;                             <em> // x is dangling!</em>
</pre>

<p>There is also one practical problem with returning a reference. The func=
tion takes two arguments by reference: the optional object and the default =
value. It can happen that one is deduced as lvalue reference and the other =
as rvalue reference. In such case we would not know what kind of reference =
to return. Returning lvalue reference might prevent move optimization; retu=
rning an rvalue reference might cause an unsafe move from lvalue. By return=
ing by value we avoid these problems by requiring one unnecessary move in s=
ome cases.</p>

<p>We also do not want to return a constant lvalue reference because that w=
ould prevent a copy elision in cases where optional object is returned by v=
alue.</p>

<p>It has also been suggested (by Luc Danton) that function <code>optional&=
lt;T&gt;::value_or&lt;V&gt;</code> should return type <code>decay&lt;common=
_type&lt;T, V&gt;::type&gt;::type</code> rather than <code>decay&lt;T&gt;::=
type</code>. This would avoid certain problems, such as loss of accuracy on=
 arithmetic types:</p>

<pre><em>// not proposed</em>
std::optional&lt;int&gt; op =3D <em>/* ... */</em>;
long gl =3D <em>/* ... */</em>;

auto lossless =3D op.value_or(gl);   <em>// lossless deduced as long rather=
 than int</em>
</pre>

<p>However, we did not find many practical use cases for this extension, so=
 we do not propose is at this time.</p>

<p>Together with function <code>value</code>, <code>value_or</code> makes a=
 set of similarly called functions for accessing the contained value that d=
o not cause an undefined behavior when invoked on a disengaged optional (at=
 the expense of runtime overhead). They differ though, in the return type: =
one returns a value, the other a reference.</p>

<p>One other similar convenience function has been suggested. Sometimes the=
 default value is not given, and computing it takes some time. We only want=
 to compute it, when we know the optional object is disengaged:</p>

<pre>optional&lt;int&gt; oi =3D <em>/* ... */</em>;

if (oi) {
  use(*oi);
}
else {
  int i =3D painfully_compute_default();
  use(i);
}
</pre>

<p>The solution to that situation would be another convenience function whi=
ch rather taking a default value takes a callable object that is capable of=
 computing a default value if needed:</p>

<pre>
use( oi.value_or_call(&amp;painfully_compute_default) );      =20
<em>// or</em>
use( oi.value_or_call([&amp;]{return painfully_compute_default();} );
</pre>

<p>We do not propose this, as we prefer to standardize the existing practic=
e. Also, it is not clear how often the above situations may occur, and the =
tool prove useful. </p>



<h3><a name=3D'rationale.make_optional'>Function <code>make_optional</code>=
</a></h3>


<p>We also propose a helper function <code>make_optional</code>. Its semant=
ics is closer to that of <code>make_pair</code> or <code>make_tuple</code> =
than that of <code>make_shared</code>. You can use it in order for the type=
 of the optional to be deduced:</p>

<pre>int i =3D 1;
auto oi =3D make_optional(i);          <em>// decltype(oi) =3D=3D optional&=
lt;int&gt;</em>
</pre>

<p>This may occasionally be useful when you need to pick the right overload=
 and not type the type of the optional by hand: </p>

<pre>void fun(optional&lt;complex&lt;double&gt;&gt;);
void fun(Current);                   <em>// complex is convertible to Curre=
nt</em>

complex&lt;double&gt; c{0.0, 0.1};
fun(c);                              <em>// ambiguous</em>
fun({c});                            <em>// ambiguous</em>
fun(make_optional(c));               <em>// picks first overload</em>
</pre>

<p>This is not very useful in return statements, as long as the converting =
constructor from <code>T</code> is implicit, because you can always use the=
 brace syntax:</p>

<pre>optional&lt;complex&lt;double&gt;&gt; findC()
{
  complex&lt;double&gt; c{0.0, 0.1};
  return {c};
}
</pre>

<p><code>make_shared</code>-like function does not appear to be useful at a=
ll: it is no different than manually creating a temporary optional object:<=
/p>

<pre><em>// not proposed</em>
fun( make_optional&lt;Rational&gt;(1, 2) );
fun( optional&lt;Rational&gt;{1, 2} );     <em>// same as above</em>
</pre>

<p>It would also not be a good alternative for tagged placement constructor=
, because using it would require type <code>T</code> to be <code>MoveConstr=
uctible</code>:</p>

<pre><em>// not proposed</em>
auto og =3D make_optional&lt;Guard&gt;("arg1"); <em>// ERROR: Guard is not =
MoveConstructible</em>
</pre>

<p>Such solution works for <code>shared_ptr</code> only because its copy co=
nstructor is shallow. One useful variant of <code>shared_ptr</code>-like <c=
ode>make_optional</code> would be a function that either creates an engaged=
 or a disengaged optional based on some boolean condition:</p>

<pre><em>// not proposed</em>
return make_optional_if&lt;Rational&gt;(good(i) &amp;&amp; not_bad(j), i, j=
);

<em>// same as:</em>
if (good(i) &amp;&amp; not_bad(j)) {
  return {i, j};
}
else {
  return nullopt;
}

<em>// same as:</em>
optional&lt;Rational&gt; or =3D nullopt;
if (good(i) &amp;&amp; not_bad(j)) or.emplace(i, j);
return or; <em>// move-construct on return</em>
</pre>

<p>Since this use case is rare, and the function call not that elegant, and=
 a two-liner alternative exists, we do not propose it.</p>



<h3><a name=3D'rationale.as_ptr'>Raw pointer interface</a></h3>


<p>It has been suggested by a couple of people that <code>optional</code> a=
lso provides a function that returns a raw pointer: <code>nullptr</code> if=
 optional is engaged, otherwise a pointer to the contained value. This is s=
imilar to member function <code>get</code> in smart pointers and function <=
code>target</code> in <code>std::function</code>:</p>

<pre>auto optional&lt;T&gt;::as_ptr() -&gt; value_type*
{
  return bool(*this) ? addressof(**this) : nullptr;
}</pre>

<p>It might be convenient when trying to use <code>optional</code> with old=
er libraries that used raw pointers to represent optional values. It was ob=
served, however, that different library components start to diverge from on=
e another in terms of the interface: smart pointers have <code>get</code>, =
<code>function</code> has <code>target</code> it is not clear which name sh=
ould <code>optional</code> use. The semantics of either are different than =
these of <code>optional&lt;T&gt;::as_ptr</code>. Smart pointers store a raw=
 pointer that can be returned. In <code>optional</code> it has to be built.=
 Function <code>target</code> requires specifying a pointer type. It was al=
so suggested that a generic function applicable uniformly to all smart poin=
ters and <code>optional</code> would be a better choice. However, when trea=
ting pointers and <code>optional</code> uniformly, the situation becomes tr=
icky because unlike all pointers, <code>optional</code>  <em>contains</em> =
the object it indirects to and needs to propagate constness when providing =
access to its contained value. </p>


<pre>template &lt;typename NullableProxy&gt;
auto as_ptr( NullableProxy&amp;&amp; x ) -> decltype(addressof(*std::forwar=
d&lt;NullableProxy&gt;(x)))
{
  return bool(x) ? addressof(*std::forward&lt;NullableProxy&gt;(x)) : nullp=
tr;
}
</pre>

<p>The convoluted type may be too tricky for a Library function, and a type=
 traits for <code>NullableProxy</code> may be needed. This appears to be to=
o much changes compared with the limited functionality the function would o=
ffer. We chose not to propose it. Programmers who need it for <code>optiona=
l</code> can easily implement it as indicated above. If the feature turns o=
ut to be necessary, it can be added in the future in a backwards-compatible=
 manner.</p>


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

<p>At some point the following goal was considered for <code>optional</code=
>; it is the property that could informally be called "copy initialization =
forwarding". It is somewhat similar to the one-argument version of perfect =
forwarding constructor; i.e., if a given initializer can be used to copy-in=
itialize objects of type <code>T</code>, it should also be possible to to u=
se it to copy-initialize objects of type <code>optional&lt;T&gt;</code> wit=
h the same samantics as initializing object of type <code>T</code>. This go=
al cannot be achieved in 100% without severely compromising other design go=
als. For instance, we cannot guarantee the following: </p>

<pre>T x =3D {};             <em>// "{}" is the initializer; x is value-ini=
tialized</em>
optional&lt;T&gt; ox =3D {};  <em>// same initializer; contained value not =
initialized</em>

assert (x =3D=3D *ox);    <em>// not guaranteed!</em>
</pre>

<p>Apart from this default initialization case, and a couple of others (con=
cerning initializer-list), "copy initialization forwarding" could be provid=
ed for <code>optional</code>.</p>

<p>Since <code>optional&lt;T&gt;</code> can be thought of as an "almost <co=
de>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>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 tur=
n 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 =3D ptr;           <em>// ILLEGAL</em>=20
}

void implicit_conv( int * ptr ) {
  optional&lt;unique_ptr&lt;int&gt;&gt; v =3D 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 constru=
ctor. We could achieve this by specifying two constructor templates with id=
entical 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;=20
  <em>// enable_if: Constructible&lt;T, U&amp;&amp;&gt; &amp;&amp; !Convert=
ible&lt;U&amp;&amp;, T&gt;</em>
  explicit optional&lt;T&gt;::optional(U&amp;&amp;);
  =20
template &lt;class U&gt;=20
  <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 ex=
istence of such converting constructor would cause unexpected ambiguities i=
n overload resolution. Consider the following scenario. We start from a wor=
king 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 optiona=
l 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 substitut=
ing 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 <cod=
e>string const&amp;</code> than to create a temporary optional object and t=
rigger 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 =3D "hello";
fun(s);                                <em>// copy ctor invoked!</em>
</pre>


<p>This example shows how an implicit conversion can cause an inadvertent a=
nd 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=3D'rationale.initializer_list'>Handling <code>initializer_list<=
/code></a></h3>

<p>Another feature worth considering is a "sequence constructor" (one that =
takes <code>initializer_list</code> as its argument). It would be enabled (=
in <code>enable_if</code> sense) only for these <code>T</code>s that themse=
lf provide a sequence constructor. This would be useful to fully support tw=
o features we already mentioned above (but chose not to propose).</p>

<p>First, our goal of "copy initialization forwarding" for <code>optional</=
code> also needs to address the following usages of <code>initializer_list<=
/code>:
</p>

<pre>vector&lt;int&gt; v =3D {1, 2, 4, 8};
optional&lt;vector&lt;int&gt;&gt; ov =3D {1, 2, 4, 8};

assert (v =3D=3D *ov);
</pre>

<p>This is not only a syntactical convenience. It also avoids subtle bugs. =
When perfect forwarding constructor is implemented naively with one variadi=
c constructor, optional vector initialization may render surprising result:=
</p>

<pre>optional&lt;vector&lt;int&gt;&gt; ov =3D {3, 1};

assert (*ov =3D=3D vector{3, 1});    // FAILS!
assert (*ov =3D=3D vector{1, 1, 1}); // TRUE!
</pre>

<p>However this sequence constructor feature is incompatible with another o=
ne: default constructor creating a disengaged optional. This is because, as=
 outlined in the former example, initializer <code>{}</code>, that looks li=
ke 0-element list, is in fact interpretted as the request for value-initial=
ization (default constructor call). This may hit programmers that use initi=
alizer list in "generic" context:</p>

<pre>
template &lt;class ...A&gt; <em>// enable_if: every A is int</em>
void fwd(const A&&... a)
{
  optional&lt;vector&lt;int&gt;&gt; o =3D {a...};
  assert (bool(o)); <em>// not true for empty a</em>
}
</pre>

<p>If this feature were to be added, we would need to provide an assignment=
 from initializer list and variadic 'emplacement' constructor with the firs=
t forwarded argument being <code>initializer_list</code>:</p>

<pre>ov =3D {1, 2, 4, 8};

allocator&lt;int&gt; a;
optional&lt;vector&lt;int&gt;&gt; ou { emplace, {1, 2, 4, 8}, a };

assert (ou =3D=3D ov);
</pre>


<p>Since we are not proposing neither perfect forwarding constructor, nor t=
he "copy initialization forwarding", we are also not proposing the sequence=
 constructor. However, in this proposal, the following constructs work:</p>

<pre>optional&lt;vector&lt;int&gt;&gt; ov{emplace, {3, 1}};
assert (*ov =3D=3D vector{3, 1});

ov.emplace({3, 1});
assert (*ov =3D=3D vector{3, 1});
</pre>


<h3><a name=3D'rationale.optional_optional'><code>optional&lt;optional&lt;T=
&gt;&gt;</code></a></h3>


<p>The necessity to create a "double" optional explicitly does not occur of=
ten. Such type may appear though in generic contexts where we create <code>=
optional&lt;V&gt;</code> and <code>V</code> only happens to be <code>option=
al&lt;T&gt;</code>. Some special behavior to be observed in this situation =
is the following. When copy-initializing with <code>nullopt</code>, the "ou=
termost" optional is initialized to disengaged state. Thus, changing functi=
on argument from <code>optional&lt;T&gt;</code> to <code>optional&lt;option=
al&lt;T&gt;&gt;</code> will silently break the code in places where the arg=
ument passed to function happens to be of type <code>nullopt_t</code>:</p>

<pre><em>// before change</em>
void fun(optional&lt;T&gt; v) {
  process(v);
}

fun(nullopt); <em>// process() called</em>

<em>// after change</em>
void fun(optional&lt;optional&lt;T&gt;&gt; v) {
  if (v) process(*v);
  else   doSthElse();
}

fun(nullopt); <em>// process() not called!</em>
</pre>

<p>This issue would not arise if <code>nullopt</code> were <code>T</code>-s=
pecific:</p>=20

<pre>
fun(optional&lt;T&gt;::nullopt);            <em>// process() called</em>
fun(optional&lt;optional&lt;T&gt;&gt;::nullopt);  <em>// process() not call=
ed</em>
</pre>


<p>Since <code>T</code>-dependent <code>nullopt</code> is not proposed, in =
order to create an engaged optional containing a disengaged optional, one n=
eeds to use one of the following constructs:</p>

<pre>
optional&lt;optional&lt;T&gt;&gt; ot {emplace};
optional&lt;optional&lt;T&gt;&gt; ou {emplace, nullopt};
optional&lt;optional&lt;T&gt;&gt; ov {optional&lt;T&gt;{}};
</pre>

<p>Also note that <code>make_optional</code> will create a "double" optiona=
l when called with optional argument:</p>

<pre>optional&lt;int&gt; oi;
auto ooi =3D make_optional(oi);
static_assert( is_same&lt;optional&lt;optional&lt;int&gt;&gt;, decltype(ooi=
)&gt;::value, "");
</pre>



<h3><a name=3D'rationale.conditional.init'>Conditional initialization to en=
gaged state</a></h3>


<p>It has been suggested, and in fact implemented in Boost.Optional, that <=
code>optional</code> shall have an another constructor with the first argum=
ent of type <code>bool</code>. The value of this argument should be used to=
 determine whether the object should be disengaged, or engaged using the re=
maining arguments. If we wanted to provide it in <code>optional</code> and =
disampiguate the situations where the contained value is also initialized w=
ith the first argument of type <code>bool</code>, it could be easily done b=
y providing a yet another tag (similar to <code>emplace_t</code>):</p>

<pre>bool doIt =3D false;
tr2::optional&lt;string&gt; opstr1{ tr2::only_if(doIt), 3, 'x' };=20
<em>// disengaged</em>

doIt =3D true;
tr2::optional&lt;string&gt; opstr2{ tr2::only_if(doIt), 3, 'x' };=20
<em>// contained value is "xxx"</em>
</pre>

<p>However, we do not see a practical use case for this usage. Undoubtedly,=
 it spares you from an explicit if-statement and a two-phase initialization=
 of the optional object, but then it appears obvious that at some time you =
need to check the value of <code>doIt</code> anyway, because you do not eve=
n know the state of the optional object. It is at that time that you may as=
 well decide to initialize the contained value:</p>

<pre>optional&lt;string&gt; process( bool doIt )
{
  fun1(doIt);
  optional&lt;string&gt; optstr;
 =20
  if (doIt) { <em> // we ned an if to conditionally call fun2()</em>
    fun2();
    optstr.emplace(3, 'x');
  }
 =20
  return optstr;
}</pre>

<p>Also, even if you create optional as disengaged in the conditional const=
ructor, you still have to compute the values of the arguments that you coul=
d potentially use to initialize the contained value, so the two-phase initi=
alization may look more attractive.
</p>

<p>For these reasons we do not propose such conditional constructor at this=
 point. However, there appears to be no difficulty in adding it if we find =
convincing use cases.</p>



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



<p>Add new header in Table 14 (C++ library headers).</p>

<blockquote class=3D"std">
<table>
<caption>Table 14 &mdash; C++ library headers</caption>
<tbody>
<tr>
<td><code>&lt;algorithm&gt;</code></td> <td><code>&lt;fstream&gt;</code></t=
d> <td><code>&lt;list&gt;</code></td> <td><code>&lt;ratio&gt;</code></td> <=
td><code>&lt;tuple&gt;</code></td>

</tr>
<tr>
<td><code>&lt;array&gt;</code></td> <td><code>&lt;functional&gt;</code></td=
> <td><code>&lt;locale&gt;</code></td> <td><code>&lt;regex&gt;</code></td> =
<td><code>&lt;typeindex&gt;</code></td>
</tr>
<tr>
<td><code>&lt;atomic&gt;</code></td> <td><code>&lt;future&gt;</code></td> <=
td><code>&lt;map&gt;</code></td> <td><code>&lt;set&gt;</code></td> <td><cod=
e>&lt;typeinfo&gt;</code></td>

</tr>
<tr>
<td><code>&lt;bitset&gt;</code></td> <td><code>&lt;initializer_list&gt;</co=
de></td> <td><code>&lt;memory&gt;</code></td> <td><code>&lt;sstream&gt;</co=
de></td> <td><code>&lt;type_traits&gt;</code></td>
</tr>
<tr>
<td><code>&lt;chrono&gt;</code></td> <td><code>&lt;iomanip&gt;</code></td> =
<td><code>&lt;mutex&gt;</code></td> <td><code>&lt;stack&gt;</code></td> <td=
><code>&lt;unordered_map&gt;</code></td>

</tr>
<tr>
<td><code>&lt;codecvt&gt;</code></td> <td><code>&lt;ios&gt;</code></td> <td=
><code>&lt;new&gt;</code></td> <td><code>&lt;stdexcept&gt;</code></td> <td>=
<code>&lt;unordered_set&gt;</code></td>
</tr>
<tr>
<td><code>&lt;complex&gt;</code></td> <td><code>&lt;iosfwd&gt;</code></td> =
<td><code>&lt;numeric&gt;</code></td> <td><code>&lt;streambuf&gt;</code></t=
d> <td><code>&lt;utility&gt;</code></td>

</tr>
<tr>
<td><code>&lt;condition_variable&gt;</code></td> <td><code>&lt;iostream&gt;=
</code></td> <td><ins><code>&lt;optional&gt;</code></ins></td> <td><code>&l=
t;string&gt;</code></td> <td><code>&lt;valarray&gt;</code></td>
</tr>
<tr>
<td><code>&lt;dequeue&gt;</code></td> <td><code>&lt;istream&gt;</code></td>=
 <td><code>&lt;ostream&gt;</code></td> <td><code>&lt;strstream&gt;</code></=
td> <td><code>&lt;vector&gt;</code></td>

</tr>
<tr>
<td><code>&lt;exception&gt;</code></td> <td><code>&lt;iterator&gt;</code></=
td> <td><code>&lt;queue&gt;</code></td> <td><code>&lt;system_error&gt;</cod=
e></td> <td><code>&nbsp;</code></td>
</tr>
<tr>
<td><code>&lt;forward_list&gt;</code></td> <td><code>&lt;limits&gt;</code><=
/td> <td><code>&lt;random&gt;</code></td> <td><code>&lt;thread&gt;</code></=
td> <td><code>&nbsp;</code></td>

</tr>
</tbody>
</table>
</blockquote>


<p>After chapter 20.4 Tuples [tuple], insert a new paragraph. (Chapter [tem=
plate.bitset] (Class template <code>bitset</code>) becomes 20.6.)</p>
 =20
 <blockquote class=3D"std">=20
<h3><a name=3D"optional">20.5 Optional objects <span style=3D"float:right">=
[optional]</span></a></h3>


<h4><a name=3D"optional.general">20.5.1 In general <span style=3D"float:rig=
ht">[optional.general]</span></a></h4>

<p>This subclause describes class template <code>optional</code> that repre=
sents <em>optional objects</em>. An <em>optional object for object types</e=
m> is an object that contains the storage for another object and manages th=
e lifetime of this contained object. The contained object may be initialize=
d after the optional object has been initialized, and may be destroyed befo=
re the optional object has been destroyed. The initialization state of the =
contained object is tracked by the optional object. An <em>optional object =
for lvalue reference types</em> is an object capable of storing the address=
 of another object. The address stored by the optional object can be change=
d or set to a value that does not represent a valid address. </p>

 =20
<h4><a name=3D"optional.synop">20.5.2 Header <kbd>&lt;optional&gt;</kbd> sy=
nopsis <span style=3D"float:right">[optional.synop]</span></a></h4>

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

  // <em><a href=3D"#optional.inplace">20.5.5</a>, In-place construction</e=
m>
  struct emplace_t{};
  constexpr emplace_t emplace{};

  // <em><a href=3D"#optional.nullopt">20.5.6</a>, Disengaged state indicat=
or</em>
  struct nullopt_t{<em>see below</em>};
  constexpr nullopt_t nullopt(<em>unspecified</em>);
 =20
  // <em><a href=3D"#optional.bad_optional_access">20.5.7</a>, class bad_op=
tional_access</em>
  class bad_optional_access;

  // <em><a href=3D"#optional.relops">20.5.8</a>, Relational operators</em>
  template &lt;class T&gt;
    constexpr bool operator=3D=3D(const optional&lt;T&gt;&amp;, const optio=
nal&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator!=3D(const optional&lt;T&gt;&amp;, const optiona=
l&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&lt;(const optional&lt;T&gt;&amp;, const optiona=
l&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&gt;(const optional&lt;T&gt;&amp;, const optiona=
l&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&lt;=3D(const optional&lt;T&gt;&amp;, const opti=
onal&lt;T&gt;&amp;);
  template &lt;class T&gt;
    constexpr bool operator&gt;=3D(const optional&lt;T&gt;&amp;, const opti=
onal&lt;T&gt;&amp;);

  // <em><a href=3D"#optional.nullops">20.5.9</a>, Comparison with <code>nu=
llopt</code></em>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;=
T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator=3D=3D(nullopt_t, const o=
ptional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&=
gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator!=3D(nullopt_t, const opt=
ional&lt;T&gt;&amp;) noexcept;
  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 opt=
ional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const optional&lt=
;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&lt;=3D(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 opt=
ional&lt;T&gt;&amp;) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const optional&lt=
;T&gt;&amp;, nullopt_t) noexcept;
  template &lt;class T&gt; constexpr bool operator&gt;=3D(nullopt_t, const =
optional&lt;T&gt;&amp;) noexcept;

  // <em><a href=3D"#optional.comp_with_t">20.5.10</a>, Comparison with T</=
em>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;=
T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;, cons=
t optional&lt;T&gt;&amp;);
  template &lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&=
gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, const =
optional&lt;T&gt;&amp;);
  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&lt;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;, con=
st 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;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;, con=
st optional&lt;T&gt;&amp;);

  // <em><a href=3D"#optional.specalg">20.5.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;=20
    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;=20
    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; mak=
e_optional(T&amp;&amp;);

  // <em><a href=3D"#optional.hash">20.5.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;;
} <em>// namespace experimental</em>
} <em>// namespace std</em>
</pre>
 =20
  <p>A program that necessitates the instantiation of template <code>option=
al</code> for an lvalue reference or rvalue reference type, or for types <c=
ode>emplace_t</code> or <code>nullopt_t</code>, or a possibli cv-qualified =
reference to types <code>emplace_t</code> or <code>nullopt_t</code> is ill-=
formed.</p>

<h4><a name=3D"optional.defs">20.5.3 Definitions <span style=3D"float:right=
">[optional.defs]</span></a></h4>
=20
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>diseng=
aged</em> if it has been default constructed, constructed or assigned with =
a value of type <code>nullopt_t</code>, constructed or assigned with a dise=
ngaged optional object of type <code>optional&lt;T&gt;</code>.
  </p>

  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>engage=
d</em> if it has=20
  been modified with member function <code>emplace</code>, constructed with=
 a value of type <code>T</code>, assigned a value of type <code>T</code>, c=
opy-constructed from or assigned with an engaged optional object of type <c=
ode>optional&lt;T&gt;</code>.=20
  </p>
 =20
  <!--
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>diseng=
aged</em> if it has been default constructed, constructed or assigned with =
a value of type <code>nullopt_t</code>, constructed or assigned with a dise=
ngaged optional object of type <code>optional&lt;U&gt;</code>, with <code>U=
</code> being equal or not to <code>T</code>.
  </p>
 =20
  <p>An instance of <code>optional&lt;T&gt;</code> is said to be <em>engage=
d</em> if it has=20
  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>, c=
opy-constructed from or assigned with an engaged optional object of type <c=
ode>optional&lt;U&gt;</code>, where <code>U</code> is same as or convertibl=
e to <code>T</code>.=20
  </p>
  -->

  <p>Being engaged or disengaged is part of the optional object's state.</p=
>
 =20
  <!-- <p>An <em>optional object for object type</em> is an instance of cla=
ss <code>optional&lt;T&gt;</code> where <code>T</code> is of object type (3=
..9).</p>
 =20
  <p>An <em>optional object for lvalue reference type</em> is an instance o=
f class <code>optional&lt;T&gt;</code> where <code>T</code> is of lvalue re=
ference type.</p>-->
 =20

 =20
<h4><a name=3D"optional.object">20.5.4 <code>optional</code> for object typ=
es <span style=3D"float:right">[optional.object]</span></a></h4>

 =20
<pre>
namespace std {
namespace experimental {

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

    // <em><a href=3D"#optional.object.ctor">20.5.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(emplace_t, A=
rgs&amp;&amp;...);
    template &lt;class U, class... Args&gt;
      constexpr explicit optional(emplace_t, initializer_list&lt;U&gt;, Arg=
s&amp;&amp;...);

    // <em><a href=3D"#optional.object.dtor">20.5.4.2</a>, destructor</em>
    ~optional();

    // <em><a href=3D"#optional.object.assign">20.5.4.3</a>, assignment</em=
>
    optional&amp; operator=3D(nullopt_t) noexcept;<!--
    optional&amp; operator=3D(const T&amp;);
    optional&amp; operator=3D(T&amp;&amp;);-->
    optional&amp; operator=3D(const optional&amp;);
    optional&amp; operator=3D(optional&amp;&amp;) noexcept(<em>see below</e=
m>);
    template &lt;class U&gt; optional&amp; operator=3D(U&amp;&amp;);<!--
    template &lt;class U&gt; optional&amp; operator=3D(const optional&lt;U&=
gt;&amp;);
    template &lt;class U&gt; optional&amp; operator=3D(optional&lt;U&gt;&am=
p;&amp;);
    template &lt;class U&gt; optional&amp; operator=3D(initializer_list&lt;=
U&gt;);-->
    template &lt;class... Args&gt; optional&amp; emplace(Args&amp;&amp;...)=
;
    template &lt;class U, class... Args&gt;
      optional&amp; emplace(initializer_list&lt;U&gt;, Args&amp;&amp;...);

    // <em><a href=3D"#optional.object.swap">20.5.4.4</a>, swap</em>
    void swap(optional&) noexcept(<em>see below</em>);

    // <em><a href=3D"#optional.object.observe">20.5.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>
  };

} // <em>namespace experimental</em>
} // <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 t=
he optional object. Implementations are not permitted to use additional sto=
rage, such as dynamic memory, to allocate its contained value.  The contain=
ed value shall be allocated in a region of the <code>optional&lt;T&gt;</cod=
e> storage suitably aligned for the type <code>T</code>. Initializing the c=
ontained value shall put the optional object into engaged state. Destroying=
 the contained value shall put the optional object into disengaged state.</=
p>

<p>Members <code><var>init</var></code> and <code><var>val</var></code> are=
 provided for exposition only. Implementations need not provide those membe=
rs. <code><var>init</var></code> indicates whether the <code>optional</code=
> object's contained value has been initialized (and not yet destroyed); <c=
ode><var>val</var></code> points to (a possibly uninitialized) contained va=
lue.
  </p>

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

<p>Throughout this subclause term <em>direct-non-list-initialization</em> i=
s used to denote a direct-initialization that is not list-initialization.</=
p>



<h5><a name=3D"optional.object.ctor">20.5.4.1 Constructors <span style=3D"f=
loat:right">[optional.object.ctor]</span></a></h5>

 =20
  <p class=3D"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=3D"attribute">
  <dt>Effects:</dt> <dd><p>Constructs a disengaged <code>optional</code> ob=
ject.</p></dd>
  <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D false</code>.</p=
></dd>
  <dt>Remarks:</dt> <dd><p>No <code>T</code> object referenced is initializ=
ed. For every object type <code>T</code> these constructors shall be <code>=
constexpr</code> constructors (7.1.5).</p></dd>
  </dl>

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

  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue as if direct-non-list-initializing an object of type <code>T</code> wit=
h the expression <code>*<var>rhs</var></code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) =3D=3D bool(=
*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;::optional(optional&lt;T&gt; &amp;&amp; <var>rhs</=
var>) noexcept(<em>see below</em>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue as if direct-non-list-initializing an object of type <code>T</code> wit=
h the expression <code>std::move(*<var>rhs</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) =3D=3D bool(=
*this)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</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>

 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;::optional(const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object by =
direct-non-list-initializing the contained value with the expression <code>=
<var>v</var></code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</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>const=
expr</code> constructor.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;::optional(T&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object by =
direct-non-list-initializing the contained value with the expression <code>=
std::move(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s sele=
cted constructor throws.</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>const=
expr</code> constructor.</p></dd>
  </dl>
 =20
<!-- =20
  <p class=3D"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;::option=
al(U&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an engaged <code>optional</code> ob=
ject; initializes the contained value as if constructing an object of type =
<code>T</code> with argument <code>std::forward&lt;U&gt;(<var>v</var>)</cod=
e>.</p></dd>

    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code> an=
d <code>*(*this)</code> is equivalent to <code><var>v</var></code> if conve=
rted to <code>T</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload resolution unless <code>OnlyExplicitlyConstructible(T, U&amp;&amp;)<=
/code> is <code>true</code>. The second function shall not participate in t=
he overload resolution unless <code>is_convertible&lt;U&amp;&amp;, T&gt;::v=
alue</code> is <code>true</code>. The expression inside <code>noexcept</cod=
e> is equivalent to:</p> <pre>noexcept(T(std::declval&lt;U&amp;&amp;&gt;())=
)</pre></dd>
  </dl>
 =20
 =20
  <p class=3D"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;  optio=
nal&lt;T&gt;::optional(const optional&lt;U&gt;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue 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>) =3D=3D fa=
lse</code> then <code>bool(*this) =3D=3D false</code>; otherwise <code>bool=
(*this) =3D=3D true</code> 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>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload 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>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(optio=
nal&lt;U&gt;&amp;&amp; <var>rhs</var>);<br>template &lt;class U&gt;  option=
al&lt;T&gt;::optional(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue 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>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload resolution unless <code>OnlyExplicitlyConstructible(T, U&amp;&amp;)<=
/code> is <code>true</code>. The second function shall not participate in t=
he overload resolution unless <code>is_convertible&lt;U&amp;&amp;, T&gt;::v=
alue</code> is <code>true</code>.</dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; explicit optional&lt;T&gt;::optional(initi=
alizer_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>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>Constructs an <code>optional</code> object. If=
 <code>bool(<var>rhs</var>) =3D=3D true</code> initializes the contained va=
lue as if constructing an object of type <code>T</code> with argument <code=
><var>il</var></code></p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall not participate in ov=
erload resolution unless <code>OnlyExplicitlyConstructible(T, initializer_l=
ist&lt;U&gt;)</code> is <code>true</code>. The second function shall not pa=
rticipate in the overload resolution unless <code>is_convertible&lt;initial=
izer_list&lt;U&gt;, T&gt;::value</code> is <code>true</code>.</dd>
  </dl>
-->
 =20
  <p class=3D"function">
  <code>template &lt;class... Args&gt; constexpr explicit optional(emplace_=
t, Args&amp;&amp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"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>Constructs an engaged <code>optional</code> ob=
ject. Initializes the contained value as if constructing an object of type =
T with the arguments <code>std::forward&lt;Args&gt;(<var>args</var>)...</co=
de>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>T</code>'s constructor selected for t=
he initialization is a <code>constexpr</code> constructor, this constructor=
 shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>


  <p class=3D"function">
  <code>template &lt;class U, class... Args&gt;<br>
  explicit optional(emplace_t, initializer_list&lt;U&gt; <var>il</var>, Arg=
s&amp;&amp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></d=
d>
    <dt>Effects:</dt> <dd><p>Constructs an engaged <code>optional</code> ob=
ject. Initializes the contained value as if constructing an object of type =
T with the arguments <code><var>il</var>, std::forward&lt;Args&gt;(<var>arg=
s</var>)...</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization throws.</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;, =
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 t=
he initialization is a <code>constexpr</code> constructor, this constructor=
 shall be a <code>constexpr</code> constructor.</p></dd>
  </dl>


<h5><a name=3D"optional.object.dtor">20.5.4.2 Destructor  <span style=3D"fl=
oat:right">[optional.object.dtor]</span></a></h5>
 =20
  <p class=3D"function">=20
  <code>optional&lt;T&gt;::~optional();</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>If <code>is_trivially_destructible&lt;T&gt;::v=
alue !=3D true</code> and <code>bool(*this) =3D=3D true</code>, calls <code=
><var>val</var>-&gt;T::~T()</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>If <code>is_trivially_destructible&lt;T&gt;::v=
alue =3D=3D true</code> then this destructor shall be a trivial destructor.=
</p></dd>
  </dl>
 =20
 =20
 =20
<h5><a name=3D"optional.object.assign">20.5.4.3 Assignment  <span style=3D"=
float:right">[optional.object.assign]</span></a></h5>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(nullopt_t) no=
except;</code>
  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> 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>bool(*this) =3D=3D false</code>.<=
/p></dd>
  </dl>
 =20
  <!--
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(const T&amp; =
<var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> assign=
s <code><var>rhs</var></code> to the contained value; otherwise constructs =
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>bool(*this) =3D=3D true</code> an=
d <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 du=
ring the call to <code>T</code>'s copy constructor, no effect. If an except=
ion is thrown during the call to <code>T</code>'s copy assignment, the stat=
e of its contained value is as defined by the exception safety guarantee of=
 <code>T</code>'s copy assignment.</p></dd>
  </dl>

 =20
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(T&amp;&amp; <=
var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> assign=
s <code>std::move(<var>rhs</var>)</code> to the contained value; otherwise =
constructs 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>bool(*this) =3D=3D true</code> an=
d <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 du=
ring 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</c=
ode>'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>
 =20
  -->
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(const optiona=
l&lt;T&gt;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D 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><code>bool(<var>rhs</var>) =3D=3D bool(=
*this)</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 un=
changed. 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 constructor.</p=
></dd>
  </dl>
 =20
=20
  <p class=3D"function">
  <code>optional&lt;T&gt;&amp; optional&lt;T&gt;::operator=3D(optional&lt;T=
&gt;&amp;&amp; <var>rhs</var>) noexcept(<em>see below</em>);</code>
  </p>
 =20
  <dl class=3D"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</co=
de> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D=
 true</code>, assigns <code>std::move(*<var>rhs</var>)</code> to the contai=
ned value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(<var>rhs</var>) =3D=3D 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 && is_n=
othrow_move_constructible&lt;T&gt;::value</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 un=
changed. 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 <c=
ode><var>*val</var></code> and <code>*rhs.val</code> is determined by excep=
tion safety guarantee of <code>T</code>'s move assignment.</p></dd>

  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(U&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&gt;::value</co=
de> 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>bool(*this) =3D=3D true</code> assign=
s <code>std::forward&lt;U&gt;(<var>v</var>)</code> to the contained value; =
otherwise constructs 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>bool(*this) =3D=3D true</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 du=
ring the call to <code>T</code>'s constructor, the state of <code><var>v</v=
ar></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 as=
signment.</p></dd>
    <dt>Remarks:</dt> <dd><p>The function shall not participate in overload=
 resolution unless  <code>is_same&lt;typename remove_reference&lt;U&gt;::ty=
pe, T&gt;::value</code> is  <code>true</code>.</dd>
  </dl>
  <p>[<i>Note:</i> The reson to provide such generic assignment and then co=
nstraining it so that effectively <code>T</code> =3D=3D <code>U</code> is t=
o guarantee that assignment of the form <code>o =3D {}</code> is unambiguou=
s. &mdash;<i>end note</i>]</p>

  <!--
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(U&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, U&gt;::value</co=
de> 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>bool(*this) =3D=3D true</code> assign=
s <code>std::forward&lt;U&gt;(<var>v</var>)</code> to the contained value; =
otherwise constructs 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>bool(*this) =3D=3D true</code> an=
d <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 du=
ring the call to <code>T</code>'s constructor, the state of <code><var>v</v=
ar></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 as=
signment.</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 o=
f class template <code>optional</code>.</dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(const optional&lt;U&gt;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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&a=
mp;&gt;::value</code> is <code>true</code>.</p></dd>
    <dt>Effects:</dt> <dd><p>If <code><var>init</var> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D 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>) =3D=3D fa=
lse</code> then <code>bool(*this) =3D=3D false</code>; otherwise <code>bool=
(*this) =3D=3D true</code> 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 un=
changed. If an exception is thrown during the call to <code>T</code>'s cons=
tructor, no effect. If an exception is thrown during the call to <code>T</c=
ode>'s  assignment, the state of i<code><var>*val</var></code> is defined b=
y the exception safety guarantee of <code>T</code>'s  assignment.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(optional&lt;U&gt;&amp;&amp; <var>rhs</var>);</code>
  </p>
 =20
  <dl class=3D"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> =3D=3D false &amp;&am=
p; <var>rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</=
var> =3D=3D true && <var>rhs.init</var> =3D=3D false</code>, destroys the c=
ontained value by calling <code><var>val</var>-&gt;T::~T()</code>. If <code=
><var>init</var> =3D=3D false &amp;&amp; <var>rhs.init</var> =3D=3D true</c=
ode>, constructs 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> =3D=3D true &amp;&amp; <var>rhs.init</var> =3D=3D=
 true</code>, assigns <code>std::move(*<var>rhs</var>)</code> to the contai=
ned value.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) =3D=3D fa=
lse</code> then <code>bool(*this) =3D=3D false</code>; otherwise <code>bool=
(*this) =3D=3D true</code> and <code>*(*this)</code> is equivalent to the v=
alue <code>*<var>rhs</var></code> had initially if converted to <code>T</co=
de>.</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 un=
changed. If an exception is thrown during the call to <code>T</code>'s cons=
tructor, no effect. If an exception is thrown during the call to <code>T</c=
ode>'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>
 =20

  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&gt;&amp; optional&lt;T&gt;::=
operator=3D(initializer_list&lt;U&gt; <var>il</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;&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>.</p></=
dd>
     <dt>Effects:</dt> <dd><p>If <code>bool(*this) =3D=3D true</code> assig=
ns <code><var>il</var></code> to the contained value; otherwise constructs =
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>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever <code>T::T(initializer_list&lt;U&gt;)<=
/code> or <code>T::operator=3D(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 d=
uring 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;&g=
t;::value</code> is <code>true</code> and <code>is_assignable&lt;T, initial=
izer_list&lt;U&gt;&gt;::value</code> is <code>true</code>.</dd>
  </dl>
-->
 =20
  <p class=3D"function">
  <code>template &lt;class... Args&gt; optional&lt;T&gt;&amp; optional&lt;T=
&gt;::emplace(Args&amp;&amp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"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 =3D nullopt</code>. Then ini=
tializes 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>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever expression <code>T(std::forward&lt;Arg=
s&gt;(<var>args</var>)...)</code> throws.</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>
 =20
 =20
   <p class=3D"function">
  <code>template &lt;class U, class... Args&gt; optional&lt;T&gt;&amp; opti=
onal&lt;T&gt;::emplace(initializer_list&lt;U&gt; <var>il</var>, Args&amp;&a=
mp;... <var>args</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>is_constructible&lt;T, initializer_list=
&lt;U&gt;, Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></d=
d>
    <dt>Effects:</dt> <dd><p>Calls <code>*this =3D nullopt</code>. Then ini=
tializes 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>Returns:</dt> <dd><p><code>*this</code></p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</=
p></dd>
    <dt>Throws:</dt> <dd><p>Whatever expression <code>T(<var>il</var>, std:=
:forward&lt;Args&gt;(<var>args</var>)...)</code> throws.</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;, =
Args&amp;&amp;...&gt;::value</code> is <code>true</code>.</p></dd>
  </dl>


 =20
<h5><a name=3D"optional.object.swap">20.5.4.4 Swap <span style=3D"float:rig=
ht">[optional.object.swap]</span></a></h5>


  <p class=3D"function">
  <code>void optional&lt;T&gt;::swap(optional&lt;T&gt;& rhs) noexcept(<em>s=
ee below</em>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>T</code> shall be swappable for lvalues=
 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> =3D=3D false && <var>=
rhs.init</var> =3D=3D false</code>, no effect. If <code><var>init</var> =3D=
=3D true && <var>rhs.init</var> =3D=3D false</code>, constructs the contain=
ed 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>i=
nit</var>, <var>rhs.init</var>)</code>. If <code><var>init</var> =3D=3D fal=
se && <var>rhs.init</var> =3D=3D true</code>, constructs the contained valu=
e 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> =3D=3D true && =
<var>rhs.init</var> =3D=3D true</code>, calls <code>swap(*(*this), *<var>rh=
s</var>)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever expressions <code>swap(declval&lt;T&am=
p;&gt;(), declval&lt;T&amp;&gt;())</code> and <code>T{move(declval&lt;T&amp=
;&amp;&gt;())}</code> 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 && noexc=
ept(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 un=
changed. 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> i=
s determined by the exception safety quarantee of <code>swap</code> for lva=
lues of <code>T</code>. If an exception is thrown durning 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 quarantee of <=
code>T</code>'s move constructor.</p></dd>

  </dl>

 =20
 =20
<h5><a name=3D"optional.object.observe">20.5.4.5 Observers  <span style=3D"=
float:right">[optional.object.observe]</span></a></h5>


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

  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</p></dd=
>
    <dt>Returns:</dt> <dd><p><code>val</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 w=
ith overloaded unary <code>operator&amp;</code>, the first function shall b=
e a <code>constexpr</code> function.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T const&amp; optional&lt;T&gt;::operator*() const;<br>T&a=
mp; optional&lt;T&gt;::operator*();</code>

  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</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>
 =20
 =20
  <p class=3D"function">
  <code>constexpr explicit optional&lt;T&gt;::operator bool() noexcept;</co=
de>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code><var>init</var></code></p></dd>
    <dt>Remarks:</dt> <dd><p>this function shall be a <code>constexpr</code=
> function.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T const&amp; optional&lt;T&gt;::value() const;<br>T&amp; =
optional&lt;T&gt;::value();</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code><var>*val</var></code>, if <code>bool(*t=
his)</code>.</p></dd>
    <dt>Throws:</dt> <dd><p><code>bad_optional_access</code> if <code>!*thi=
s</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>The first function shall be a <code>constexpr<=
/code> function.</p></dd>

  </dl>
 =20
 =20

  <p class=3D"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>
 =20
  <dl class=3D"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>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization of the return value throws.</p></dd=
>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> =3D=3D true<=
/code> and exception is thrown durning the call to <code>T</code>'s constru=
ctor, the value of <code><var>init</var></code> and <code><var>v</var></cod=
e> remains unchanged and the state of <code><var>*val</var></code> is deter=
mined by the exception safety quarantee of the selected <code>T</code>'s co=
nstructor. Otherwise, when exception is thrown durning the call to <code>T<=
/code>'s constructor, the value of <code><var>*this</var></code> remains un=
changed and the state of <code><var>v</var></code> is determined by the exc=
eption safety quarantee of the selected <code>T</code>'s constructor</p></d=
d>
    <dt>Remarks:</dt> <dd><p>If the selected <code>T</code>'s constructor i=
s a <code>constexpr</code> constructor, this function shall be a <code>cons=
texpr</code> function.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; T optional&lt;T&gt;::value_or(U&amp;&amp; =
<var>v</var>) &amp;&amp;;</code>
  </p>
 =20
  <dl class=3D"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>Whatever the execution of the selected <code>T<=
/code>'s constructor selected for the initialization of the return value th=
rows.</p></dd>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> =3D=3D true<=
/code> and exception is thrown durning the call to <code>T</code>'s constru=
ctor, the value of <code><var>init</var></code> and <code><var>v</var></cod=
e> remains unchanged and the state of <code><var>*val</var></code> is deter=
mined by the exception safety quarantee of the <code>T</code>'s constructor=
.. Otherwise, when exception is thrown durning the call to <code>T</code>'s =
constructor, the value of <code><var>*this</var></code> remains unchanged a=
nd the state of <code><var>v</var></code> is determined by the exception sa=
fety quarantee of the selected <code>T</code>'s constructor</p></dd>
  </dl>


 =20
<h4><a name=3D"optional.inplace">20.5.5 In-place construction  <span style=
=3D"float:right">[optional.inplace]</span></a></h4>


  <p class=3D"function">
  <code>struct emplace_t{}; <br>constexpr emplace_t emplace{};</code>
  </p>

  <p>The struct <code>emplace_t</code> is a disengaged structure type used =
as a unique type to disambiguate constructor and function overloading. Spec=
ifically, <code>optional&lt;T&gt;</code> has a constructor with <code>empla=
ce_t</code> as the first argument followed by an argument pack; this indica=
tes 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>

 =20
<h4><a name=3D"optional.nullopt">20.5.6 Disengaged state indicator  <span s=
tyle=3D"float:right">[optional.nullopt]</span></a></h4>


  <p class=3D"function">
  <code>struct nullopt_t{<em>see below</em>}; <br>constexpr nullopt_t nullo=
pt(<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> objec=
ts. In particular, <code>optional&lt;T&gt;</code> has a constructor with <c=
ode>nullopt_t</code> as single argument; this indicates that a disengaged o=
ptional object shall be constructed.
  </p>
 =20
  <p>Type <code>nullopt_t</code> shall not have a default constructor. It s=
hall be a literal type. Constant <code>nullopt</code> shall be initialized =
with argument of literal type.</p>
 =20
=20
 =20
<h4><a name=3D"optional.bad_optional_access">20.5.7 Class <code>bad_optiona=
l_access</code>  <span style=3D"float:right">[optional.bad_optional_access]=
</span></a></h4>
=20
<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 t=
hrown as exceptions to report the situation where an attempt is made to acc=
ess the value of a disengaged optional object.</p>

  <p class=3D"function">=20
  <code>bad_optional_access(const string&amp; what_arg);</code>
  </p>
 =20
  <dl class=3D"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()) =3D=
=3D 0</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">=20
  <code>bad_optional_access(const char* what_arg);</code>
  </p>
 =20
  <dl class=3D"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) =3D=3D 0</c=
ode>.</p></dd>
  </dl>

=20
<h4><a name=3D"optional.relops">20.5.8 Relational operators  <span style=3D=
"float:right">[optional.relops]</span></a></h4>
=20

  <p class=3D"function">=20
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const option=
al&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code>
  </p>
 =20
  <dl class=3D"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) !=3D bool(y)</code>, <code>fals=
e</code>; otherwise if <code>bool(x) =3D=3D false</code>, <code>true</code>=
; otherwise <code>*x =3D=3D *y</code>.</p></dd>
  <dt>Remarks:</dt> <dd><p>Instantiations of this function template for whi=
ch <code>*x =3D=3D *y</code> is a core constant expression, shall be <code>=
constexpr</code> functions.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>!(x =3D=3D y)</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <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>=20
  </p>

  <dl class=3D"attribute">
  <dt>Requires:</dt> <dd><p><code>T</code> shall meet the requirements of <=
code>LessThanComparable</code>.</p></dd>
  <dt>Returns:</dt> <dd><p>If <code>(!y)</code>, <code>false</code>; otherw=
ise, if <code>(!x)</code>, <code>true</code>; otherwise <code>*x &lt; *y</c=
ode>.</p></dd>
  <dt>Remarks:</dt> <dd><p>Instantiations of this function template for whi=
ch <code>*x &lt; *y</code> is a core constant expression, shall be <code>co=
nstexpr</code> functions.</p></dd>
  </dl>

 =20
  <p class=3D"function">
  <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>=20
  </p>

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

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

  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>!(x < y)</code>.</p></dd>
  </dl>
=20

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


  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; <var>x</var>, nullopt_t) noexcept;<br>template &lt;class T&g=
t; constexpr bool operator!=3D(nullopt_t, const optional&lt;T&gt;&amp; x) n=
oexcept;</code>
  </p>

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


  <p class=3D"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>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>false</code>.</p></dd>
  </dl>

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

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

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

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

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

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

=20
<h4><a name=3D"optional.comp_with_t">20.5.10 Comparison with <code>T</code>=
 <span style=3D"float:right">[optional.comp_with_t]</span></a></h4>


  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const option=
al&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> =3D=3D =
<var>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;=
 <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> =3D=3D *=
<var>x</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> !=3D <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> !=3D *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
  <!-- -->
 =20
  <p class=3D"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>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt; <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt; *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <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>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &gt; <v=
ar>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt; *<v=
ar>x</var> : false</code>.</p></dd>
  </dl>
 =20
  <!-- -->
=20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var>  &gt;=
=3D <var>v</var> : false</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt;=3D =
*<var>x</var> : false</code>.</p></dd>
  </dl>
  =20
  =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt;=3D=
 <var>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);</code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt;=3D =
*<var>x</var> : true</code>.</p></dd>
  </dl>
 =20

=20
<h4><a name=3D"optional.specalg">20.5.11 Specialized algorithms <span style=
=3D"float:right">[optional.specalg]</span></a></h4>


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

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


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

  <dl class=3D"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>
 =20
 =20
  <p class=3D"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=3D"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 safe=
ty as <code>T</code>'s move-constructor.</p></dd>
  </dl>-->
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt;<br>
    &nbsp;&nbsp;constexpr optional&lt;typename decay&lt;T&gt;::type&gt; mak=
e_optional(T&amp;&amp; <var>v</var>);</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code>optional&lt;typename decay&lt;T&gt;::typ=
e&gt;(std::forward&lt;T&gt;(<var>v</var>))</code>.</p></dd>
  </dl>
 =20
 =20
<h4><a name=3D"optional.hash">20.5.12 Hash support <span style=3D"float:rig=
ht">[optional.hash]</span></a></h4>


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

  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p>the template specilaization <code>hash&lt;T&g=
t;</code> shall meet the requirements of class template <code>hash</code> (=
20.9.12).
      The template specilaization <code>hash&lt;optional&lt;T&gt;&gt;</code=
> shall meet the requirements of class template <code>hash</code>.=20
      For an object <code>o</code> of type <code>optional&lt;T&gt;</code>, =
if <code>bool(o) =3D=3D true</code>,=20
      <code>hash&lt;optional&lt;T&gt;&gt;()(o)</code> shall evaluate to the=
 same value as <code>hash&lt;T&gt;()(*o)</code>.</p></dd>=20
  </dl>
 =20
</blockquote>




<h2><a name=3D"optional_ref">Auxiliary proposal &mdash; optional references=
</a></h2>



<p>We propose optional references as an auxiliary proposal. This is to give=
 the Committee the freedom to make the decision to accept or not optional r=
eferences independently of the decision to accept optional values. </p>


<h3><a name=3D"optional_ref.overview">Overview</a></h3>

<p>Optional references are surprising to many people because they do not ap=
pear to add any more functionality than pointers do. There exist though a c=
ouple of arguments in favour optional references:</p>

<ul>
<li><code>optional&lt;T&gt;</code> can be used in generic code, were <code>=
T</code> can be either a reference or an object. </li>
<li>It is slightly easier to pass arguments to functions, beacause addresso=
f operator is not required. </li>
<li>Raw pointers historically add confusion in the sense that it is not cle=
ar whether we should delete the value they point to or not, as well as whet=
her we should expect <code>nullptr</code> or not. With optional references =
the answer to these questions is obvious.</li>
</ul>


<p>The interface for optional references is somewhat limited compared to th=
at for optional values.</p>

<pre>int i =3D 1;
int j =3D 2;
optional&lt;int&amp;&gt; ora;                <em>// disengaged optional ref=
erence to int</em>
optional&lt;int&amp;&gt; orb =3D i;            <em>// contained reference r=
efers to object i</em>

*orb =3D 3;                          <em>// i becomes 3</em>
ora =3D j;                           <em>// ERROR: optional refs do not hav=
e assignment from T</em>
ora =3D optional&lt;int&amp;&gt;{j};           <em>// contained reference r=
efers to object j</em>
ora =3D {j};                         <em>// same as line above</em>
orb =3D ora;                         <em>// rebinds orb to refers to the sa=
me object as ora</em>
*orb =3D 7;                          <em>// j becomes 7</em>
ora.emplace(j);                    <em>// OK: contained reference refers to=
 object j</em>
ora.emplace(i);                    <em>// OK: contained reference now refer=
s to object i</em>
ora =3D nullopt;                     <em>// OK: ora becomes disengaged</em>
</pre>

<p>In some aspects optional lvalue references act like raw pointers: they a=
re rebindable, may become disengaged, and do not have special powers (such =
as extending the life-time of a temporary). In other aspects they act like =
C++ references: they do not provide pointer arithmetic, operations like com=
parisons, hashing are performed on referenced objects. </p>

<pre>hash&lt;int&gt; hi;
hash&lt;optional&lt;int&amp;&gt;&gt; hoi;

int i =3D 0;
int j =3D 0;
optional&lt;int&amp;&gt; ori =3D i;            <em>// orj and ori refer to =
two different objects</em>
optional&lt;int&amp;&gt; orj =3D j;            <em>// but with the same val=
ue</em>

assert (hoi(ori) =3D=3D hi(i));       =20
assert (hoi(ori) =3D=3D hoi(orj));     <em>// because we are hashing the va=
lues</em>
assert (ori =3D=3D orj);               <em>// because we are comparing the =
values</em>
</pre>

<p>Because optional references are easily implementable  with a raw pointer=
, we require that almost no operation on optional references (except <code>=
value</code> and <code>value_or</code>) throws exceptions. </p>

<p>Optional references do not popagate constness to the object they indirec=
t to:</p>

<pre>int i =3D 9;
const optional&lt;int&amp;&gt; mi =3D i;
int&amp; r =3D *mi;                      <em>// OK: decltype(*mi) =3D=3D in=
t&amp;</em>
=20
optional&lt;const int&amp;&gt; ci =3D i;
int&amp; q =3D *ci;                      <em>// ERROR: decltype(*ci) =3D=3D=
 const int&amp;</em>
</pre>

<p>Note also the peculiar way in which function <code>make_optional</code> =
can create optional references:</p>

<pre>int i =3D 1;
auto oi =3D make_optional(i);          <em>// decltype(oi) =3D=3D optional&=
lt;int&gt;</em>
auto ri =3D make_optional(ref(i));     <em>// decltype(ri) =3D=3D optional&=
lt;int&amp;&gt;</em>
</pre>



<h3><a name=3D"optional_ref.use_cases">Practical use cases</a></h3>


<p>While used significantly less often, optional references are useful in c=
ertain cases. For instance, consider that you need to find a possibly missi=
ng element in some sort of a collection, and if it is found, change it. The=
 following is a possible implementation of such function that will help wit=
h the find.</p>

<pre>
optional&lt;int&amp;&gt; find_biggest( vector&lt;int&gt;& vec )
{
  optional&lt;int&amp;&gt; biggest;=20
  for (int & val : vec) {
    if (!biggest || *biggest &lt; val) {
      biggest.emplace(val);
    }
  }
  return biggest;
}=20
</pre>

<p>This could be alternatively implemented using a raw pointer; however, op=
tional reference makes it clear that the caller will not be owing the objec=
t. For another example, consider that we want a function to modify the pass=
ed object (<code>storeHere</code> in the example) as one of its responsibil=
ities, but we sometimes cannot provide the object, but we do want the funct=
ion to perform other responsibilities. The following is the possible implem=
entation</p>

<pre>template &lt;typename T&gt;
T getValue( optional&lt;T&gt; newVal =3D nullopt, optional&lt;T&amp;&gt; st=
oreHere =3D nullopt )
{
  if (newVal) {
    cached =3D *newVal;
   =20
    if (storeHere) {
      *storeHere =3D *newVal; <em>// LEGAL: assigning T to T</em>
    }     =20
  }
  return cached;     =20
}
</pre>

<p>Again, this can also be implemented with pointers; however, with optiona=
l references the ownership of the memory is clear. Additionally, we do not =
have to use the indirection operator:</p>

<pre>
static int global =3D 0;
const int newVal =3D 2;
return getValue(newVal, global);
</pre>


<h3><a name=3D'optional_ref.rationale.assign'>Assignment for optional refer=
ences</a></h3>

<p>The semantics of optional references' copy assignment turned out to be v=
ery controversial. This is because whatever semantics for such assignment i=
s chosen, it is confusing to many programmers. An optional reference can be=
 seen as a reference with postponed initialization. In this case, assignmen=
t (to engaged optional reference) is expected to have deep copy semantics: =
it should assign value to the referred object. On the other hand, an option=
al reference can be seen as a pointer with different syntax. In this case t=
he assignment (to engaged optional reference) should change the reference, =
so that it refers to the new object. Neither of these models appears more v=
alid than the other. On the other hand, the majority of people insist that =
optional should be copy-assignable. We choose somewhat arbitralrily to prov=
ide a rebinding semantics for <code>std::optional</code>. This is to follow=
 the practice adapted by <code>std::reference_wrapper</code> and <code>boos=
t::optional</code>. In consequence, optional references are not value seman=
tic types: <code>operator=3D=3D</code> compares something else than copy as=
signment and constructor are copying. This should not be surprising, though=
, for a component that is called a "reference".</p>

<p>The other semantics can be implemented by using the following idiom:</p>

<pre>void assign_norebind(optional&lt;T&amp;&gt;&amp; optref, T&amp; obj)
{
  if (optref) *optref =3D obj;
  else        optref.emplace(obj);
}
</pre>

<h3><a name=3D'optional_ref.rationale.rref_binding'>No rvalue binding for o=
ptional references</a></h3>

<p>Optional referencs cannot provide an essential feature of native referen=
ces: extending the life-time of temporaries (rvalues). Temporaries can be b=
ound to (1) rvalue references and to (2) lvalue references to const. In ord=
er to avoid dangling reference problems we need to prevent either type of b=
infing to optional references. In order to prevent the former, we disallow =
optional rvalue references altogether. We are not aware of any practical us=
e case for such entities. Since optional lvalue references to const appear =
useful, we avoid the rvalue binding problem by requiring implementations to=
 "poison" rvalue reference constructors for optional lvalue references to c=
onst. This may appear surprising as it is inconsistent with normal (non-opt=
ional) reference behavior:</p>

<pre>const int&amp; nr =3D int{1};           <em>// ok</em>
optional&lt;const int&&gt; or =3D int{1}; <em>// error: int&amp;&amp; ctor =
deleted</em>
</pre>





<h3><a name=3D"optional_ref.wording">Wording</a></h3>


<p>The wording for optional references is relative to the wording for optio=
nal values form the main proposal. It assumes the wording for optional valu=
es has already been applied.</p>

<p>In [optional.synop] add declaration of template specialization <code>opt=
ional&lt;T&amp;&gt;</code>:</p>

 <blockquote class=3D"std">
 <h4>20.5.2 Header <kbd>&lt;optional&gt;</kbd> synopsis <span style=3D"floa=
t:right">[optional.synop]</span></h4>

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

<ins>  // <em><a href=3D"#optional.lref">20.5.5</a>, <code>optional</code> =
for lvalue reference types</em>
  template &lt;class T&gt; class optional&lt;T&amp;&gt;;</ins>

  // <em><a href=3D"#optional.inplace"><del>20.5.5</del><ins>20.5.6</ins></=
a>, In-place construction</em>
  struct emplace_t{};
  constexpr emplace_t emplace{};

 </pre>
 </blockquote>
=20
 <p>Next, in the same clause change:</p>
=20
 <blockquote class=3D"std">
  <pre>// <em><a href=3D"#optional.comp_with_t"><del>20.5.10</del><ins>20.5=
..11</ins></a>, Comparison with T</em>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;=
T&gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const optiona=
l&lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const optiona=
l&lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;, cons=
t optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;,=
 const optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;,=
 const optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&=
gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const optional&=
lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const optional&=
lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, const =
optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, c=
onst optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp;, c=
onst optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&=
gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;(const optional&=
lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;(const optional&=
lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  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 T&amp;, c=
onst optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp;, c=
onst optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const option=
al&lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const option=
al&lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;, con=
st optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;=
, const optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp;=
, const optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&=
gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const optional&=
lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const optional&=
lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, const =
optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, c=
onst optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp;, c=
onst optional&lt;const T&amp;&gt;&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const optional&lt=
;T&gt;&amp;, const T&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const option=
al&lt;T&amp;&gt;&amp;, const T&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const option=
al&lt;const T&amp;&gt;&amp;, const T&amp;);</ins>
  template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;, con=
st optional&lt;T&gt;&amp;);
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;=
, const optional&lt;T&amp;&gt;&amp;);</ins>
  <ins>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp;=
, const optional&lt;const T&amp;&gt;&amp;);</ins>
 =20
  // <em><a href=3D"#optional.specalg"><del>20.5.11</del><ins>20.5.12</ins>=
</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;=20
    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;=20
    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; mak=
e_optional(T&amp;&amp;);
 =20
  // <em><a href=3D"#optional.hash"><del>20.5.12</del><ins>20.5.13</ins></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>template &lt;class T&gt; struct hash&lt;optional&lt;T&amp;&gt;&gt;;<=
/ins>
  </pre>
 </blockquote>
=20
  <p>In the same clause, in the last sentence change:</p>

<blockquote class=3D"std">
 <p>A program that necessitates the instantiation of template <code>optiona=
l</code> for an <del>lvalue reference or </del>rvalue reference type, or fo=
r types <code>emplace_t</code> or <code>nullopt_t</code>, or a possibly cv-=
qualified reference to types <code>emplace_t</code> or <code>nullopt_t</cod=
e> is ill-formed.</p>
</blockquote>

<p>After clause 2.5.4 [optional.object] insert clause 20.5.5 [optional.lref=
] (clause [optional.inplace] becomes 20.5.6) </p>
<blockquote class=3D"stdins">
<h4><a name=3D"optional.lref">20.5.5 <code>optional</code> for lvalue refer=
ence types <span style=3D"float:right">[optional.lref]</span></a></h4>


<pre>
namespace std {
namespace experimental {

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

    // <em><a href=3D"#optional.lref.ctor">20.5.5.1</a>, construction/destr=
uction</em>
    constexpr optional() noexcept;
    constexpr optional(nullopt_t) noexcept;
    constexpr optional(T&amp;) noexcept;
    constexpr optional(T&amp;&amp;) =3D delete;
    constexpr optional(const optional&amp;) noexcept;<!--
    template &lt;class U&gt; constexpr optional(const optional&lt;U&amp;&gt=
;&amp;) noexcept;-->
    constexpr explicit optional(emplace_t, T&amp;) noexcept;
    constexpr explicit optional(emplace_t, T&amp;&amp;) =3D delete;
    ~optional() =3D default;

    // <em><a href=3D"#optional.lref.mutate">20.5.5.2</a>, mutation</em>
    optional&amp; operator=3D(nullopt_t) noexcept;<!--
    optional&amp; operator=3D(const optional&amp;) noexcept;
    optional&amp; operator=3D(optional&amp;&amp;) noexcept;-->
    template &lt;class U&gt; optional&amp; operator=3D(U&amp;&amp;) noexcep=
t;
    template &lt;class U&gt; optional&amp; operator=3D(U&amp;&amp;) noexcep=
t =3D delete;
    optional&amp; emplace(T&amp;) noexcept;
    optional&amp; emplace(T&amp;&amp;) =3D delete;

    // <em><a href=3D"#optional.lref.swap">20.5.5.3</a>, swap</em>
    void swap(optional&amp; rhs) noexcept;

    // <em><a href=3D"#optional.lref.observe">20.5.5.4</a>, observers</em>
    constexpr T* operator-&gt;() const;
    constexpr T&amp; operator*() const;
    constexpr explicit operator bool() const noexcept;
    constexpr T&amp; value() const;
    template &lt;class U&gt; constexpr typename decay&lt;T&gt;::type value_=
or(U&amp;&amp;) const&amp;;

  <var>private:</var>
    <var>T* ref;  //</var> <em>exposition only</em>
  };

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

  <p>Engaged instances of <code>optional&lt;T&gt;</code> where <code>T</cod=
e> is of lvalue reference type, refer to objects of type <code>std::remove_=
reference&lt;T&gt;::type</code>, but their life-time is not connected to th=
e life-time of the referred to object. Destroying or disengageing the optio=
nal object does not affect the state of the referred to object.</p>
 =20
  <p>Member <code><var>ref</var></code> is provided for exposition only. Im=
plementations need not provide this member. If <code><var>ref</var> =3D=3D =
nullptr</code>, optional object is disengaged; otherwise <code><var>ref</va=
r></code> points to a valid object.
  </p>

  <p>In several places in this Clause the expression <code>static_addressof=
(<var>v</var>)</code> is used. This expression is defined as follows. If ty=
pe <code>typename decay&lt;decltype(<var>v</var>)&gt;::type</code> is a use=
r-defined class type with overloaded <code>operator-&gt;</code>:
  </p>
 =20
  <pre>addressof(<var>v</var>)</pre>
 =20
  <p>otherwise:</p>
 =20
  <pre>&amp;(<var>v</var>)</pre>
 =20
 =20
=20
=20
=20
<h5><a name=3D"optional.lref.ctor">20.5.5.1 Construction and destruction <s=
pan style=3D"float:right">[optional.lref.ctor]</span></a></h5>


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

  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Constructs a disengaged <code>optional</code> =
object by initializing <code><var>ref</var></code> with <code>nullptr</code=
>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D false</code>.<=
/p></dd>
    <dt>Remarks:</dt> <dd><p>These constructors shall be <code>constexpr</c=
ode> constructors (7.1.5).</p></dd>
  </dl>

  <p class=3D"function">
  <code>constexpr optional&lt;T&amp;&gt;::optional(T&amp; <var>v</var>) noe=
xcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Constructs an engaged <code>optional</code> ob=
ject by initializing <code><var>ref</var></code> with <code>static_addresso=
f(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true &amp;&amp=
; static_addressof(*(*this)) =3D=3D static_addressof(<var>v</var>)</code>.<=
/p></dd>
    <dt>Remarks:</dt> <dd><p>Unless <code>T</code> is a user-defined class =
type with overloaded <code>operator-&gt;</code>, this constructor shall be =
a <code>constexpr</code> constructor.</p></dd>
  </dl>=20
 =20
 =20
  <p class=3D"function">
  <code>constexpr optional&lt;T&amp;&gt;::optional(const optional&amp; <var=
>rhs</var>) noexcept;<!--<br>template &lt;class U&gt; optional&lt;T&amp;&gt=
;::optional(const optional&lt;U&amp;&gt;&amp; <var>rhs</var>) noexcept;--><=
/code>
  </p>

 =20
  <dl class=3D"attribute">
    <!--<dt>Requires:</dt> <dd><p><code>is_base_of&lt;T, U&gt;::value =3D=
=3D true</code>, and <code>is_convertible&lt;U&amp;, T&amp;&gt;::value</cod=
e> is <code>true</code>.</p></dd>-->
    <dt>Effects:</dt> <dd><p>If <code><var>rhs</var></code> is disengaged, =
initializes <code><var>ref</var></code> with <code>nullptr</code>; otherwis=
e, constructs an engaged object by initializing <code><var>ref</var></code>=
 with <code>static_addressof(*<var>rhs</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p>If <code>bool(<var>rhs</var>) =3D=3D tr=
ue</code>, then <code>bool(*this) =3D=3D true &amp;&amp; static_addressof(*=
(*this)) =3D=3D static_addressof(*<var>rhs</var>)</code>; otherwise <code>b=
ool(*this) =3D=3D false</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This constructor shall be a <code>constexpr</c=
ode> constructor.</p></dd>
  </dl>  =20
 =20
  <p class=3D"function">
  <code>constexpr explicit optional&lt;T&amp;&gt;::optional(emplace_t, T&am=
p; <var>v</var>) noexcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Constructs an engaged <code>optional</code> ob=
ject by initializing <code><var>ref</var></code> with <code>static_addresso=
f(<var>v</var>)</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true &amp;&amp=
; static_addressof(*(*this)) =3D=3D static_addressof(<var>v</var>)</code>.<=
/p></dd>
    <dt>Remarks:</dt> <dd><p>Unless <code>T</code> is a user-defined class =
type with overloaded <code>operator-&gt;</code>, this constructor shall be =
a <code>constexpr</code> constructor.</p></dd>
  </dl>=20
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;::~optional() =3D default;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>No effect. This destructor shall be a trivial =
destructor.</p></dd>
  </dl> =20
 =20
 =20
<h5><a name=3D"optional.lref.mutate">20.5.5.2 Mutation <span style=3D"float=
:right">[optional.lref.mutate]</span></a></h5>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;&amp; optional&lt;T&amp;&gt;::operator=3D(nul=
lopt_t) noexcept;</code>
  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code>nullptr</code>. If <code><var>ref</var></code> was non-null ini=
tially, the object it referred to is unaffected.</p></dd>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D false</code>.<=
/p></dd>
  </dl><!--
=20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;&amp; optional&lt;T&amp;&gt;::operator=3D(con=
st optional&amp; <var>v</var>) noexcept;<br>optional&lt;T&amp;&gt;&amp; opt=
ional&lt;T&amp;&gt;::operator=3D(optional&amp;&amp; <var>v</var>) noexcept;=
</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code><var>v</var>.<var>ref</var></code>. If <code><var>ref</var></co=
de> was non-null initially, the object it referred to is unaffected.</p></d=
d>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>(!*this) ? (!<var>v</var>) : bool=
(<var>v</var>) &amp;&amp; static_addressof(*(*this)) =3D=3D static_addresso=
f(*<var>v</var>)</code>.</p></dd>
  </dl>-->
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&amp;&gt;&amp; optional&lt;T&=
amp;&gt;::operator=3D(U &amp;&amp; <var>v</var>) noexcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code><var>v</var>.<var>ref</var></code>. If <code><var>ref</var></co=
de> was non-null initially, the object it referred to is unaffected.</p></d=
d>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>(!*this) ? (!<var>v</var>) : bool=
(<var>v</var>) &amp;&amp; static_addressof(*(*this)) =3D=3D static_addresso=
f(*<var>v</var>)</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This function shall not participate in overloa=
d resolution unless <code>is_same&lt;typename decay&lt;U&gt;::type, optiona=
l&lt;T&amp;&gt;&gt;::value</code> is <code>true</code>. </dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; optional&lt;T&amp;&gt;&amp; optional&lt;T&=
amp;&gt;::operator=3D(U &amp;&amp; <var>v</var>) noexcept =3D delete;</code=
>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Remarks:</dt> <dd><p>This function shall not participate in overloa=
d resolution unless <code>is_same&lt;typename decay&lt;U&gt;::type, optiona=
l&lt;T&amp;&gt;&gt;::value</code> is <code>false</code>. </dd>
  </dl>
 =20
 =20
  <p>[<i>Note:</i> The reson to provide these two overloads is to enable no=
tation <code>oj =3D {}</code> and <code>oj =3D {j}</code> and copy/move ass=
ignment, but to prevent notation <code>oj =3D j</code>, where <code>oj</cod=
e> is of type <code>optional&lt;T&amp;&gt;</code> and <code>j</code> is of =
type <code>T</code>. &mdash;<i>end note</i>]</p>
 =20
 =20
  <p class=3D"function">
  <code>optional&lt;T&amp;&gt;&amp; optional&lt;T&amp;&gt;::emplace(T&amp; =
<var>v</var>) noexcept;</code>
  </p>

  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Assigns <code><var>ref</var></code> with a val=
ue of <code>static_addressof(<var>v</var>)</code>. If <code>*this</code> wa=
s engaged before the call the object it referred to is not affected.</p></d=
d>
    <dt>Returns:</dt> <dd><p><code>*this</code>.</p></dd>
    <dt>Postconditions:</dt> <dd><p><code>bool(*this) =3D=3D true &amp;&amp=
; static_addressof(*(*this)) =3D=3D static_addressof(<var>v</var>)</code>.<=
/p></dd>
  </dl>
=20


<h5><a name=3D"optional.lref.swap">20.5.5.3 Swap <span style=3D"float:right=
">[optional.lref.observe]</span></a></h5>

  <p class=3D"function">
  <code>void optional&lt;T&amp;&gt;::swap(optional&amp; <var>rhs</var>) noe=
xcept;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Effects:</dt> <dd><p>Calls <code>swap(<var>ref</var>, <var>rhs</var=
>.<var>ref</var>)</code>.</p></dd>
  </dl>

 =20
 =20
 =20
<h5><a name=3D"optional.lref.observe">20.5.5.4 Observers <span style=3D"flo=
at:right">[optional.lref.observe]</span></a></h5>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T* optional&lt;T&amp;&gt;::operator-&gt;() const;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</p></dd=
>
    <dt>Returns:</dt> <dd><p><code><var>ref</var></code>.</p></dd>
    <dt>Throws:</dt> <dd><p>nothing.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>constexpr T&amp; optional&lt;T&amp;&gt;::operator*() const;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p><code>bool(*this) =3D=3D true</code>.</p></dd=
>
    <dt>Returns:</dt> <dd><p><code>*<var>ref</var></code></p></dd>
    <dt>Throws:</dt> <dd><p>nothing.</p></dd>
  </dl>
 =20
 =20
    <p class=3D"function">
  <code>constexpr T&amp; optional&lt;T&amp;&gt;::value() const;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code>*<var>ref</var></code>, if <code>bool(*t=
his) =3D=3D true</code>.</p></dd>
    <dt>Throws:</dt> <dd><p><code>bad_optional_access</code> if <code>bool(=
*this) =3D=3D false</code>.</p></dd>
    <dt>Remarks:</dt> <dd><p>This function shall be a constexpr function.</=
p></dd>
  </dl>

 =20
  <p class=3D"function">
  <code>constexpr explicit optional&lt;T&amp;&gt;::operator bool() noexcept=
;</code>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt> <dd><p><code><var>ref</var> !=3D nullptr</code></p></=
dd>
  </dl> =20
 =20
  <p class=3D"function">
  <code>template &lt;class U&gt; constexpr typename decay&lt;T&gt;::type op=
tional&lt;T&gt;::value_or(U&amp;&amp; <var>v</var>) const&amp;;</code>
  </p>
 =20
  <dl class=3D"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;ty=
pename decay&lt;T&gt;::type&gt;(std::forward&lt;U&gt;(<var>v</var>))</code>=
..</p></dd>
    <dt>Throws:</dt> <dd><p>Whatever the execution of <code>T</code>'s cons=
tructor selected for the initialization of the return value throws.</p></dd=
>
    <dt>Exception Safety:</dt> <dd><p>If <code><var>init</var> =3D=3D true<=
/code> and exception is thrown durning the call to <code>T</code>'s constru=
ctor, the value of <code>*this</code> and <code><var>v</var></code> remains=
 unchanged. Otherwise, when exception is thrown durning the call to <code>T=
</code>'s constructor, the value of <code><var>*this</var></code> remains u=
nchanged and the state of <code><var>v</var></code> is determined by the ex=
ception safety quarantee of the selected <code>T</code>'s constructor</p></=
dd>
    <dt>Remarks:</dt> <dd><p>If the selected <code>T</code>'s constructor i=
s a <code>constexpr</code> constructor, this function shall be a <code>cons=
texpr</code> function.</p></dd>
  </dl>
 =20

</blockquote>

<p>In subclause [optional.comp_with_t] change: </p>

<blockquote class=3D"std">
<h4><a name=3D"optional.comp_with_t"><del>20.5.10</del><ins>20.5.11</ins> C=
omparison with <code>T</code> <span style=3D"float:right">[optional.comp_wi=
th_t]</span></a></h4>


  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const option=
al&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>template=
 &lt;class T&gt; constexpr bool operator=3D=3D(const optional&lt;T&amp;&gt;=
&amp; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;=
class T&gt; constexpr bool operator=3D=3D(const optional&lt;const T&amp;&gt=
;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> =3D=3D =
<var>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator=3D=3D(const T&amp;=
 <var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class =
T&gt; constexpr bool operator=3D=3D(const T&amp; <var>v</var>, const option=
al&lt;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt;=
 constexpr bool operator=3D=3D(const T&amp; <var>v</var>, const optional&lt=
;const T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> =3D=3D *=
<var>x</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const optional=
&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>template &=
lt;class T&gt; constexpr bool operator!=3D(const optional&lt;T&amp;&gt;&amp=
; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;clas=
s T&gt; constexpr bool operator!=3D(const optional&lt;const T&amp;&gt;&amp;=
 <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> !=3D <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator!=3D(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class T&=
gt; constexpr bool operator!=3D(const T&amp; <var>v</var>, const optional&l=
t;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt; con=
stexpr bool operator!=3D(const T&amp; <var>v</var>, const optional&lt;const=
 T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> !=3D *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
  <!-- -->
 =20
  <p class=3D"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><ins>template &=
lt;class T&gt; constexpr bool operator&lt;(const optional&lt;T&amp;&gt;&amp=
; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;clas=
s T&gt; constexpr bool operator&lt;(const optional&lt;const T&amp;&gt;&amp;=
 <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt; <v=
ar>v</var> : true</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class T&=
gt; constexpr bool operator&gt;(const T&amp; <var>v</var>, const optional&l=
t;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt; con=
stexpr bool operator&gt;(const T&amp; <var>v</var>, const optional&lt;const=
 T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt; *<v=
ar>x</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <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>);<br><ins>template &=
lt;class T&gt; constexpr bool operator&gt;(const optional&lt;T&amp;&gt;&amp=
; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &lt;clas=
s T&gt; constexpr bool operator&gt;(const optional&lt;const T&amp;&gt;&amp;=
 <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &gt; <v=
ar>v</var> : false</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;(const T&amp; <=
var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class T&=
gt; constexpr bool operator&lt;(const T&amp; <var>v</var>, const optional&l=
t;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&gt; con=
stexpr bool operator&lt;(const T&amp; <var>v</var>, const optional&lt;const=
 T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt; *<v=
ar>x</var> : false</code>.</p></dd>
  </dl>
 =20
  <!-- -->
=20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>templat=
e &lt;class T&gt; constexpr bool operator&gt;=3D(const optional&lt;T&amp;&g=
t;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &l=
t;class T&gt; constexpr bool operator&gt;=3D(const optional&lt;const T&amp;=
&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var>  &gt;=
=3D <var>v</var> : false</code>.</p></dd>
  </dl>
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class=
 T&gt; constexpr bool operator&lt;=3D(const T&amp; <var>v</var>, const opti=
onal&lt;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&g=
t; constexpr bool operator&lt;=3D(const T&amp; <var>v</var>, const optional=
&lt;const T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &lt;=3D =
*<var>x</var> : false</code>.</p></dd>
  </dl>
  =20
  =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&lt;=3D(const optio=
nal&lt;T&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);<br><ins>templat=
e &lt;class T&gt; constexpr bool operator&lt;=3D(const optional&lt;T&amp;&g=
t;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins><br><ins>template &l=
t;class T&gt; constexpr bool operator&lt;=3D(const optional&lt;const T&amp;=
&gt;&amp; <var>x</var>, const T&amp; <var>v</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? *<var>x</var> &lt;=3D=
 <var>v</var> : true</code>.</p></dd>
  </dl>
 =20
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt; constexpr bool operator&gt;=3D(const T&amp=
; <var>v</var>, const optional&lt;T&gt;&amp; x);<br><ins>template &lt;class=
 T&gt; constexpr bool operator&gt;=3D(const T&amp; <var>v</var>, const opti=
onal&lt;T&amp;&gt;&amp; <var>x</var>);</ins><br><ins>template &lt;class T&g=
t; constexpr bool operator&gt;=3D(const T&amp; <var>v</var>, const optional=
&lt;const T&amp;&gt;&amp; <var>x</var>);</ins></code>
  </p>
 =20
  <dl class=3D"attribute">
  <dt>Returns:</dt> <dd><p><code>bool(<var>x</var>) ? <var>v</var> &gt;=3D =
*<var>x</var> : true</code>.</p></dd>
  </dl>

</blockquote>

<p>In clause [optional.specalg] change: </p>


<blockquote class=3D"std">
 =20
  <p class=3D"function">
  <code>template &lt;class T&gt;<br>
    &nbsp;&nbsp;constexpr optional&lt;<del><em>V</em></del><ins>typename de=
cay&lt;T&gt;::type</ins>&gt; make_optional(T&amp;&amp; <var>v</var>);</code=
>
  </p>
 =20
  <dl class=3D"attribute">
    <dt>Returns:</dt>=20
    <dd><p><code>optional&lt;<del><em>V</em></del><ins>typename decay&lt;T&=
gt;::type</ins>&gt;(std::forward&lt;T&gt;(<var>v</var>))</code><ins>, <br>w=
here <code><var>V</var></code> is defined as <code><em>X</em>&amp;</code> i=
f <code>T</code> equals <code>reference_wrapper&lt;X&gt;</code>; otherwise =
<code><var>V</var></code> is <code>typename decay&lt;T&gt;::type</code></in=
s>.</p></dd>
  </dl>

</blockquote>

<p>In clause [optional.hash] add: </p>

<blockquote class=3D"std">
<h4><a name=3D"optional.hash"><del>20.5.12</del><ins>20.5.13</ins> Hash sup=
port <span style=3D"float:right">[optional.hash]</span></a></h4>


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

  <dl class=3D"attribute">
    <dt>Requires:</dt> <dd><p>the template specilaization <code>hash&lt;T&g=
t;</code> shall meet the requirements of class template <code>hash</code> (=
20.9.12).
      The template specilaization <code>hash&lt;optional&lt;T&gt;&gt;</code=
> shall meet the requirements of class template <code>hash</code>.=20
      For an object <code>o</code> of type <code>optional&lt;T&gt;</code>, =
if <code>bool(o) =3D=3D true</code>,=20
      <code>hash&lt;optional&lt;T&gt;&gt;()(o)</code> shall evaluate to the=
 same value as <code>hash&lt;T&gt;()(*o)</code>.</p></dd>=20
  </dl>
 =20
   <p class=3D"function">
  <ins><code>template &lt;class T&gt; struct hash&lt;optional&lt;T&amp;&gt;=
&gt;;</code></ins>
  </p>

  <dl class=3D"attribute">
    <dt><ins>Requires:</ins></dt> <dd><p><ins>the template specilaization <=
code>hash&lt;T&gt;</code> shall meet the requirements of class template <co=
de>hash</code> (20.9.12).
      The template specilaization <code>hash&lt;optional&lt;T&amp;&gt;&gt;<=
/code> shall meet the requirements of class template <code>hash</code>.=20
      For an object <code>o</code> of type <code>optional&lt;T&amp;&gt;</co=
de>, if <code>bool(o) =3D=3D true</code>,=20
      <code>hash&lt;optional&lt;T&amp;&gt;&gt;()(o)</code> shall evaluate t=
o the same value as <code>hash&lt;T&gt;()(*o)</code>.</ins></p></dd>=20
  </dl>
</blockquote>


<h2><a name=3D'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 o=
f this proposal can be found at <a href=3D"https://github.com/akrzemi1/Opti=
onal/">https://github.com/akrzemi1/Optional/</a>. Below we demonstrate how =
one can implement <code>optional</code>'s <code>constexpr</code> constructo=
rs 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 {
namespace experimental {

#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& v)  <em>// value ctor</em>
    : value_{v} {}

  ~optional_storage() =3D 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& v) : initialized_{true}, storage_{v} {}

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

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

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



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


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

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

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

<p>People in discussion group "ISO C++ Standard - Future Proposals" provide=
d numerous insightful suggestions: Vladimir Batov (who described and suppor=
ted the perfect forwarding constructor), Nevin Liber, Ville Voutilainen, Ri=
chard Smiths, Dave Abrahams, Chris Jefferson, Jeffrey Yasskin, Nikolay Ivch=
enkov, Matias Capeletto, Olaf van der Spek, Vincent Jacquet, Kazutoshi Sato=
da, Vicente J. Botet Escriba, R&oacute;bert D&aacute;vid, Vincent Jacquet, =
Luc Danton, 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 ove=
rloaded.</p>


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


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

<li>Fernando Cacciola, Boost.Optional library (<a href=3D'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=3D'http=
://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx'>http://msdn.microsoft.co=
m/en-us/library/1t3y8s4s.aspx</a></li>

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

<li>Jaakko J&auml;rvi, Boost Tuple Library (<a href=3D"http://www.boost.org=
/doc/libs/1_49_0/libs/tuple/doc/tuple_users_guide.html">http://www.boost.or=
g/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=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/=
papers/2011/n3248.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2=
011/n3248.pdf</a>)</li>

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

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



 =20

</body>
</html>

------=_Part_349_2402407.1360935974052--

.


Author: Tony V E <tvaneerd@gmail.com>
Date: Fri, 15 Feb 2013 17:19:35 -0500
Raw View
On Fri, Feb 15, 2013 at 8:46 AM, Andrzej Krzemie=F1ski <akrzemi1@gmail.com>=
 wrote:
> Hi everyone,
> We have updated the proposal based on recent feedback. I enclose the late=
st
> draft.
> We would appreciate your feedback, especially on the standardese.
>
> Regards,
> &rzej
>
> --
>

Is this Revision 4? (or have these all been revisions of Revision 3,
and it doesn't really become Revision 3 until presented to committee?)

-------

optional<int> om{1};              // om is engaged; its contained value is =
1
optional<int> on =3D om;            // on is engaged; its contained value i=
s 1
om =3D 2;                           // om is engaged; its contained value i=
s 2
assert (on !=3D om);                // on still contains 3. They are not po=
inters


on still contains 1 not 3

-------

*ol =3D 9;                          // the object contained in ol becomes 9
assert(*ol =3D=3D 9);

I'd prefer // the *value* contained in ol becomes 9
as you talk about contained value everywhere else,
and it keeps it conceptual, not implementation.

-------

only a basic exception sagety guarantee

safety

-------

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



.


Author: =?UTF-8?Q?Andrzej_Krzemie=C5=84ski?= <akrzemi1@gmail.com>
Date: Sat, 16 Feb 2013 10:21:07 -0800 (PST)
Raw View
------=_Part_333_22360257.1361038867738
Content-Type: text/plain; charset=ISO-8859-2
Content-Transfer-Encoding: quoted-printable



W dniu pi=B1tek, 15 lutego 2013 23:19:35 UTC+1 u=BFytkownik Tony V E napisa=
=B3:
>
>
> Is this Revision 4? (or have these all been revisions of Revision 3,=20
> and it doesn't really become Revision 3 until presented to committee?)=20
>
>
Thanks for the feedback. "Revision 3" has not yet been submitted to the=20
Committee, although we already have an allocated N-number. So we are still=
=20
discussing about sub-revisions of "Revision 3"

Regards,
&rzej

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



------=_Part_333_22360257.1361038867738
Content-Type: text/html; charset=ISO-8859-2
Content-Transfer-Encoding: quoted-printable

<br><br>W dniu pi=B1tek, 15 lutego 2013 23:19:35 UTC+1 u=BFytkownik Tony V =
E napisa=B3:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-lef=
t: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><br>Is this Revisi=
on 4? (or have these all been revisions of Revision 3,
<br>and it doesn't really become Revision 3 until presented to committee?)
<br>
<br></blockquote><div><br>Thanks for the feedback. "Revision 3" has not yet=
 been submitted to the Committee, although we already have an allocated N-n=
umber. So we are still discussing about sub-revisions of "Revision 3"<br><b=
r>Regards,<br>&amp;rzej<br></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_333_22360257.1361038867738--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Sat, 16 Feb 2013 22:30:03 +0100
Raw View
This is a multi-part message in MIME format.
--------------010209000306010006010405
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 15/02/13 14:46, Andrzej Krzemien'ski a =E9crit :
> Hi everyone,
> We have updated the proposal based on recent feedback. I enclose the=20
> latest draft. It is also available at:
> http://kojot.sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html=20
> <http://kojot.sggw.waw.pl/%7Eakrzemi1/optional/tr2.optional.proposal.html=
>
>
> We would appreciate your feedback, especially on the standardese.
>
>
Hi,

Next follows some remarks

* As optional references are part of the auxiliary proposal, shouldn't=20
the following be removed from 20.5.1?

An /optional object for lvalue reference types/ is an object capable of=20
storing the address of another object. The address stored by the=20
optional object can be changed or set to a value that does not represent=20
a valid address.

|* optional<T>::optional(const optional<T>& rhs);|

What about adding a post-condition on the copied value
|false =3D=3D bool(rhs)| || rhs.value() =3D=3D this->value()

What about adding noexecpt
|optional<T>::optional(const optional<T>& rhs) noecept(see below);
|
||Remarks:||The expression inside |noexcept| is equivalent to:|

    is_nothrow_copy_constructible<T>::value

|

|* optional<T>::optional(const T& v);
|

|I'm curious to know How the remark can be implemented in C++11
|

||Remarks:||If |T|'s selected constructor is a |constexpr| constructor,=20
this constructor shall be a |constexpr| constructor.

What about adding a post-condition on the copied value
v =3D=3D this->value()

What about noexcept(is_nothrow_copy_constructible<T>::value||) as before?

|
* There is an assignment from U, but not a constructor from U. Is this=20
missing?

|||
|* template <class T> constexpr bool operator=3D=3D(const optional<T>& x,=
=20
const optional<T>& y);


I don't understand the remark |

    Instantiations of this function template for which |*x =3D=3D *y| is a
    core constant expression, shall be |constexpr| functions.

|Is the function a constexpr or not?

Best,
Vicente


|

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



--------------010209000306010006010405
Content-Type: text/html; charset=ISO-8859-1

<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Le 15/02/13 14:46, Andrzej Krzemie&#324;ski
      a &eacute;crit&nbsp;:<br>
    </div>
    <blockquote
      cite="mid:c689b87e-bb6f-42a4-83d7-50d1e395d13d@isocpp.org"
      type="cite">Hi everyone,<br>
      We have updated the proposal based on recent feedback. I enclose
      the latest draft. It is also available at:<br>
      <a moz-do-not-send="true"
href="http://kojot.sggw.waw.pl/%7Eakrzemi1/optional/tr2.optional.proposal.html">http://kojot.sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html</a><br>
      <br>
      <p>We would appreciate your feedback, especially on the
        standardese.</p>
      <br>
    </blockquote>
    Hi,<br>
    <br>
    Next follows some remarks <br>
    <br>
    * As optional references are part of the auxiliary proposal,
    shouldn't the following be removed from 20.5.1?<br>
    <br>
    <meta http-equiv="content-type" content="text/html;
      charset=ISO-8859-1">
    An <em>optional object for lvalue reference types</em> is an object
    capable of storing the address of another object. The address stored
    by the optional object can be changed or set to a value that does
    not represent a valid address. <br>
    <br>
    <meta http-equiv="content-type" content="text/html;
      charset=ISO-8859-1">
    <p class="function"> <code>* optional&lt;T&gt;::optional(const
        optional&lt;T&gt;&amp; <var>rhs</var>);</code><br>
    </p>
    <p class="function">What about adding a post-condition on the copied
      value<br>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <code><var>false</var> == bool(rhs)</code> || rhs.value() ==
      this-&gt;value()<br>
    </p>
    What about adding noexecpt<br>
    <code>optional&lt;T&gt;::optional(const optional&lt;T&gt;&amp; <var>rhs</var>)
      noecept(see below);<br>
    </code><br>
    <code>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
    </code>Remarks:<code> </code>The expression inside <code>noexcept</code>
    is equivalent to:<code>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
      <dd>
        <pre>is_nothrow_copy_constructible&lt;T&gt;::value</pre>
      </dd>
    </code>
    <p>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
    </p>
    <p class="function"> <code>* optional&lt;T&gt;::optional(const
        T&amp; <var>v</var>);<br>
      </code></p>
    <p class="function"><code>I'm curious to know How the remark can be
        implemented in C++11<br>
      </code>
      <meta http-equiv="content-type" content="text/html;
        charset=ISO-8859-1">
    </p>
    <p class="function"><dt><code></code>Remarks:<code> </code>If <code>T</code>'s
        selected constructor is a <code>constexpr</code> constructor,
        this constructor shall be a <code>constexpr</code> constructor.</dt>
    </p>
    <p>What about adding a post-condition on the copied value<br>
      v == this-&gt;value()</p>
    <p>
      What about noexcept(is_nothrow_copy_constructible&lt;T&gt;::value<code></code>)
      as before?<br>
    </p>
    <code><br>
      * There is an assignment from U, but not a constructor from U. Is
      this missing?<br>
      <br>
    </code><code></code><br>
    <code>* template &lt;class T&gt; constexpr bool operator==(const
      optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);<br>
      <br>
      <br>
      I don't understand the remark </code>
    <meta http-equiv="content-type" content="text/html;
      charset=ISO-8859-1">
    <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>
    <code>Is the function a constexpr or not?<br>
      <br>
      Best,<br>
      Vicente<br>
      <br>
      <br>
    </code>
  </body>
</html>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en">http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en</a>.<br />
&nbsp;<br />
&nbsp;<br />

--------------010209000306010006010405--

.


Author: Tony V E <tvaneerd@gmail.com>
Date: Sun, 17 Feb 2013 13:34:50 -0500
Raw View
On Sat, Feb 16, 2013 at 4:30 PM, Vicente J. Botet Escriba
<vicente.botet@wanadoo.fr> wrote:
>
>
> * optional<T>::optional(const optional<T>& rhs);
>
> What about adding a post-condition on the copied value
> false == bool(rhs) || rhs.value() == this->value()
>

You can't guarantee rhs.value() == this->value() for all T.  You are
probably assuming T is Regular.


>
> What about adding a post-condition on the copied value
> v == this->value()
>

Same here.


Tony

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



.


Author: Tony V E <tvaneerd@gmail.com>
Date: Sun, 17 Feb 2013 13:35:45 -0500
Raw View
On Sun, Feb 17, 2013 at 1:34 PM, Tony V E <tvaneerd@gmail.com> wrote:
> On Sat, Feb 16, 2013 at 4:30 PM, Vicente J. Botet Escriba
> <vicente.botet@wanadoo.fr> wrote:
>>
>>
>> * optional<T>::optional(const optional<T>& rhs);
>>
>> What about adding a post-condition on the copied value
>> false == bool(rhs) || rhs.value() == this->value()
>>
>
> You can't guarantee rhs.value() == this->value() for all T.  You are
> probably assuming T is Regular.
>
>
>>
>> What about adding a post-condition on the copied value
>> v == this->value()
>>
>
> Same here.
>
>
> Tony

P.S.

Doesn't mean you can't add some wording, but it might not be quite as
simple as you would expect.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



.


Author: Chris Jefferson <chris@bubblescope.net>
Date: Sun, 17 Feb 2013 18:57:37 +0000
Raw View
On 17/02/13 18:34, Tony V E wrote:
> On Sat, Feb 16, 2013 at 4:30 PM, Vicente J. Botet Escriba
> <vicente.botet@wanadoo.fr> wrote:
>>
>> * optional<T>::optional(const optional<T>& rhs);
>>
>> What about adding a post-condition on the copied value
>> false == bool(rhs) || rhs.value() == this->value()
>>
> You can't guarantee rhs.value() == this->value() for all T.  You are
> probably assuming T is Regular.

In these kinds of situations I find it helpful to look at other parts of
the standard, for example pair assignment says "effects: Initializes
members from the corresponding members of the argument".

So don't worry about getting 'code equivalent', in situations like this
it is fairly obvious what the constructor is supposed to do, so a text
description is (my opinion) more than sufficient.

Chris

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



.


Author: =?UTF-8?Q?Andrzej_Krzemie=C5=84ski?= <akrzemi1@gmail.com>
Date: Mon, 18 Feb 2013 08:42:24 -0800 (PST)
Raw View
------=_Part_521_25082555.1361205744491
Content-Type: text/plain; charset=ISO-8859-2
Content-Transfer-Encoding: quoted-printable



W dniu sobota, 16 lutego 2013 22:30:03 UTC+1 u=BFytkownik Vicente J. Botet=
=20
Escriba napisa=B3:
>
>  Le 15/02/13 14:46, Andrzej Krzemie=F1ski a =E9crit :
> =20
> Hi everyone,
> We have updated the proposal based on recent feedback. I enclose the=20
> latest draft. It is also available at:
> http://kojot.sggw.waw.pl/~akrzemi1/optional/tr2.optional.proposal.html
>
> We would appreciate your feedback, especially on the standardese.
>
>  Hi,
>
> Next follows some remarks=20
>
> * As optional references are part of the auxiliary proposal, shouldn't th=
e=20
> following be removed from 20.5.1?
>
> An *optional object for lvalue reference types* is an object capable of=
=20
> storing the address of another object. The address stored by the optional=
=20
> object can be changed or set to a value that does not represent a valid=
=20
> address.=20
>

Thanks for the input Vicente. I will fix this one.
 =20

>
>  * optional<T>::optional(const optional<T>& rhs);
> =20
> What about adding a post-condition on the copied value
> false =3D=3D bool(rhs) || rhs.value() =3D=3D this->value()
>

I initially put it. But Tony observed that for non-regular types it cannot=
=20
be guaranteed. The requirement that the contained value should be crated as=
=20
though copy-initialized should be a sufficiently clear requirement.=20

>  What about adding noexecpt
> optional<T>::optional(const optional<T>& rhs) noecept(see below);
>
>  Remarks: The expression inside noexcept is equivalent to:=20
>
> is_nothrow_copy_constructible<T>::value
>
>
Again, initially a conditional noexcept was applied aggressively to nearly=
=20
every function in the proposal. But we were advised to follow the Library=
=20
style where only move operations and swap are (sometimes conditionally)=20
constexpr. We adapted to the Library style.

>    * optional<T>::optional(const T& v);
> =20
> I'm curious to know How the remark can be implemented in C++11
> =20
> Remarks: If T's selected constructor is a constexpr constructor, this=20
> constructor shall be a constexpr constructor.
>

This is tricky, but possible with the clever use of a unions. It is working=
=20
in reference implementation: https://github.com/akrzemi1/Optional/
The overview of the solution is also provided at the end of the proposal.=
=20
Implementations may also use implementation-specific additions to satisfy=
=20
this guarantee.

>  What about adding a post-condition on the copied value
> v =3D=3D this->value()
>
> What about noexcept(is_nothrow_copy_constructible<T>::value) as before?
> =20
> * There is an assignment from U, but not a constructor from U. Is this=20
> missing?
>

The assignment from U is not really an assignment from U. The remark says=
=20
"The function shall not participate in overload resolution unless is_same<t=
ypename=20
remove_reference<U>::type, T>::value is true" Thus U is effectively T. The=
=20
reason to provide such generic assignment and then constraining it so that=
=20
effectively T =3D=3D U is to guarantee that assignment of the form o =3D {}=
 is=20
unambiguous


>
> * template <class T> constexpr bool operator=3D=3D(const optional<T>& x, =
const=20
> optional<T>& y);
>
>
> I don't understand the remark =20
>
> Instantiations of this function template for which *x =3D=3D *y is a core=
=20
> constant expression, shall be constexpr functions.
>  Is the function a constexpr or not?
>

In the Standard, sect 7.1.5, para 6 we have: =20

If the instantiated template specialization of a constexpr function=20
> template or member function of a class
> template would fail to satisfy the requirements for a constexpr function=
=20
> or constexpr constructor, that
> specialization is not a constexpr function or constexpr constructor. [ *
> Note:* If the function is a member
> function it will still be const as described below. *--end note* ] If no=
=20
> specialization of the template would
> yield a constexpr function or constexpr constructor, the program is=20
> ill-formed; no diagnostic required.
>

This gives implementation the freedom to provide a constexpr function=20
templates whose instantiations are not constexpr functions. We want to=20
limit this freedom.=20

Regards,
&rzej

--=20

---=20
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/?hl=3Den.



------=_Part_521_25082555.1361205744491
Content-Type: text/html; charset=ISO-8859-2
Content-Transfer-Encoding: quoted-printable

<br><br>W dniu sobota, 16 lutego 2013 22:30:03 UTC+1 u=BFytkownik Vicente J=
.. Botet Escriba napisa=B3:<blockquote class=3D"gmail_quote" style=3D"margin=
: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
 =20
   =20
 =20
  <div bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div>Le 15/02/13 14:46, Andrzej Krzemie=F1ski
      a =E9crit&nbsp;:<br>
    </div>
    <blockquote type=3D"cite">Hi everyone,<br>
      We have updated the proposal based on recent feedback. I enclose
      the latest draft. It is also available at:<br>
      <a href=3D"http://kojot.sggw.waw.pl/%7Eakrzemi1/optional/tr2.optional=
..proposal.html" target=3D"_blank">http://kojot.sggw.waw.pl/~<wbr>akrzemi1/o=
ptional/tr2.<wbr>optional.proposal.html</a><br>
      <br>
      <p>We would appreciate your feedback, especially on the
        standardese.</p>
      <br>
    </blockquote>
    Hi,<br>
    <br>
    Next follows some remarks <br>
    <br>
    * As optional references are part of the auxiliary proposal,
    shouldn't the following be removed from 20.5.1?<br>
    <br>
   =20
    An <i>optional object for lvalue reference types</i> is an object
    capable of storing the address of another object. The address stored
    by the optional object can be changed or set to a value that does
    not represent a valid address. <br></div></blockquote><div><br>Thanks f=
or the input Vicente. I will fix this one.<br>&nbsp; <br></div><blockquote =
class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1p=
x #ccc solid;padding-left: 1ex;"><div bgcolor=3D"#FFFFFF" text=3D"#000000">
    <br>
   =20
    <p> <code>* optional&lt;T&gt;::optional(const
        optional&lt;T&gt;&amp; <var>rhs</var>);</code><br>
    </p>
    <p>What about adding a post-condition on the copied
      value<br>
     =20
      <code><var>false</var> =3D=3D bool(rhs)</code> || rhs.value() =3D=3D
      this-&gt;value()<br></p></div></blockquote><div><br>I initially put i=
t. But Tony observed that for non-regular types it cannot be guaranteed. Th=
e requirement that the contained value should be crated as though copy-init=
ialized should be a sufficiently clear requirement. <br></div><blockquote c=
lass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px=
 #ccc solid;padding-left: 1ex;"><div bgcolor=3D"#FFFFFF" text=3D"#000000"><=
p>
    </p>
    What about adding noexecpt<br>
    <code>optional&lt;T&gt;::optional(const optional&lt;T&gt;&amp; <var>rhs=
</var>)
      noecept(see below);<br>
    </code><br>
    <code>
     =20
    </code>Remarks:<code> </code>The expression inside <code>noexcept</code=
>
    is equivalent to:<code>
     =20
      <dd>
        <pre>is_nothrow_copy_constructible&lt;<wbr>T&gt;::value</pre></dd><=
/code></div></blockquote><div><br>Again, initially a conditional noexcept w=
as applied aggressively to nearly every function in the proposal. But we we=
re advised to follow the Library style where only move operations and swap =
are (sometimes conditionally) constexpr. We adapted to the Library style.<b=
r></div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0=
..8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div bgcolor=3D"#FFFFF=
F" text=3D"#000000"><code><dd>
      </dd>
    </code>
    <p>
     =20
    </p>
    <p> <code>* optional&lt;T&gt;::optional(const
        T&amp; <var>v</var>);<br>
      </code></p>
    <p><code>I'm curious to know How the remark can be
        implemented in C++11<br>
      </code>
     =20
    </p>
    <p></p><dt><code></code>Remarks:<code> </code>If <code>T</code>'s
        selected constructor is a <code>constexpr</code> constructor,
        this constructor shall be a <code>constexpr</code> constructor.</dt=
></div></blockquote><div><br>This is tricky, but possible with the clever u=
se of a unions. It is working in reference implementation: https://github.c=
om/akrzemi1/Optional/<br>The overview of the solution is also provided at t=
he end of the proposal. Implementations may also use implementation-specifi=
c additions to satisfy this guarantee.<br></div><blockquote class=3D"gmail_=
quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;pa=
dding-left: 1ex;"><div bgcolor=3D"#FFFFFF" text=3D"#000000">
    <p></p>
    <p>What about adding a post-condition on the copied value<br>
      v =3D=3D this-&gt;value()</p>
    <p>
      What about noexcept(is_nothrow_copy_<wbr>constructible&lt;T&gt;::valu=
e<code></code>)
      as before?<br>
    </p>
    <code><br>
      * There is an assignment from U, but not a constructor from U. Is
      this missing?<br></code></div></blockquote><div><br>The assignment fr=
om U is not really an assignment from U. The remark says "The function shal=
l not participate in overload resolution unless  <code>is_same&lt;typename =
remove_reference&lt;U&gt;::type, T&gt;::value</code> is  <code>true</code>"=
 Thus U is effectively T.  The reason to provide such generic assignment an=
d then constraining it so that effectively <code>T</code> =3D=3D <code>U</c=
ode> is to guarantee that assignment of the form <code>o =3D {}</code> is u=
nambiguous<br><br></div><blockquote class=3D"gmail_quote" style=3D"margin: =
0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div b=
gcolor=3D"#FFFFFF" text=3D"#000000"><code>
      <br>
    </code><code></code><br>
    <code>* template &lt;class T&gt; constexpr bool operator=3D=3D(const
      optional&lt;T&gt;&amp; x, const optional&lt;T&gt;&amp; y);<br>
      <br>
      <br>
      I don't understand the remark </code>
   =20
    <dd>
      <p>Instantiations of this function template for which <code>*x =3D=3D
          *y</code> is a core constant expression, shall be <code>constexpr=
</code>
        functions.</p>
    </dd>
    <code>Is the function a constexpr or not?<br></code></div></blockquote>=
<div><br>In the Standard, sect 7.1.5, para 6 we have:&nbsp; <br><br><blockq=
uote style=3D"margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 20=
4, 204); padding-left: 1ex;" class=3D"gmail_quote">If the instantiated temp=
late specialization of a <span style=3D"font-family: courier new,monospace;=
">constexpr </span>function template or member function of a class<br>templ=
ate would fail to satisfy the requirements for a <span style=3D"font-family=
: courier new,monospace;">constexpr </span>function or <span style=3D"font-=
family: courier new,monospace;">constexpr </span>constructor, that<br>speci=
alization is not a <span style=3D"font-family: courier new,monospace;">cons=
texpr </span>function or <span style=3D"font-family: courier new,monospace;=
">constexpr </span>constructor. [ <i>Note:</i> If the function is a member<=
br>function it will still be const as described below. <i>&mdash;end note</=
i> ] If no specialization of the template would<br>yield a <span style=3D"f=
ont-family: courier new,monospace;">constexpr </span>function or <span styl=
e=3D"font-family: courier new,monospace;">constexpr </span>constructor, the=
 program is ill-formed; no diagnostic required.<br></blockquote><div><br>Th=
is gives implementation the freedom to provide a constexpr function templat=
es whose instantiations are not constexpr functions. We want to limit this =
freedom. <br><br>Regards,<br>&amp;rzej<br></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_521_25082555.1361205744491--

.