最終更新日時(UTC):
が更新

履歴 編集

function template
<optional>

std::optional::and_then(C++23)

template <class F> constexpr auto and_then(F&& f) &;       // (1)
template <class F> constexpr auto and_then(F&& f) &&;      // (2)
template <class F> constexpr auto and_then(F&& f) const&;  // (3)
template <class F> constexpr auto and_then(F&& f) const&&; // (4)

概要

有効値を保持していれば、値に対してfを適用した結果をoptionalとして返す。 有効値を保持していなければ、std::nulloptを返す。

実際には複数オーバーロードが提供されるが、大まかには下記シグニチャのようにみなせる。 and_thenへは、引数リストに1個のT型をとりstd::optional<Return>型を返す関数や関数オブジェクトを与える。

template <class T>
class optional {
  template <class Return>
  std::optional<Return> and_then(function<std::optional<Return>(T)> func);
};

適格要件

説明用のU型を次の通りとする:

remove_cvref_t<U>optionalの特殊化であること

効果

備考

and_thenは、メソッドチェーンをサポートするモナド風(monadic)操作として導入された。 関数型プログラミングの文脈における Monadic Bind 操作に対応する。

#include <cassert>
#include <optional>

// 正数なら2倍/それ以外は無効値を返す関数
std::optional<int> twice(int n)
{
  if (0 < n) {
    return n * 2;
  } else {
    return std::nullopt;
  }
}

int main()
{
  std::optional<int> o1 = 2;
  assert(o1.and_then(twice).value() == 4);

  std::optional<int> o2 = -1;
  assert(not o2.and_then(twice).has_value());

  std::optional<int> o3 = std::nullopt;
  assert(not o3.and_then(twice).has_value());
}

出力

バージョン

言語

  • C++23

処理系

関連項目

参照