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

履歴 編集

function
<expected>

std::expected::operator->(C++23)

constexpr const T* operator->() const noexcept;  // (1)
constexpr T* operator->() noexcept;              // (2)

概要

保持している正常値のメンバにアクセスする。

事前条件

has_value() == true

戻り値

正常値へのポインタ。

例外

投げない

#include <expected>
#include <iostream>
#include <string>

int main()
{
  std::expected<std::string, int> x = "Hello";
  std::cout << x->size() << std::endl;

  std::expected<std::string, int> y = std::unexpected{42};
//std::cout << y->size() << std::endl;
  // エラー値を保持する y に対する operator-> 呼び出しは未定義動作
}

出力

5

バージョン

言語

  • C++23

処理系

関連項目

参照