GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::optional::and_then - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. optional
  3. optional
  4. and_then

最終更新日時: 2026年09月19日 21時31分51秒 (JST)
rotarymars が更新

履歴 編集

function template
<optional>

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

// optional<T>版のオーバーロード
template <class F> constexpr auto and_then(F&& f) &;       // (1) C++23
template <class F> constexpr auto and_then(F&& f) &&;      // (2) C++23
template <class F> constexpr auto and_then(F&& f) const&;  // (3) C++23
template <class F> constexpr auto and_then(F&& f) const&&; // (4) C++23

// optional<T&>版のオーバーロード (C++26)
template <class F> constexpr auto and_then(F&& f) const;   // (5) C++26

概要

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

  • (1) : *thisが非const左辺値の場合
  • (2) : *thisが非const右辺値の場合
  • (3) : *thisがconst左辺値の場合
  • (4) : *thisがconst右辺値の場合
  • (5) : optional<T&>の場合

optional<T>では (1)~(4) が定義され、optional<T&>では (5) のみが定義される。

実際には複数オーバーロードが提供されるが、大まかには下記シグニチャのようにみなせる。 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型を次の通りとする:

  • (1), (3) : invoke_result_t<F, decltype(value())>
  • (2), (4) : invoke_result_t<F, decltype(std::move(value()))>
  • (5) : invoke_result_t<F, decltype(*val)> (説明専用メンバvalはT*型)

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

効果

  • (1), (3) : 次と等価

    if (*this) {
      return invoke(std::forward<F>(f), value());
    } else {
      return remove_cvref_t<U>();
    }
    

  • (2), (4) : 次と等価

    if (*this) {
      return invoke(std::forward<F>(f), std::move(value()));
    } else {
      return remove_cvref_t<U>();
    }
    

  • (5) : 次と等価

    if (*this) {
      return invoke(std::forward<F>(f), *val);
    } else {
      return remove_cvref_t<U>();
    }
    

備考

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

処理系

  • Clang: 14.0.6 ✅
  • GCC: ??
  • Visual C++: ??

関連項目

  • transform
  • or_else

参照

  • P0798R8 Monadic operations for std::optional
  • P2988R12 std::optional<T&>
    • C++26で参照型T&に対する部分特殊化を追加
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.