[optional.monadic] # 22 General utilities library [[utilities]](./#utilities) ## 22.5 Optional objects [[optional]](optional#monadic) ### 22.5.3 Class template optional [[optional.optional]](optional.optional#optional.monadic) #### 22.5.3.8 Monadic operations [optional.monadic] [🔗](#lib:and_then,optional) `template constexpr auto and_then(F&& f) &; template constexpr auto and_then(F&& f) const &; ` [1](#1) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4266) Let U be invoke_result_t[.](#1.sentence-1) [2](#2) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4269) *Mandates*: remove_cvref_t is a specialization of optional[.](#2.sentence-1) [3](#3) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4273) *Effects*: Equivalent to:if (*this) {return invoke(std::forward(f), **val*);} else {return remove_cvref_t();} [🔗](#lib:and_then,optional_) `template constexpr auto and_then(F&& f) &&; template constexpr auto and_then(F&& f) const &&; ` [4](#4) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4292) Let U be invoke_result_t[.](#4.sentence-1) [5](#5) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4295) *Mandates*: remove_cvref_t is a specialization of optional[.](#5.sentence-1) [6](#6) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4299) *Effects*: Equivalent to:if (*this) {return invoke(std::forward(f), std::move(**val*));} else {return remove_cvref_t();} [🔗](#lib:transform,optional) `template constexpr auto transform(F&& f) &; template constexpr auto transform(F&& f) const &; ` [7](#7) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4318) Let U be remove_cv_t>[.](#7.sentence-1) [8](#8) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4321) *Mandates*: U is a valid contained type for optional[.](#8.sentence-1) The declarationU u(invoke(std::forward(f), **val*)); is well-formed for some invented variable u[.](#8.sentence-2) [*Note [1](#note-1)*: There is no requirement that U is movable ([[dcl.init.general]](dcl.init.general "9.5.1 General"))[.](#8.sentence-3) — *end note*] [9](#9) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4333) *Returns*: If *this contains a value, an optional object whose contained value is direct-non-list-initialized withinvoke(std​::​forward(f), **val*); otherwise, optional()[.](#9.sentence-1) [🔗](#lib:transform,optional_) `template constexpr auto transform(F&& f) &&; template constexpr auto transform(F&& f) const &&; ` [10](#10) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4348) Let U beremove_cv_t>[.](#10.sentence-1) [11](#11) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4352) *Mandates*: U is a valid contained type for optional[.](#11.sentence-1) The declarationU u(invoke(std::forward(f), std::move(**val*))); is well-formed for some invented variable u[.](#11.sentence-2) [*Note [2](#note-2)*: There is no requirement that U is movable ([[dcl.init.general]](dcl.init.general "9.5.1 General"))[.](#11.sentence-3) — *end note*] [12](#12) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4364) *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()[.](#12.sentence-1) [🔗](#lib:or_else,optional) `template constexpr optional or_else(F&& f) const &; ` [13](#13) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4378) *Constraints*: F models [invocable](concept.invocable#concept:invocable "18.7.2 Concept invocable [concept.invocable]") andT models [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14 Concept copy_­constructible [concept.copyconstructible]")[.](#13.sentence-1) [14](#14) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4383) *Mandates*: is_same_v>, optional> is true[.](#14.sentence-1) [15](#15) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4387) *Effects*: Equivalent to:if (*this) {return *this;} else {return std::forward(f)();} [🔗](#lib:or_else,optional_) `template constexpr optional or_else(F&& f) &&; ` [16](#16) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4405) *Constraints*: F models [invocable](concept.invocable#concept:invocable "18.7.2 Concept invocable [concept.invocable]") andT models [move_constructible](concept.moveconstructible#concept:move_constructible "18.4.13 Concept move_­constructible [concept.moveconstructible]")[.](#16.sentence-1) [17](#17) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4410) *Mandates*: is_same_v>, optional> is true[.](#17.sentence-1) [18](#18) [#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4414) *Effects*: Equivalent to:if (*this) {return std::move(*this);} else {return std::forward(f)();}