Files
cppdraft_translate/cppdraft/optional.md
2025-10-25 03:02:53 +03:00

95 KiB
Raw Permalink Blame History

[optional]

22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.1 General [optional.general]

1

#

Subclause [optional] describes class template optional that represents optional objects.

An optional object is an object that contains the storage for another object and manages the lifetime of this contained object, if any.

The contained object may be initialized after the optional object has been initialized, and may be destroyed before the optional object has been destroyed.

The initialization state of the contained object is tracked by the optional object.

22.5.2 Header synopsis [optional.syn]

🔗

// mostly freestanding#include // see [compare.syn]namespace std {// [optional.optional], class template optionaltemplateclass optional; // partially freestanding// [optional.optional.ref], partial specialization of optional for lvalue reference typestemplateclass optional<T&>; // partially freestandingtemplateconstexpr bool ranges::enable_view<optional> = true; templateconstexpr auto format_kind<optional> = range_format::disabled; templateconstexpr bool ranges::enable_borrowed_range<optional<T&>> = true; templateconcept is-derived-from-optional = requires(const T& t) { // exposition only[](const optional&){ }(t); }; // [optional.nullopt], no-value state indicatorstruct nullopt_t{see below}; inline constexpr nullopt_t nullopt(unspecified); // [optional.bad.access], class bad_optional_accessclass bad_optional_access; // [optional.relops], relational operatorstemplate<class T, class U>constexpr bool operator==(const optional&, const optional&); template<class T, class U>constexpr bool operator!=(const optional&, const optional&); template<class T, class U>constexpr bool operator<(const optional&, const optional&); template<class T, class U>constexpr bool operator>(const optional&, const optional&); template<class T, class U>constexpr bool operator<=(const optional&, const optional&); template<class T, class U>constexpr bool operator>=(const optional&, const optional&); template<class T, three_way_comparable_with U>constexpr compare_three_way_result_t<T, U>operator<=>(const optional&, const optional&); // [optional.nullops], comparison with nullopttemplate constexpr bool operator==(const optional&, nullopt_t) noexcept; templateconstexpr strong_ordering operator<=>(const optional&, nullopt_t) noexcept; // [optional.comp.with.t], comparison with Ttemplate<class T, class U> constexpr bool operator==(const optional&, const U&); template<class T, class U> constexpr bool operator==(const T&, const optional&); template<class T, class U> constexpr bool operator!=(const optional&, const U&); template<class T, class U> constexpr bool operator!=(const T&, const optional&); template<class T, class U> constexpr bool operator<(const optional&, const U&); template<class T, class U> constexpr bool operator<(const T&, const optional&); template<class T, class U> constexpr bool operator>(const optional&, const U&); template<class T, class U> constexpr bool operator>(const T&, const optional&); template<class T, class U> constexpr bool operator<=(const optional&, const U&); template<class T, class U> constexpr bool operator<=(const T&, const optional&); template<class T, class U> constexpr bool operator>=(const optional&, const U&); template<class T, class U> constexpr bool operator>=(const T&, const optional&); template<class T, class U>requires (is-derived-from-optional) && three_way_comparable_with<T, U>constexpr compare_three_way_result_t<T, U>operator<=>(const optional&, const U&); // [optional.specalg], specialized algorithmstemplateconstexpr void swap(optional&, optional&) noexcept(see below); templateconstexpr optional<decay_t> make_optional(T&&); template<class T, class... Args>constexpr optional make_optional(Args&&... args); template<class T, class U, class... Args>constexpr optional make_optional(initializer_list il, Args&&... args); // [optional.hash], hash supporttemplate struct hash; template struct hash<optional>;}

22.5.3 Class template optional [optional.optional]

22.5.3.1 General [optional.optional.general]

🔗

namespace std {templateclass optional {public:using value_type = T; using iterator = implementation-defined; // see [optional.iterators]using const_iterator = implementation-defined; // see [optional.iterators]// [optional.ctor], 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, Args&&...); template<class U = remove_cv_t>constexpr explicit(see below) optional(U&&); templateconstexpr explicit(see below) optional(const optional&); templateconstexpr explicit(see below) optional(optional&&); // [optional.dtor], destructorconstexpr ~optional(); // [optional.assign], assignmentconstexpr optional& operator=(nullopt_t) noexcept; constexpr optional& operator=(const optional&); constexpr optional& operator=(optional&&) noexcept(see below); template<class U = remove_cv_t> constexpr optional& operator=(U&&); template constexpr optional& operator=(const optional&); template constexpr optional& operator=(optional&&); template<class... Args> constexpr T& emplace(Args&&...); template<class U, class... Args> constexpr T& emplace(initializer_list, Args&&...); // [optional.swap], swapconstexpr void swap(optional&) noexcept(see below); // [optional.iterators], iterator supportconstexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept; constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept; // [optional.observe], 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> constexpr T value_or(U&&) const &; template<class U = remove_cv_t> constexpr T value_or(U&&) &&; // [optional.monadic], monadic operationstemplate constexpr auto and_then(F&& f) &; template constexpr auto and_then(F&& f) &&; template constexpr auto and_then(F&& f) const &; template constexpr auto and_then(F&& f) const &&; template constexpr auto transform(F&& f) &; template constexpr auto transform(F&& f) &&; template constexpr auto transform(F&& f) const &; template constexpr auto transform(F&& f) const &&; template constexpr optional or_else(F&& f) &&; template constexpr optional or_else(F&& f) const &; // [optional.mod], modifiersconstexpr void reset() noexcept; private: T* val; // exposition only}; template optional(T) -> optional;}

1

#

Any instance of optional at any given time either contains a value or does not contain a value.

When an instance of optional contains a value, it means that an object of type T, referred to as the optional object's contained value, is nested within ([intro.object]) the optional object.

When an object of type optional is contextually converted to bool, the conversion returns true if the object contains a value; otherwise the conversion returns false.

2

#

When an optional object contains a value, member val points to the contained value.

3

#

A type X is avalid contained type for optional if X is an lvalue reference type or a complete non-array object type, and remove_cvref_t is a type other than in_place_t or nullopt_t.

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.

If T is an object type,T shall meet the Cpp17Destructible requirements (Table 35).

22.5.3.2 Constructors [optional.ctor]

1

#

The exposition-only variable template converts-from-any-cvref is used by some constructors for optional.

template<class T, class W>constexpr bool converts-from-any-cvref = // exposition only disjunction_v<is_constructible<T, W&>, is_convertible<W&, T>, is_constructible<T, W>, is_convertible<W, T>, is_constructible<T, const W&>, is_convertible<const W&, T>, is_constructible<T, const W>, is_convertible<const W, T>>;

🔗

constexpr optional() noexcept; constexpr optional(nullopt_t) noexcept;

2

#

Postconditions: *this does not contain a value.

3

#

Remarks: No contained value is initialized.

For every object type T these constructors are constexpr constructors ([dcl.constexpr]).

🔗

constexpr optional(const optional& rhs);

4

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *rhs.

5

#

Postconditions: rhs.has_value() == this->has_value().

6

#

Throws: Any exception thrown by the selected constructor of T.

7

#

Remarks: This constructor is defined as deleted unlessis_copy_constructible_v is true.

If is_trivially_copy_constructible_v is true, this constructor is trivial.

🔗

constexpr optional(optional&& rhs) noexcept(see below);

8

#

Constraints: is_move_constructible_v is true.

9

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *std::move(rhs).

rhs.has_value() is unchanged.

10

#

Postconditions: rhs.has_value() == this->has_value().

11

#

Throws: Any exception thrown by the selected constructor of T.

12

#

Remarks: The exception specification is equivalent tois_nothrow_move_constructible_v.

If is_trivially_move_constructible_v is true, this constructor is trivial.

🔗

template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);

13

#

Constraints: is_constructible_v<T, Args...> is true.

14

#

Effects: Direct-non-list-initializes the contained value with std::forward(args)....

15

#

Postconditions: *this contains a value.

16

#

Throws: Any exception thrown by the selected constructor of T.

17

#

Remarks: If T's constructor selected for the initialization is a constexpr constructor, this constructor is a constexpr constructor.

🔗

template<class U, class... Args> constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args);

18

#

Constraints: is_constructible_v<T, initializer_list&, Args...> is true.

19

#

Effects: Direct-non-list-initializes the contained value with il, std::forward(args)....

20

#

Postconditions: *this contains a value.

21

#

Throws: Any exception thrown by the selected constructor of T.

22

#

Remarks: If T's constructor selected for the initialization is a constexpr constructor, this constructor is a constexpr constructor.

🔗

template<class U = remove_cv_t<T>> constexpr explicit(see below) optional(U&& v);

23

#

Constraints:

is_constructible_v<T, U> is true,

is_same_v<remove_cvref_t, in_place_t> is false,

is_same_v<remove_cvref_t, optional> is false, and

if T is cv bool,remove_cvref_t is not a specialization of optional.

24

#

Effects: Direct-non-list-initializes the contained value with std::forward(v).

25

#

Postconditions: *this contains a value.

26

#

Throws: Any exception thrown by the selected constructor of T.

27

#

Remarks: If T's selected constructor is a constexpr constructor, this constructor is a constexpr constructor.

The expression inside explicit is equivalent to:!is_convertible_v<U, T>

🔗

template<class U> constexpr explicit(see below) optional(const optional<U>& rhs);

28

#

Constraints:

is_constructible_v<T, const U&> is true, and

if T is not cv bool,converts-from-any-cvref<T, optional> is false.

29

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *rhs.

30

#

Postconditions: rhs.has_value() == this->has_value().

31

#

Throws: Any exception thrown by the selected constructor of T.

32

#

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<const U&, T>

🔗

template<class U> constexpr explicit(see below) optional(optional<U>&& rhs);

33

#

Constraints:

is_constructible_v<T, U> is true, and

if T is not cv bool,converts-from-any-cvref<T, optional> is false.

34

#

Effects: If rhs contains a value, direct-non-list-initializes the contained value with *std::move(rhs).

rhs.has_value() is unchanged.

35

#

Postconditions: rhs.has_value() == this->has_value().

36

#

Throws: Any exception thrown by the selected constructor of T.

37

#

Remarks: The expression inside explicit is equivalent to:!is_convertible_v<U, T>

22.5.3.3 Destructor [optional.dtor]

🔗

constexpr ~optional();

1

#

Effects: If is_trivially_destructible_v != true and *this contains a value, callsval->T::~T()

2

#

Remarks: If is_trivially_destructible_v is true, then this destructor is trivial.

22.5.3.4 Assignment [optional.assign]

🔗

constexpr optional<T>& operator=(nullopt_t) noexcept;

1

#

Effects: If *this contains a value, calls val->T::~T() to destroy the contained value; otherwise no effect.

2

#

Postconditions: *this does not contain a value.

3

#

Returns: *this.

🔗

constexpr optional<T>& operator=(const optional& rhs);

4

#

Effects: See Table 67.

Table 67 — optional::operator=(const optional&) effects [tab:optional.assign.copy]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *rhs to the contained value direct-non-list-initializes the contained value with *rhs
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

5

#

Postconditions: rhs.has_value() == this->has_value().

6

#

Returns: *this.

7

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's copy constructor, no effect.

If an exception is thrown during the call to T's copy assignment, the state of its contained value is as defined by the exception safety guarantee of T's copy assignment.

This operator is defined as deleted unlessis_copy_constructible_v is true andis_copy_assignable_v is true.

If is_trivially_copy_constructible_v &&is_trivially_copy_assignable_v &&is_trivially_destructible_v is true, this assignment operator is trivial.

🔗

constexpr optional& operator=(optional&& rhs) noexcept(see below);

8

#

Constraints: is_move_constructible_v is true andis_move_assignable_v is true.

9

#

Effects: See Table 68.

The result of the expression rhs.has_value() remains unchanged.

Table 68 — optional::operator=(optional&&) effects [tab:optional.assign.move]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *std::move(rhs) to the contained value direct-non-list-initializes the contained value with *std::move(rhs)
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

10

#

Postconditions: rhs.has_value() == this->has_value().

11

#

Returns: *this.

12

#

Remarks: The exception specification is equivalent to:is_nothrow_move_assignable_v && is_nothrow_move_constructible_v

13

#

If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's move constructor, the state of *rhs.val is determined by the exception safety guarantee of T's move constructor.

If an exception is thrown during the call to T's move assignment, the state of *val and *rhs.val is determined by the exception safety guarantee of T's move assignment.

If is_trivially_move_constructible_v &&is_trivially_move_assignable_v &&is_trivially_destructible_v is true, this assignment operator is trivial.

🔗

template<class U = remove_cv_t<T>> constexpr optional& operator=(U&& v);

14

#

Constraints:

is_same_v<remove_cvref_t, optional> is false,

conjunction_v<is_scalar, is_same<T, decay_t>> is false,

is_constructible_v<T, U> is true, and

is_assignable_v<T&, U> is true.

15

#

Effects: If *this contains a value, assigns std::forward(v) to the contained value; otherwise direct-non-list-initializes the contained value with std::forward(v).

16

#

Postconditions: *this contains a value.

17

#

Returns: *this.

18

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's constructor, the state of v is determined by the exception safety guarantee of T's constructor.

If an exception is thrown during the call to T's assignment, the state of *val and v is determined by the exception safety guarantee of T's assignment.

🔗

template<class U> constexpr optional<T>& operator=(const optional<U>& rhs);

19

#

Constraints:

is_constructible_v<T, const U&> is true,

is_assignable_v<T&, const U&> is true,

converts-from-any-cvref<T, optional> is false,

is_assignable_v<T&, optional&> is false,

is_assignable_v<T&, optional&&> is false,

is_assignable_v<T&, const optional&> is false, and

is_assignable_v<T&, const optional&&> is false.

20

#

Effects: See Table 69.

Table 69 — optional::operator=(const optional&) effects [tab:optional.assign.copy.templ]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *rhs to the contained value direct-non-list-initializes the contained value with *rhs
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

21

#

Postconditions: rhs.has_value() == this->has_value().

22

#

Returns: *this.

23

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's constructor, the state of *rhs.val is determined by the exception safety guarantee of T's constructor.

If an exception is thrown during the call to T's assignment, the state of *val and *rhs.val is determined by the exception safety guarantee of T's assignment.

🔗

template<class U> constexpr optional<T>& operator=(optional<U>&& rhs);

24

#

Constraints:

is_constructible_v<T, U> is true,

is_assignable_v<T&, U> is true,

converts-from-any-cvref<T, optional> is false,

is_assignable_v<T&, optional&> is false,

is_assignable_v<T&, optional&&> is false,

is_assignable_v<T&, const optional&> is false, and

is_assignable_v<T&, const optional&&> is false.

25

#

Effects: See Table 70.

The result of the expression rhs.has_value() remains unchanged.

Table 70 — optional::operator=(optional&&) effects [tab:optional.assign.move.templ]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
assigns *std::move(rhs) to the contained value direct-non-list-initializes the contained value with *std::move(rhs)
🔗
rhs does not contain a value
destroys the contained value by calling val->T::~T() no effect

26

#

Postconditions: rhs.has_value() == this->has_value().

27

#

Returns: *this.

28

#

Remarks: If any exception is thrown, the result of the expression this->has_value() remains unchanged.

If an exception is thrown during the call to T's constructor, the state of *rhs.val is determined by the exception safety guarantee of T's constructor.

If an exception is thrown during the call to T's assignment, the state of *val and *rhs.val is determined by the exception safety guarantee of T's assignment.

🔗

template<class... Args> constexpr T& emplace(Args&&... args);

29

#

Mandates: is_constructible_v<T, Args...> is true.

30

#

Effects: Calls *this = nullopt.

Then direct-non-list-initializes the contained value with std::forward(args)....

31

#

Postconditions: *this contains a value.

32

#

Returns: A reference to the new contained value.

33

#

Throws: Any exception thrown by the selected constructor of T.

34

#

Remarks: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous *val (if any) has been destroyed.

🔗

template<class U, class... Args> constexpr T& emplace(initializer_list<U> il, Args&&... args);

35

#

Constraints: is_constructible_v<T, initializer_list&, Args...> is true.

36

#

Effects: Calls *this = nullopt.

Then direct-non-list-initializes the contained value withil, std::forward(args)....

37

#

Postconditions: *this contains a value.

38

#

Returns: A reference to the new contained value.

39

#

Throws: Any exception thrown by the selected constructor of T.

40

#

Remarks: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous *val (if any) has been destroyed.

22.5.3.5 Swap [optional.swap]

🔗

constexpr void swap(optional& rhs) noexcept(see below);

1

#

Mandates: is_move_constructible_v is true.

2

#

Preconditions: T meets the Cpp17Swappable requirements ([swappable.requirements]).

3

#

Effects: See Table 71.

Table 71 — optional::swap(optional&) effects [tab:optional.swap]

🔗 *this contains a value *this does not contain a value
🔗
rhs contains a value
calls swap(*(*this), *rhs) direct-non-list-initializes the contained value of *this with std::move(*rhs), followed by rhs.val->T::~T(); postcondition is that *this contains a value and rhs does not contain a value
🔗
rhs does not contain a value
direct-non-list-initializes the contained value of rhs with std::move(*(*this)), followed by val->T::~T(); postcondition is that *this does not contain a value and rhs contains a value no effect

4

#

Throws: Any exceptions thrown by the operations in the relevant part of Table 71.

5

#

Remarks: The exception specification is equivalent to:is_nothrow_move_constructible_v && is_nothrow_swappable_v

6

#

If any exception is thrown, the results of the expressions this->has_value() and rhs.has_value() remain unchanged.

If an exception is thrown during the call to function swap, the state of *val and *rhs.val is determined by the exception safety guarantee of swap for lvalues of T.

If an exception is thrown during the call to T's move constructor, the state of *val and *rhs.val is determined by the exception safety guarantee of T's move constructor.

22.5.3.6 Iterator support [optional.iterators]

🔗

using iterator = implementation-defined; using const_iterator = implementation-defined;

1

#

These types model contiguous_iterator ([iterator.concept.contiguous]), meet the Cpp17RandomAccessIterator requirements ([random.access.iterators]), and meet the requirements for constexpr iterators ([iterator.requirements.general]), with value type remove_cv_t.

The reference type is T& for iterator andconst T& for const_iterator.

2

#

All requirements on container iterators ([container.reqmts]) apply tooptional::iterator and optional::const_iterator as well.

3

#

Any operation that initializes or destroys the contained value of an optional object invalidates all iterators into that object.

🔗

constexpr iterator begin() noexcept; constexpr const_iterator begin() const noexcept;

4

#

Returns: If has_value() is true, an iterator referring to the contained value.

Otherwise, a past-the-end iterator value.

🔗

constexpr iterator end() noexcept; constexpr const_iterator end() const noexcept;

5

#

Returns: begin() + has_value().

22.5.3.7 Observers [optional.observe]

🔗

constexpr const T* operator->() const noexcept; constexpr T* operator->() noexcept;

1

#

Hardened preconditions: has_value() is true.

2

#

Returns: val.

3

#

Remarks: These functions are constexpr functions.

🔗

constexpr const T& operator*() const & noexcept; constexpr T& operator*() & noexcept;

4

#

Hardened preconditions: has_value() is true.

5

#

Returns: *val.

6

#

Remarks: These functions are constexpr functions.

🔗

constexpr T&& operator*() && noexcept; constexpr const T&& operator*() const && noexcept;

7

#

Hardened preconditions: has_value() is true.

8

#

Effects: Equivalent to: return std::move(*val);

🔗

constexpr explicit operator bool() const noexcept;

9

#

Returns: true if and only if *this contains a value.

10

#

Remarks: This function is a constexpr function.

🔗

constexpr bool has_value() const noexcept;

11

#

Returns: true if and only if *this contains a value.

12

#

Remarks: This function is a constexpr function.

🔗

constexpr const T& value() const &; constexpr T& value() &;

13

#

Effects: Equivalent to:return has_value() ? *val : throw bad_optional_access();

🔗

constexpr T&& value() &&; constexpr const T&& value() const &&;

14

#

Effects: Equivalent to:return has_value() ? std::move(*val) : throw bad_optional_access();

🔗

template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) const &;

15

#

Mandates: is_copy_constructible_v && is_convertible_v<U&&, T> is true.

16

#

Effects: Equivalent to:return has_value() ? **this : static_cast(std::forward(v));

🔗

template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) &&;

17

#

Mandates: is_move_constructible_v && is_convertible_v<U&&, T> is true.

18

#

Effects: Equivalent to:return has_value() ? std::move(**this) : static_cast(std::forward(v));

22.5.3.8 Monadic operations [optional.monadic]

🔗

template<class F> constexpr auto and_then(F&& f) &; template<class F> constexpr auto and_then(F&& f) const &;

1

#

Let U be invoke_result_t<F, decltype(*val)>.

2

#

Mandates: remove_cvref_t is a specialization of optional.

3

#

Effects: Equivalent to:if (*this) {return invoke(std::forward(f), *val);} else {return remove_cvref_t();}

🔗

template<class F> constexpr auto and_then(F&& f) &&; template<class F> constexpr auto and_then(F&& f) const &&;

4

#

Let U be invoke_result_t<F, decltype(std::move(*val))>.

5

#

Mandates: remove_cvref_t is a specialization of optional.

6

#

Effects: Equivalent to:if (*this) {return invoke(std::forward(f), std::move(*val));} else {return remove_cvref_t();}

🔗

template<class F> constexpr auto transform(F&& f) &; template<class F> constexpr auto transform(F&& f) const &;

7

#

Let U be remove_cv_t<invoke_result_t<F, decltype(*val)>>.

8

#

Mandates: U is a valid contained type for optional.

The declarationU u(invoke(std::forward(f), *val)); is well-formed for some invented variable u.

[Note 1:

There is no requirement that U is movable ([dcl.init.general]).

— end note]

9

#

Returns: If *this contains a value, an optional object whose contained value is direct-non-list-initialized withinvoke(std::forward(f), *val); otherwise, optional().

🔗

template<class F> constexpr auto transform(F&& f) &&; template<class F> constexpr auto transform(F&& f) const &&;

10

#

Let U beremove_cv_t<invoke_result_t<F, decltype(std::move(*val))>>.

11

#

Mandates: U is a valid contained type for optional.

The declarationU u(invoke(std::forward(f), std::move(*val))); is well-formed for some invented variable u.

[Note 2:

There is no requirement that U is movable ([dcl.init.general]).

— end note]

12

#

Returns: If *this contains a value, an optional object whose contained value is direct-non-list-initialized withinvoke(std::forward(f), std::move(*val)); otherwise, optional().

🔗

template<class F> constexpr optional or_else(F&& f) const &;

13

#

Constraints: F models invocable andT models copy_constructible.

14

#

Mandates: is_same_v<remove_cvref_t<invoke_result_t>, optional> is true.

15

#

Effects: Equivalent to:if (*this) {return *this;} else {return std::forward(f)();}

🔗

template<class F> constexpr optional or_else(F&& f) &&;

16

#

Constraints: F models invocable andT models move_constructible.

17

#

Mandates: is_same_v<remove_cvref_t<invoke_result_t>, optional> is true.

18

#

Effects: Equivalent to:if (*this) {return std::move(*this);} else {return std::forward(f)();}

22.5.3.9 Modifiers [optional.mod]

🔗

constexpr void reset() noexcept;

1

#

Effects: If *this contains a value, calls val->T::~T() to destroy the contained value; otherwise no effect.

2

#

Postconditions: *this does not contain a value.

22.5.4 Partial specialization of optional for reference types [optional.optional.ref]

22.5.4.1 General [optional.optional.ref.general]

namespace std {templateclass optional<T&> {public:using value_type = T; using iterator = implementation-defined; // see [optional.ref.iterators]public:// [optional.ref.ctor], constructorsconstexpr optional() noexcept = default; constexpr optional(nullopt_t) noexcept : optional() {}constexpr optional(const optional& rhs) noexcept = default; templateconstexpr explicit optional(in_place_t, Arg&& arg); templateconstexpr explicit(see below) optional(U&& u) noexcept(see below); templateconstexpr explicit(see below) optional(optional& rhs) noexcept(see below); templateconstexpr explicit(see below) optional(const optional& rhs) noexcept(see below); templateconstexpr explicit(see below) optional(optional&& rhs) noexcept(see below); templateconstexpr explicit(see below) optional(const optional&& rhs) noexcept(see below); constexpr ~optional() = default; // [optional.ref.assign], assignmentconstexpr optional& operator=(nullopt_t) noexcept; constexpr optional& operator=(const optional& rhs) noexcept = default; template constexpr T& emplace(U&& u) noexcept(see below); // [optional.ref.swap], swapconstexpr void swap(optional& rhs) noexcept; // [optional.ref.iterators], iterator supportconstexpr iterator begin() const noexcept; constexpr iterator end() const noexcept; // [optional.ref.observe], observersconstexpr T* operator->() const noexcept; constexpr T& operator*() const noexcept; constexpr explicit operator bool() const noexcept; constexpr bool has_value() const noexcept; constexpr T& value() const; // freestanding-deletedtemplate<class U = remove_cv_t>constexpr remove_cv_t value_or(U&& u) const; // [optional.ref.monadic], monadic operationstemplate constexpr auto and_then(F&& f) const; template constexpr optional<invoke_result_t<F, T&>> transform(F&& f) const; template constexpr optional or_else(F&& f) const; // [optional.ref.mod], modifiersconstexpr void reset() noexcept; private: T* val = nullptr; // exposition only// [optional.ref.expos], exposition only helper functionstemplateconstexpr void convert-ref-init-val(U&& u); // exposition only};}

1

#

An object of optional<T&>contains a value if and only if val != nullptr is true.

When an optional<T&> contains a value, the contained value is a reference to *val.

22.5.4.2 Constructors [optional.ref.ctor]

🔗

template<class Arg> constexpr explicit optional(in_place_t, Arg&& arg);

1

#

Constraints:

is_constructible_v<T&, Arg> is true, and

reference_constructs_from_temporary_v<T&, Arg> is false.

2

#

Effects: Equivalent to: convert-ref-init-val(std::forward(arg)).

3

#

Postconditions: *this contains a value.

🔗

template<class U> constexpr explicit(!is_convertible_v<U, T&>) optional(U&& u) noexcept(is_nothrow_constructible_v<T&, U>);

4

#

Constraints:

is_same_v<remove_cvref_t, optional> is false,

is_same_v<remove_cvref_t, in_place_t> is false, and

is_constructible_v<T&, U> is true.

5

#

Effects: Equivalent to: convert-ref-init-val(std::forward(u)).

6

#

Postconditions: *this contains a value.

7

#

Remarks: This constructor is defined as deleted ifreference_constructs_from_temporary_v<T&, U> is true.

🔗

template<class U> constexpr explicit(!is_convertible_v<U&, T&>) optional(optional<U>& rhs) noexcept(is_nothrow_constructible_v<T&, U&>);

8

#

Constraints:

is_same_v<remove_cv_t, optional> is false,

is_same_v<T&, U> is false, and

is_constructible_v<T&, U&> is true.

9

#

Effects: Equivalent to:if (rhs.has_value()) convert-ref-init-val(*rhs);

10

#

Remarks: This constructor is defined as deleted ifreference_constructs_from_temporary_v<T&, U&> is true.

🔗

template<class U> constexpr explicit(!is_convertible_v<const U&, T&>) optional(const optional<U>& rhs) noexcept(is_nothrow_constructible_v<T&, const U&>);

11

#

Constraints:

is_same_v<remove_cv_t, optional> is false,

is_same_v<T&, U> is false, and

is_constructible_v<T&, const U&> is true.

12

#

Effects: Equivalent to:if (rhs.has_value()) convert-ref-init-val(*rhs);

13

#

Remarks: This constructor is defined as deleted ifreference_constructs_from_temporary_v<T&, const U&> is true.

🔗

template<class U> constexpr explicit(!is_convertible_v<U, T&>) optional(optional<U>&& rhs) noexcept(is_nothrow_constructible_v<T&, U>);

14

#

Constraints:

is_same_v<remove_cv_t, optional> is false,

is_same_v<T&, U> is false, and

is_constructible_v<T&, U> is true.

15

#

Effects: Equivalent to:if (rhs.has_value()) convert-ref-init-val(*std::move(rhs));

16

#

Remarks: This constructor is defined as deleted ifreference_constructs_from_temporary_v<T&, U> is true.

🔗

template<class U> constexpr explicit(!is_convertible_v<const U, T&>) optional(const optional<U>&& rhs) noexcept(is_nothrow_constructible_v<T&, const U>);

17

#

Constraints:

is_same_v<remove_cv_t, optional> is false,

is_same_v<T&, U> is false, and

is_constructible_v<T&, const U> is true.

18

#

Effects: Equivalent to:if (rhs.has_value()) convert-ref-init-val(*std::move(rhs));

19

#

Remarks: This constructor is defined as deleted ifreference_constructs_from_temporary_v<T&, const U> is true.

22.5.4.3 Assignment [optional.ref.assign]

🔗

constexpr optional& operator=(nullopt_t) noexcept;

1

#

Effects: Assigns nullptr to val.

2

#

Postconditions: *this does not contain a value.

3

#

Returns: *this.

🔗

template<class U> constexpr T& emplace(U&& u) noexcept(is_nothrow_constructible_v<T&, U>);

4

#

Constraints:

is_constructible_v<T&, U> is true, and

reference_constructs_from_temporary_v<T&, U> is false.

5

#

Effects: Equivalent to: convert-ref-init-val(std::forward(u)).

22.5.4.4 Swap [optional.ref.swap]

🔗

constexpr void swap(optional& rhs) noexcept;

1

#

Effects: Equivalent to: swap(val, rhs.val).

22.5.4.5 Iterator support [optional.ref.iterators]

🔗

using iterator = implementation-defined;

1

#

This type models contiguous_iterator ([iterator.concept.contiguous]), meets the Cpp17RandomAccessIterator requirements ([random.access.iterators]), and meets the requirements for constexpr iterators ([iterator.requirements.general]), with value type remove_cv_t.

The reference type is T& for iterator.

2

#

All requirements on container iterators ([container.reqmts]) apply tooptional::iterator.

🔗

constexpr iterator begin() const noexcept;

3

#

Returns: If has_value() is true, an iterator referring to *val.

Otherwise, a past-the-end iterator value.

🔗

constexpr iterator end() const noexcept;

4

#

Returns: begin() + has_value().

22.5.4.6 Observers [optional.ref.observe]

🔗

constexpr T* operator->() const noexcept;

1

#

Hardened preconditions: has_value() is true.

2

#

Returns: val.

🔗

constexpr T& operator*() const noexcept;

3

#

Hardened preconditions: has_value() is true.

4

#

Returns: *val.

🔗

constexpr explicit operator bool() const noexcept;

5

#

Returns: val != nullptr.

🔗

constexpr bool has_value() const noexcept;

6

#

Returns: val != nullptr.

🔗

constexpr T& value() const;

7

#

Effects: Equivalent to:return has_value() ? *val : throw bad_optional_access();

🔗

template<class U = remove_cv_t<T>> constexpr remove_cv_t<T> value_or(U&& u) const;

8

#

Let X be remove_cv_t.

9

#

Mandates: is_constructible_v<X, T&> && is_convertible_v<U, X> is true.

10

#

Effects: Equivalent to:return has_value() ? *val : static_cast(std::forward(u));

22.5.4.7 Monadic operations [optional.ref.monadic]

🔗

template<class F> constexpr auto and_then(F&& f) const;

1

#

Let U be invoke_result_t<F, T&>.

2

#

Mandates: remove_cvref_t is a specialization of optional.

3

#

Effects: Equivalent to:if (has_value()) {return invoke(std::forward(f), *val);} else {return remove_cvref_t();}

🔗

template<class F> constexpr optional<remove_cv_t<invoke_result_t<F, T&>>> transform(F&& f) const;

4

#

Let U be remove_cv_t<invoke_result_t<F, T&>>.

5

#

Mandates: The declarationU u(invoke(std::forward(f), *val)); is well-formed for some invented variable u.

[Note 1:

There is no requirement that U is movable ([dcl.init.general]).

— end note]

6

#

Returns: If *this contains a value, an optional object whose contained value is direct-non-list-initialized withinvoke(std::forward(f), *val); otherwise, optional().

🔗

template<class F> constexpr optional or_else(F&& f) const;

7

#

Constraints: F models invocable.

8

#

Mandates: is_same_v<remove_cvref_t<invoke_result_t>, optional> is true.

9

#

Effects: Equivalent to:if (has_value()) {return *val;} else {return std::forward(f)();}

22.5.4.8 Modifiers [optional.ref.mod]

🔗

constexpr void reset() noexcept;

1

#

Effects: Assigns nullptr to val.

2

#

Postconditions: *this does not contain a value.

22.5.4.9 Exposition only helper functions [optional.ref.expos]

🔗

template<class U> constexpr void convert-ref-init-val(U&& u); // exposition only

1

#

Effects: Creates a variable r as if by T& r(std::forward(u)); and then initializes val with addressof(r).

22.5.5 No-value state indicator [optional.nullopt]

🔗

struct nullopt_t{see below}; inline constexpr nullopt_t nullopt(unspecified);

1

#

The struct nullopt_t is an empty class type used as a unique type to indicate the state of not containing a value for optional objects.

In particular, optional has a constructor with nullopt_t as a single argument; this indicates that an optional object not containing a value shall be constructed.

2

#

Type nullopt_t shall not have a default constructor or an initializer-list constructor, and shall not be an aggregate.

22.5.6 Class bad_optional_access [optional.bad.access]

namespace std {class bad_optional_access : public exception {public:// see [exception] for the specification of the special member functionsconstexpr const char* what() const noexcept override; };}

1

#

The class bad_optional_access defines the type of objects thrown as exceptions to report the situation where an attempt is made to access the value of an optional object that does not contain a value.

🔗

constexpr const char* what() const noexcept override;

2

#

Returns: An implementation-defined ntbs, which during constant evaluation is encoded with the ordinary literal encoding ([lex.ccon]).

22.5.7 Relational operators [optional.relops]

🔗

template<class T, class U> constexpr bool operator==(const optional<T>& x, const optional<U>& y);

1

#

Constraints: The expression *x == *y is well-formed and its result is convertible to bool.

[Note 1:

T need not be Cpp17EqualityComparable.

— end note]

2

#

Returns: If x.has_value() != y.has_value(), false; otherwise if x.has_value() == false, true; otherwise *x == *y.

3

#

Remarks: Specializations of this function template for which *x == *y is a core constant expression are constexpr functions.

🔗

template<class T, class U> constexpr bool operator!=(const optional<T>& x, const optional<U>& y);

4

#

Constraints: The expression *x != *y is well-formed and its result is convertible to bool.

5

#

Returns: If x.has_value() != y.has_value(), true; otherwise, if x.has_value() == false, false; otherwise *x != *y.

6

#

Remarks: Specializations of this function template for which *x != *y is a core constant expression are constexpr functions.

🔗

template<class T, class U> constexpr bool operator<(const optional<T>& x, const optional<U>& y);

7

#

Constraints: *x < *y is well-formed and its result is convertible to bool.

8

#

Returns: If !y, false; otherwise, if !x, true; otherwise *x < *y.

9

#

Remarks: Specializations of this function template for which *x < *y is a core constant expression are constexpr functions.

🔗

template<class T, class U> constexpr bool operator>(const optional<T>& x, const optional<U>& y);

10

#

Constraints: The expression *x > *y is well-formed and its result is convertible to bool.

11

#

Returns: If !x, false; otherwise, if !y, true; otherwise *x > *y.

12

#

Remarks: Specializations of this function template for which *x > *y is a core constant expression are constexpr functions.

🔗

template<class T, class U> constexpr bool operator<=(const optional<T>& x, const optional<U>& y);

13

#

Constraints: The expression *x <= *y is well-formed and its result is convertible to bool.

14

#

Returns: If !x, true; otherwise, if !y, false; otherwise *x <= *y.

15

#

Remarks: Specializations of this function template for which *x <= *y is a core constant expression are constexpr functions.

🔗

template<class T, class U> constexpr bool operator>=(const optional<T>& x, const optional<U>& y);

16

#

Constraints: The expression *x >= *y is well-formed and its result is convertible to bool.

17

#

Returns: If !y, true; otherwise, if !x, false; otherwise *x >= *y.

18

#

Remarks: Specializations of this function template for which *x >= *y is a core constant expression are constexpr functions.

🔗

template<class T, [three_way_comparable_with](cmp.concept#concept:three_way_comparable_with "17.12.4Concept three_­way_­comparable[cmp.concept]")<T> U> constexpr compare_three_way_result_t<T, U> operator<=>(const optional<T>& x, const optional<U>& y);

19

#

Returns: If x && y, *x <=> *y; otherwise x.has_value() <=> y.has_value().

20

#

Remarks: Specializations of this function template for which *x <=> *y is a core constant expression are constexpr functions.

22.5.8 Comparison with nullopt [optional.nullops]

🔗

template<class T> constexpr bool operator==(const optional<T>& x, nullopt_t) noexcept;

1

#

Returns: !x.

🔗

template<class T> constexpr strong_ordering operator<=>(const optional<T>& x, nullopt_t) noexcept;

2

#

Returns: x.has_value() <=> false.

22.5.9 Comparison with T [optional.comp.with.t]

🔗

template<class T, class U> constexpr bool operator==(const optional<T>& x, const U& v);

1

#

Constraints: U is not a specialization of optional.

The expression *x == v is well-formed and its result is convertible to bool.

[Note 1:

T need not be Cpp17EqualityComparable.

— end note]

2

#

Effects: Equivalent to: return x.has_value() ? *x == v : false;

🔗

template<class T, class U> constexpr bool operator==(const T& v, const optional<U>& x);

3

#

Constraints: T is not a specialization of optional.

The expression v == *x is well-formed and its result is convertible to bool.

4

#

Effects: Equivalent to: return x.has_value() ? v == *x : false;

🔗

template<class T, class U> constexpr bool operator!=(const optional<T>& x, const U& v);

5

#

Constraints: U is not a specialization of optional.

The expression *x != v is well-formed and its result is convertible to bool.

6

#

Effects: Equivalent to: return x.has_value() ? *x != v : true;

🔗

template<class T, class U> constexpr bool operator!=(const T& v, const optional<U>& x);

7

#

Constraints: T is not a specialization of optional.

The expression v != *x is well-formed and its result is convertible to bool.

8

#

Effects: Equivalent to: return x.has_value() ? v != *x : true;

🔗

template<class T, class U> constexpr bool operator<(const optional<T>& x, const U& v);

9

#

Constraints: U is not a specialization of optional.

The expression *x < v is well-formed and its result is convertible to bool.

10

#

Effects: Equivalent to: return x.has_value() ? *x < v : true;

🔗

template<class T, class U> constexpr bool operator<(const T& v, const optional<U>& x);

11

#

Constraints: T is not a specialization of optional.

The expression v < *x is well-formed and its result is convertible to bool.

12

#

Effects: Equivalent to: return x.has_value() ? v < *x : false;

🔗

template<class T, class U> constexpr bool operator>(const optional<T>& x, const U& v);

13

#

Constraints: U is not a specialization of optional.

The expression *x > v is well-formed and its result is convertible to bool.

14

#

Effects: Equivalent to: return x.has_value() ? *x > v : false;

🔗

template<class T, class U> constexpr bool operator>(const T& v, const optional<U>& x);

15

#

Constraints: T is not a specialization of optional.

The expression v > *x is well-formed and its result is convertible to bool.

16

#

Effects: Equivalent to: return x.has_value() ? v > *x : true;

🔗

template<class T, class U> constexpr bool operator<=(const optional<T>& x, const U& v);

17

#

Constraints: U is not a specialization of optional.

The expression *x <= v is well-formed and its result is convertible to bool.

18

#

Effects: Equivalent to: return x.has_value() ? *x <= v : true;

🔗

template<class T, class U> constexpr bool operator<=(const T& v, const optional<U>& x);

19

#

Constraints: T is not a specialization of optional.

The expression v <= *x is well-formed and its result is convertible to bool.

20

#

Effects: Equivalent to: return x.has_value() ? v <= *x : false;

🔗

template<class T, class U> constexpr bool operator>=(const optional<T>& x, const U& v);

21

#

Constraints: U is not a specialization of optional.

The expression *x >= v is well-formed and its result is convertible to bool.

22

#

Effects: Equivalent to: return x.has_value() ? *x >= v : false;

🔗

template<class T, class U> constexpr bool operator>=(const T& v, const optional<U>& x);

23

#

Constraints: T is not a specialization of optional.

The expression v >= *x is well-formed and its result is convertible to bool.

24

#

Effects: Equivalent to: return x.has_value() ? v >= *x : true;

🔗

template<class T, class U> requires (![is-derived-from-optional](#concept:is-derived-from-optional "22.5.2Header <optional> synopsis[optional.syn]")<U>) && [three_way_comparable_with](cmp.concept#concept:three_way_comparable_with "17.12.4Concept three_­way_­comparable[cmp.concept]")<T, U> constexpr compare_three_way_result_t<T, U> operator<=>(const optional<T>& x, const U& v);

25

#

Effects: Equivalent to: return x.has_value() ? *x <=> v : strong_ordering::less;

22.5.10 Specialized algorithms [optional.specalg]

🔗

template<class T> constexpr void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y)));

1

#

Constraints: is_reference_v || (is_move_constructible_v && is_swappable_v) is true.

2

#

Effects: Calls x.swap(y).

🔗

template<class T> constexpr optional<decay_t<T>> make_optional(T&& v);

3

#

Constraints: The call to make_optional does not use an explicit template-argument-list that begins with a type template-argument.

4

#

Returns: optional<decay_t>(std::forward(v)).

🔗

template<class T, class...Args> constexpr optional<T> make_optional(Args&&... args);

5

#

Effects: Equivalent to: return optional(in_place, std::forward(args)...);

🔗

template<class T, class U, class... Args> constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);

6

#

Effects: Equivalent to: return optional(in_place, il, std::forward(args)...);

22.5.11 Hash support [optional.hash]

🔗

template<class T> struct hash<optional<T>>;

1

#

The specialization hash<optional> is enabled ([unord.hash]) if and only if hash<remove_const_t> is enabled.

When enabled, for an object o of type optional, if o.has_value() == true, then hash<optional>()(o) evaluates to the same value as hash<remove_const_t>()(*o); otherwise it evaluates to an unspecified value.

The member functions are not guaranteed to be noexcept.