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

履歴 編集

class template
<execution>

std::execution::with_error(C++26)

namespace std::execution {
  template<class E>
  struct with_error {
    using type = remove_cvref_t<E>;
    type error;
  };

  template<class E>
  with_error(E) -> with_error<E>;
}

概要

タスクexecution::task戻り値型とするコルーチンにおいて、co_yield式でエラー完了すること表現するタグ型。

co_yield with_error{err}

Errのエラー完了シグネチャset_error_t(Err)を持つタスクコルーチンにおいて、非同期操作Err型のエラー値errによってエラー完了させる。このco_yield式は決して再開(resume)されない。

詳細仕様はtask::promise_type::yield_valueを参照のこと。

#include <execution>
#include <print>
namespace ex = std::execution;

enum class MyErrCode { InvalidParam, OutOfMemory };

struct Context {
  using error_types = ex::completion_signatures<ex::set_error_t(MyErrCode)>;
};

ex::task<int, Context> work(int arg) noexcept
{
  if (arg < 0) {
    co_yield ex::with_error{MyErrCode::InvalidParam};
  }
  co_return arg;
}

int main()
{
  std::this_thread::sync_wait(
    work(-1)
    | ex::upon_error([](MyErrCode ec) {
      std::println("error");
    })
  );
}

出力

error

バージョン

言語

  • C++26

処理系

関連項目

参照