std::optional<T>::or_else
来自cppreference.com
工具库
std::optional
成员函数 | ||||
观察器 | ||||
单子操作 | ||||
(C++23)
|
||||
(C++23)
|
||||
optional::or_else
(C++23)
|
||||
修改器 | ||||
非成员函数 | ||||
推导指引 | ||||
辅助类 | ||||
辅助对象 | ||||
template<
class F
>
constexpr optional or_else( F&& f ) const&; |
(1) | (C++23 起) |
template<
class F
>
constexpr optional or_else( F&& f ) &&; |
(2) | (C++23 起) |
若 *this
含值则返回它。否则返回 f
的结果。
若 std::remove_cvref_t<std::invoke_result_t<F>> 与 std::optional<T> 不是同一类型则程序非良构。
1) 等价于 return *this ? *this : std::forward<F>(f)(); 。此重载只有在 std::copy_constructible<T> 与 std::invocable<F>
均得到实现时才会参与重载决议。
2) 等价于 return *this ? std::move(*this) :
std::forward<F>(f)(); 。此重载只有在 std::move_constructible<T> 与 std::invocable<F>
均得到实现时才会参与重载决议。
参数
f | - | 返回 std::optional<T> 的函数或可调用 (Callable) 对象 |
返回值
*this 或
f
的结果,如上所示。
注解
功能特性测试宏 | 值 | 标准 | 备注 |
---|---|---|---|
__cpp_lib_optional
|
202110L | (C++23) | std::optional 的单子操作 |
示例
本节未完成 原因:暂无示例 |
参阅
在所含值可用时返回它,否则返回另一个值 (公开成员函数) |
|
(C++23)
|
在所含值存在时返回对其应用给定的函数的结果,否则返回空的 optional (公开成员函数) |
(C++23)
|
在所含值存在时返回含有变换后的所含值的 optional ,否则返回空的
optional (公开成员函数) |