std::expected<T,E>::and_then
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    |   template< class F > constexpr auto and_then( F&& f ) &;  | 
(1) | (C++23 起) | 
|   template< class F > constexpr auto and_then( F&& f ) const&;  | 
(2) | (C++23 起) | 
|   template< class F > constexpr auto and_then( F&& f ) &&;  | 
(3) | (C++23 起) | 
|   template< class F > constexpr auto and_then( F&& f ) const&&;  | 
(4) | (C++23 起) | 
如果 *this 包含期待的值,调用 f 并返回其结果;否则,返回一个包含了 error() 的复制的 std::expected 对象。
如果 T 不是(可有 cv 限定的) void ,包含的期待的值( value() )将作为参数传递给 f ;否则 f 不接受任何参数。
令 U 为:
-  如果 
T不是(可有 cv 限定的) void :- 对于重载 (1-2) , std::remove_cvref_t<std::invoke_result_t<F, decltype(value())>> ;
 - 对于重载 (3-4) , std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(value()))>> ;
 
 -  否则( 
T是(可有 cv 限定的) void ), std::remove_cvref_t<std::invoke_result_t<F>> 。 
返回值类型是 U ,其必须是 std::expected 的特化,且 std::is_same_v<U::error_type, E> 必须为 true 。
1-2) 等价于
这些重载只有在std::is_constructible_v<E, decltype(error())> 为 true 时才会参与重载决议 。
if (has_value()) { if constexpr (std::is_void_v<T>) return std::invoke(std::forward<F>(f)); else return std::invoke(std::forward<F>(f), value()); } else { return U(std::unexpect, error()); }
3-4) 等价于
这些重载只有在std::is_constructible_v<E, decltype(std::move(error()))> 为 true 时才会参与重载决议 。
if (has_value()) { if constexpr (std::is_void_v<T>) return std::invoke(std::forward<F>(f)); else return std::invoke(std::forward<F>(f), std::move(value())); } else { return U(std::unexpect, std::move(error())); }
参数
| f | - | 适合的函数或 可调用 (Callable) 对象,返回 std::expected | 
返回值
f 的结果或包含不期待的值的 std::expected 对象,如上所述
注解
| 功能特性测试宏 | 值 | 标准 | 注释 | 
|---|---|---|---|
__cpp_lib_expected | 
202211L | (C++23) | std::expected 的单子函数
 | 
示例
| 本节未完成 原因:暂无示例  | 
参阅
|    (C++23)  | 
   expected 中不期待的值的原位构造标签  (类) (常量)  | 
   若期待的值存在则返回含有变换后期待的值的 expected ,否则返回 expected 本身 (公开成员函数)  |