[optional.optional.general]

# 22 General utilities library [[utilities]](./#utilities)

## 22.5 Optional objects [[optional]](optional#optional.general)

### 22.5.3 Class template optional [[optional.optional]](optional.optional#general)

#### 22.5.3.1 General [optional.optional.general]

[ð](#lib:optional)

namespace std {template<class T>class optional {public:using value_type = T; using iterator = *implementation-defined*; // see [[optional.iterators]](optional.iterators "22.5.3.6 Iterator support")using const_iterator = *implementation-defined*; // see [[optional.iterators]](optional.iterators "22.5.3.6 Iterator support")// [[optional.ctor]](optional.ctor "22.5.3.2 Constructors"), constructorsconstexpr optional() noexcept; constexpr optional(nullopt_t) noexcept; constexpr optional(const optional&); constexpr optional(optional&&) noexcept(*see below*); template<class... Args>constexpr explicit optional(in_place_t, Args&&...); template<class U, class... Args>constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...); template<class U = remove_cv_t<T>>constexpr explicit(*see below*) optional(U&&); template<class U>constexpr explicit(*see below*) optional(const optional<U>&); template<class U>constexpr explicit(*see below*) optional(optional<U>&&); // [[optional.dtor]](optional.dtor "22.5.3.3 Destructor"), destructorconstexpr ~optional(); // [[optional.assign]](optional.assign "22.5.3.4 Assignment"), assignmentconstexpr optional& operator=(nullopt_t) noexcept; constexpr optional& operator=(const optional&); constexpr optional& operator=(optional&&) noexcept(*see below*); template<class U = remove_cv_t<T>> constexpr optional& operator=(U&&); template<class U> constexpr optional& operator=(const optional<U>&); template<class U> constexpr optional& operator=(optional<U>&&); template<class... Args> constexpr T& emplace(Args&&...); template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...); // [[optional.swap]](optional.swap "22.5.3.5 Swap"), swapconstexpr void swap(optional&) noexcept(*see below*); // [[optional.iterators]](optional.iterators "22.5.3.6 Iterator support"), iterator supportconstexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept; // [[optional.observe]](optional.observe "22.5.3.7 Observers"), observersconstexpr const T* operator->() const noexcept; constexpr T* operator->() noexcept; constexpr const T& operator*() const & noexcept; constexpr T& operator*() & noexcept; constexpr T&& operator*() && noexcept; constexpr const T&& operator*() const && noexcept; constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; constexpr const T& value() const &; // freestanding-deletedconstexpr T& value() &; // freestanding-deletedconstexpr T&& value() &&; // freestanding-deletedconstexpr const T&& value() const &&; // freestanding-deletedtemplate<class U = remove_cv_t<T>> constexpr T value_or(U&&) const &; template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&; // [[optional.monadic]](optional.monadic "22.5.3.8 Monadic operations"), monadic operationstemplate<class F> constexpr auto and_then(F&& f) &; template<class F> constexpr auto and_then(F&& f) &&; template<class F> constexpr auto and_then(F&& f) const &; template<class F> constexpr auto and_then(F&& f) const &&; template<class F> constexpr auto transform(F&& f) &; template<class F> constexpr auto transform(F&& f) &&; template<class F> constexpr auto transform(F&& f) const &; template<class F> constexpr auto transform(F&& f) const &&; template<class F> constexpr optional or_else(F&& f) &&; template<class F> constexpr optional or_else(F&& f) const &; // [[optional.mod]](optional.mod "22.5.3.9 Modifiers"), modifiersconstexpr void reset() noexcept; private: T* *val*; // *exposition only*}; template<class T> optional(T) -> optional<T>;}

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3364)

Any instance of optional<T> at any given time either contains a value or does not contain a value[.](#1.sentence-1)

When an instance of optional<T> [*contains a value*](#def:contains_a_value,optional "22.5.3.1 General [optional.optional.general]"),
it means that an object of type T, referred to as the optional object's [*contained value*](#def:contained_value,optional "22.5.3.1 General [optional.optional.general]"),
is nested within ([[intro.object]](intro.object "6.8.2 Object model")) the optional object[.](#1.sentence-2)

When an object of type optional<T> is contextually converted to bool,
the conversion returns true if the object contains a value;
otherwise the conversion returns false[.](#1.sentence-3)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3373)

When an optional<T> object contains a value,
member val points to the contained value[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3377)

A type X is a[*valid contained type*](#def:valid_contained_type,optional "22.5.3.1 General [optional.optional.general]") for optional if X is an lvalue reference type or a complete non-array object type,
and remove_cvref_t<X> is a type other than in_place_t or nullopt_t[.](#3.sentence-1)

If a specialization of optional is instantiated with a type T that is not a valid contained type for optional, the program is ill-formed[.](#3.sentence-2)

If T is an object type,T shall meet the *Cpp17Destructible* requirements (Table [35](utility.arg.requirements#tab:cpp17.destructible "Table 35: Cpp17Destructible requirements"))[.](#3.sentence-3)
