[optional.syn]

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

## 22.5 Optional objects [[optional]](optional#syn)

### 22.5.2 Header <optional> synopsis [optional.syn]

[ð](#header:%3coptional%3e)

// mostly freestanding#include <compare> // see [[compare.syn]](compare.syn "17.12.1 Header <compare> synopsis")namespace std {// [[optional.optional]](optional.optional "22.5.3 Class template optional"), class template optionaltemplate<class T>class optional; // partially freestanding// [[optional.optional.ref]](optional.optional.ref "22.5.4 Partial specialization of optional for reference types"), partial specialization of optional for lvalue reference typestemplate<class T>class optional<T&>; // partially freestandingtemplate<class T>constexpr bool ranges::enable_view<optional<T>> = true; template<class T>constexpr auto format_kind<optional<T>> = range_format::disabled; template<class T>constexpr bool ranges::enable_borrowed_range<optional<T&>> = true; template<class T>concept [*is-derived-from-optional*](#concept:is-derived-from-optional "22.5.2 Header <optional> synopsis [optional.syn]") = requires(const T& t) { // *exposition only*[]<class U>(const optional<U>&){ }(t); }; // [[optional.nullopt]](optional.nullopt "22.5.5 No-value state indicator"), no-value state indicatorstruct nullopt_t{*see below*}; inline constexpr nullopt_t nullopt(*unspecified*); // [[optional.bad.access]](optional.bad.access "22.5.6 Class bad_­optional_­access"), class bad_optional_accessclass bad_optional_access; // [[optional.relops]](optional.relops "22.5.7 Relational operators"), relational operatorstemplate<class T, class U>constexpr bool operator==(const optional<T>&, const optional<U>&); template<class T, class U>constexpr bool operator!=(const optional<T>&, const optional<U>&); template<class T, class U>constexpr bool operator<(const optional<T>&, const optional<U>&); template<class T, class U>constexpr bool operator>(const optional<T>&, const optional<U>&); template<class T, class U>constexpr bool operator<=(const optional<T>&, const optional<U>&); template<class T, class U>constexpr bool operator>=(const optional<T>&, const optional<U>&); template<class T, [three_way_comparable_with](cmp.concept#concept:three_way_comparable_with "17.12.4 Concept three_­way_­comparable [cmp.concept]")<T> U>constexpr compare_three_way_result_t<T, U>operator<=>(const optional<T>&, const optional<U>&); // [[optional.nullops]](optional.nullops "22.5.8 Comparison with nullopt"), comparison with nullopttemplate<class T> constexpr bool operator==(const optional<T>&, nullopt_t) noexcept; template<class T>constexpr strong_ordering operator<=>(const optional<T>&, nullopt_t) noexcept; // [[optional.comp.with.t]](optional.comp.with.t "22.5.9 Comparison with T"), comparison with Ttemplate<class T, class U> constexpr bool operator==(const optional<T>&, const U&); template<class T, class U> constexpr bool operator==(const T&, const optional<U>&); template<class T, class U> constexpr bool operator!=(const optional<T>&, const U&); template<class T, class U> constexpr bool operator!=(const T&, const optional<U>&); template<class T, class U> constexpr bool operator<(const optional<T>&, const U&); template<class T, class U> constexpr bool operator<(const T&, const optional<U>&); template<class T, class U> constexpr bool operator>(const optional<T>&, const U&); template<class T, class U> constexpr bool operator>(const T&, const optional<U>&); template<class T, class U> constexpr bool operator<=(const optional<T>&, const U&); template<class T, class U> constexpr bool operator<=(const T&, const optional<U>&); template<class T, class U> constexpr bool operator>=(const optional<T>&, const U&); template<class T, class U> constexpr bool operator>=(const T&, const optional<U>&); template<class T, class U>requires (![*is-derived-from-optional*](#concept:is-derived-from-optional "22.5.2 Header <optional> synopsis [optional.syn]")<U>) && [three_way_comparable_with](cmp.concept#concept:three_way_comparable_with "17.12.4 Concept three_­way_­comparable [cmp.concept]")<T, U>constexpr compare_three_way_result_t<T, U>operator<=>(const optional<T>&, const U&); // [[optional.specalg]](optional.specalg "22.5.10 Specialized algorithms"), specialized algorithmstemplate<class T>constexpr void swap(optional<T>&, optional<T>&) noexcept(*see below*); template<class T>constexpr optional<decay_t<T>> make_optional(T&&); template<class T, class... Args>constexpr optional<T> make_optional(Args&&... args); template<class T, class U, class... Args>constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args); // [[optional.hash]](optional.hash "22.5.11 Hash support"), hash supporttemplate<class T> struct hash; template<class T> struct hash<optional<T>>;}
