Files
2025-10-25 03:02:53 +03:00

172 lines
5.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[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<class F> constexpr auto and_then(F&& f) &;
template<class F> 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<F, decltype(**val*)>[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4269)
*Mandates*: remove_cvref_t<U> 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>(f), **val*);} else {return remove_cvref_t<U>();}
[🔗](#lib:and_then,optional_)
`template<class F> constexpr auto and_then(F&& f) &&;
template<class F> 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<F, decltype(std::move(**val*))>[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4295)
*Mandates*: remove_cvref_t<U> 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>(f), std::move(**val*));} else {return remove_cvref_t<U>();}
[🔗](#lib:transform,optional)
`template<class F> constexpr auto transform(F&& f) &;
template<class F> 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<invoke_result_t<F, decltype(**val*)>>[.](#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>(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.1General"))[.](#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<U> object
whose contained value is direct-non-list-initialized withinvoke(std::forward<F>(f), **val*);
otherwise, optional<U>()[.](#9.sentence-1)
[🔗](#lib:transform,optional_)
`template<class F> constexpr auto transform(F&& f) &&;
template<class F> constexpr auto transform(F&& f) const &&;
`
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4348)
Let U beremove_cv_t<invoke_result_t<F, decltype(std::move(**val*))>>[.](#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>(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.1General"))[.](#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<U> object
whose contained value is direct-non-list-initialized withinvoke(std::forward<F>(f), std::move(**val*));
otherwise, optional<U>()[.](#12.sentence-1)
[🔗](#lib:or_else,optional)
`template<class F> 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.2Concept invocable[concept.invocable]") andT models [copy_constructible](concept.copyconstructible#concept:copy_constructible "18.4.14Concept copy_­constructible[concept.copyconstructible]")[.](#13.sentence-1)
[14](#14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4383)
*Mandates*: is_same_v<remove_cvref_t<invoke_result_t<F>>, 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>(f)();}
[🔗](#lib:or_else,optional_)
`template<class F> 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.2Concept invocable[concept.invocable]") andT models [move_constructible](concept.moveconstructible#concept:move_constructible "18.4.13Concept move_­constructible[concept.moveconstructible]")[.](#16.sentence-1)
[17](#17)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L4410)
*Mandates*: is_same_v<remove_cvref_t<invoke_result_t<F>>, 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>(f)();}