Namespaces
Variants
Views
Actions

std::optional<T>::or_else

From cppreference.com
< cpp‎ | utility‎ | optional
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)

(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

Elementary string conversions
(C++17)
(C++17)

Stacktrace
 
std::optional
Member functions
Observers
Monadic operations
optional::or_else
(C++23)
Modifiers
Non-member functions
(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
(C++17)
Deduction guides
Helper classes
(C++17)
(C++17)
(C++17)
Helper objects
(C++17)
(C++17)
 
template< class F >
constexpr optional or_else( F&& f ) const&;
(1) (since C++23)
template< class F >
constexpr optional or_else( F&& f ) &&;
(2) (since C++23)

Returns *this if it contains a value. Otherwise, returns the result of f.

The program is ill-formed if std::remove_cvref_t<std::invoke_result_t<F>> is not same as std::optional<T>.

1) Equivalent to return *this ? *this : std::forward<F>(f)();. This overload participates in overload resolution only if both std::copy_constructible<T> and std::invocable<F> are modeled.
2) Equivalent to return *this ? std::move(*this) : std::forward<F>(f)();. This overload participates in overload resolution only if both std::move_constructible<T> and std::invocable<F> are modeled.

Contents

[edit] Parameters

f - a function or Callable object that returns an std::optional<T>

[edit] Return value

*this or the result of f, as described above.

[edit] Example

[edit] See also

returns the contained value if available, another value otherwise
(public member function) [edit]
(C++23)
returns the result of the given function on the contained value if it exists, or an empty optional otherwise
(public member function) [edit]
(C++23)
returns an optional containing the transformed contained value if it exists, or an empty optional otherwise
(public member function) [edit]