[optional.ref.monadic]

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

## 22.5 Optional objects [[optional]](optional#ref.monadic)

### 22.5.4 Partial specialization of optional for reference types [[optional.optional.ref]](optional.optional.ref#optional.ref.monadic)

#### 22.5.4.7 Monadic operations [optional.ref.monadic]

[ð](#itemdecl:1)

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

[1](#1)

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

Let U be invoke_result_t<F, T&>[.](#1.sentence-1)

[2](#2)

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

*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#L4894)

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

[ð](#itemdecl:2)

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

[4](#4)

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

Let U be remove_cv_t<invoke_result_t<F, T&>>[.](#4.sentence-1)

[5](#5)

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

*Mandates*: The declarationU u(invoke(std::forward<F>(f), **val*)); is well-formed for some invented variable u[.](#5.sentence-1)

[*Note [1](#note-1)*:

There is no requirement that U is movable ([[dcl.init.general]](dcl.init.general "9.5.1 General"))[.](#5.sentence-2)

â *end note*]

[6](#6)

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

*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>()[.](#6.sentence-1)

[ð](#itemdecl:3)

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

[7](#7)

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

*Constraints*: F models [invocable](concept.invocable#concept:invocable "18.7.2 Concept invocable [concept.invocable]")[.](#7.sentence-1)

[8](#8)

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

*Mandates*: is_same_v<remove_cvref_t<invoke_result_t<F>>, optional> is true[.](#8.sentence-1)

[9](#9)

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

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