命名空间
变体
操作

std::optional<T>::and_then

来自 cppreference.cn
< cpp‎ | utility‎ | optional
 
 
 
 
template< class F >
constexpr auto and_then( F&& f ) &;
(1) (since C++23)
template< class F >
constexpr auto and_then( F&& f ) const&;
(2) (since C++23)
template< class F >
constexpr auto and_then( F&& f ) &&;
(3) (since C++23)
template< class F >
constexpr auto and_then( F&& f ) const&&;
(4) (since C++23)

如果 *this 包含一个值,则使用包含的值作为参数调用 f,并返回该调用的结果;否则,返回一个空的 std::optional

返回类型(见下文)必须是 std::optional 的特化(与 transform() 不同)。否则,程序是非良构的。

1) 等价于
if (*this)
    return std::invoke(std::forward<F>(f), value());
else
    return std::remove_cvref_t<std::invoke_result_t<F, T&>>{};
2) 等价于
if (*this)
    return std::invoke(std::forward<F>(f), value());
else
    return std::remove_cvref_t<std::invoke_result_t<F, const T&>>{};
3) 等价于
if (*this)
    return std::invoke(std::forward<F>(f), std::move(value()));
else
    return std::remove_cvref_t<std::invoke_result_t<F, T>>{};
4) 等价于
if (*this)
    return std::invoke(std::forward<F>(f), std::move(value());
else
    return std::remove_cvref_t<std::invoke_result_t<F, const T>>{};

目录

[编辑] 参数

f - 一个合适的函数或 可调用 对象,它返回一个 std::optional

[编辑] 返回值

f 的结果或一个空的 std::optional,如上所述。

[编辑] 注解

一些语言称此操作为 flatmap

特性测试 Std 特性
__cpp_lib_optional 202110L (C++23) Monadic 操作std::optional

[编辑] 示例

#include <charconv>
#include <iomanip>
#include <iostream>
#include <optional>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
 
std::optional<int> to_int(std::string_view sv)
{
    int r{};
    auto [ptr, ec]{std::from_chars(sv.data(), sv.data() + sv.size(), r)};
    if (ec == std::errc())
        return r;
    else
        return std::nullopt;
}
 
int main()
{
    using namespace std::literals;
 
    const std::vector<std::optional<std::string>> v
    {
        "1234", "15 foo", "bar", "42", "5000000000", " 5", std::nullopt, "-43"
    };
 
    for (auto&& x : v | std::views::transform(
        [](auto&& o)
        {
            // debug print the content of input optional<string>
            std::cout << std::left << std::setw(13)
                      << std::quoted(o.value_or("nullopt")) << " -> ";
 
            return o
                // if optional is nullopt convert it to optional with "" string
                .or_else([]{ return std::optional{""s}; })
                // flatmap from strings to ints (making empty optionals where it fails)
                .and_then(to_int)
                // map int to int + 1
                .transform([](int n) { return n + 1; })
                // convert back to strings
                .transform([](int n) { return std::to_string(n); })
                // replace all empty optionals that were left by
                // and_then and ignored by transforms with "NaN"
                .value_or("NaN"s);
        }))
        std::cout << x << '\n';
}

输出

"1234"        -> 1235
"15 foo"      -> 16
"bar"         -> NaN
"42"          -> 43
"5000000000"  -> NaN
" 5"          -> NaN
"nullopt"     -> NaN
"-43"         -> -42

[编辑] 参见

如果可用,则返回包含的值,否则返回另一个值
(公共成员函数) [编辑]
(C++23)
如果存在,则返回包含转换后的值的 optional,否则返回一个空的 optional
(公共成员函数) [编辑]
(C++23)
如果 optional 包含值,则返回 optional 本身,否则返回给定函数的结果
(公共成员函数) [编辑]