Namespaces
Variants

cpp/experimental/optional: Difference between revisions

From cppreference.com
+nullopt
engaged/disengaged is not in the TS. It's "contains a value" now.
Line 1: Line 1:
{{cpp/title|n=experimental::|optional}}
{{cpp/title|n=experimental::|optional}}
{{cpp/experimental/optional/navbar}}
{{cpp/experimental/optional/navbar}}
{{fmbox | style=font-size: 0.8em; background-color:#ffffbb | text=After reviewing national body comments to N3690, this library component was voted out from C++14 working paper into a separate Technical Specification. It is not a part of the draft C++14 as of n3797.}}
{{fmbox | style=font-size: 0.8em; background-color:#ffffbb | text=This is work in progress, there may be inaccuracies}}
{{ddcl | header=experimental/optional | since=libfund_ts |
{{ddcl | header=experimental/optional | since=libfund_ts |
template< class T >
template< class T >
Line 8: Line 6:
}}
}}


The class template {{tt|std::experimental::optional}} manages an ''optional'' contained value, i.e. a value that semantically may not be present.
The class template {{tt|std::experimental::optional}} manages an ''optional'' contained value, i.e. a value that may not be present.


A common use case for {{tt|optional}} is the return value of a function that may fail. As opposed to other approaches, such as {{c|std::pair<T,bool>}}, {{tt|optional}} handles expensive to construct objects well and is more readable, as the intent is expressed explicitly.
A common use case for {{tt|optional}} is the return value of a function that may fail. As opposed to other approaches, such as {{c|std::pair<T,bool>}}, {{tt|optional}} handles expensive to construct objects well and is more readable, as the intent is expressed explicitly.


The value is guaranteed to be allocated within the {{tt|optional}} object itself, i.e. no dynamic memory allocation ever takes place. Thus, an {{tt|optional}} object models an object, not a pointer, even though the {{lc|operator*()}} and {{lc|operator->()}} are defined.
{{tt|optional}} not a .


The value inside an {{tt|optional}} object may be in either an initialized or uninitialized state.  An {{tt|optional}} object with a value in initialized state is called ''engaged'', whereas if the value is in uninitialized state, the object is called ''disengaged''.
value {{tt|optional}} object an {{tt|optional}} object a , the .


The {{tt|optional}} object is ''engaged'' on the following conditions:
 
The {{tt|optional}} object '''' the following conditions:


* The object is initialized with a value of type {{tt|T}}
* The object is initialized with a value of type {{tt|T}}


* The object is assigned an ''engaged'' {{tt|optional}}.
The object '''' the following conditions:
 
The object is ''disengaged'' on the following conditions:


* The object is default-initialized.
* The object is default-initialized.
 
* The object is initialized with a value of {{lc|std::experimental::nullopt_t}} or {{tt|optional}} object .
* The object is initialized with a value of {{lc|std::experimental::nullopt_t}} or a ''disengaged'' {{tt|optional}} object.
* The object is assigned a value of {{lc|std::experimental::nullopt_t}} or {{tt|optional}}
 
* The object is assigned a value of {{lc|std::experimental::nullopt_t}} or a ''disengaged'' {{tt|optional}}.


===Template parameters===
===Template parameters===

Revision as of 03:14, 16 May 2015