cpp/experimental/optional: Difference between revisions
From cppreference.com
< cpp | experimental
Kreios4004 (talk | contribs) m Fixed a grammar mistake. |
Andreas Krug (talk | contribs) m fmt |
||
| (19 intermediate revisions by 6 users not shown) | |||
| Line 1: | Line 1: | ||
{{cpp/title|optional}} | {{cpp/title|optional}} | ||
{{cpp/ | {{cpp//optional/navbar}} | ||
{{fmbox | style=font-size: 0.8em | {{fmbox|style=font-size: 0.8em|text=C++into the C++ as of }} | ||
{{ddcl | header=optional | since= | |||
{{ddcl|header=optional|since=| | |||
template< class T > | template< class T > | ||
class optional; | class optional; | ||
}} | }} | ||
The class template {{tt|std::optional}} manages an ''optional'' contained value, i.e. a value that | The class template {{tt|std::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. | ||
{{tt|optional}} not a . | |||
value {{tt|optional}} object an {{tt|optional}} object a , the . | |||
{{|}} object '''' | |||
The {{tt|}} | |||
* The object is assigned | |||
* The object is assigned {{tt|optional}} . | |||
The object | The object '''' the following conditions: | ||
* The object is default-initialized. | * The object is default-initialized. | ||
* The object is initialized with a value of {{lc|std::nullopt_t}} or {{tt|optional}} object . | |||
* The object is initialized with a value of {{lc|std::nullopt_t}} or | * The object is assigned a value of {{lc|std::nullopt_t}} or {{tt|optional}} . | ||
* The object is assigned a value of {{lc|std::nullopt_t}} or | |||
===Template parameters=== | ===Template parameters=== | ||
{{par begin}} | {{par begin}} | ||
{{par | T | the type of the value to manage initialization state for. The type must meet the requirements of {{ | {{par|T|the type of the value to manage initialization state for. The type must meet the requirements of {{|Destructible}}}} | ||
{{par end}} | {{par end}} | ||
===Member types=== | ===Member types=== | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc hitem | Member type | Definition}} | {{dsc hitem|Member type|Definition}} | ||
{{dsc | {{tt|value_type}} | {{tt|T}} }} | {{dsc|{{tt|value_type}}|{{tt|T}}}} | ||
{{dsc end}} | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc constructor}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc destructor}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc operator{{=}}}} | ||
{{dsc h2 | Observers}} | {{dsc h2|Observers}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc operator*}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc operator bool}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc value}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc value_or}} | ||
{{dsc h2 | Modifiers}} | {{dsc h2|Modifiers}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc swap}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc emplace | ||
}} | |||
{{dsc end}} | {{dsc end}} | ||
===Non-member functions=== | ===Non-member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc operator_cmp}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc make_optional}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc swap2}} | ||
{{dsc end}} | {{dsc end}} | ||
===Helper classes=== | ===Helper classes=== | ||
{{dsc begin}} | {{dsc begin}} | ||
{{dsc inc | cpp/ | {{dsc inc|cpp//optional/dsc hash | ||
}} | |||
{{dsc end}} | {{dsc end}} | ||