std::optional<T>::or_else
Min standard notice:
Returns *this if it contains a value. Otherwise, returns the result of f.
# Declarations
template< class F >
constexpr optional or_else( F&& f ) const&;
(since C++23)
template< class F >
constexpr optional or_else( F&& f ) &&;
(since C++23)
# Parameters
f: a function or Callable object that returns an std::optional
# Return value
*this or the result of f, as described above.
# Notes
Feature-test macro Value Std Feature __cpp_lib_optional 202110L (C++23) Monadic operations in std::optional
# Example
#include <iostream>
#include <optional>
#include <string>
int main()
{
using maybe_int = std::optional<int>;
auto valueless = []
{
std::cout << "Valueless: ";
return maybe_int{0};
};
maybe_int x;
std::cout << x.or_else(valueless).value() << '\n';
x = 42;
std::cout << "Has value: ";
std::cout << x.or_else(valueless).value() << '\n';
x.reset();
std::cout << x.or_else(valueless).value() << '\n';
}