cpp/experimental/optional: Difference between revisions
From cppreference.com
< cpp | experimental
+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}} | ||
{{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 | 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. | ||
{{tt|optional}} not a . | |||
value {{tt|optional}} object an {{tt|optional}} object a , the . | |||
The {{tt|optional}} object | |||
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 '''' 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 | * 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 | |||
===Template parameters=== | ===Template parameters=== | ||