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::bad_optional_access - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. optional
  3. bad_optional_access

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

class
<optional>

std::bad_optional_access(C++17)

namespace std {
  class bad_optional_access : public exception;
}

概要

std::bad_optional_accessは、std::optionalクラスのオブジェクトの無効な値にアクセスした場合に発生する例外である。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++17
(destructor) デストラクタ C++17
operator= 代入演算子 C++17
what エラー理由を取得する C++17

例

#include <iostream>
#include <optional>

int main()
{
  std::optional<int> p;
  try {
    p.value(); // 有効値を保持していないのに、有効値を取り出そうとした
  }
  catch (std::bad_optional_access& e) {
    std::cout << "exception : " << e.what() << std::endl;
  }
}

出力例

exception : bad optional access

バージョン

言語

  • C++17

処理系

  • Clang: 4.0.1 ✅
  • GCC: 7.2 ✅
  • Visual C++: ??

参照

  • LWG Issue 2806. Base class of bad_optional_access
You are visitor 01402503 — 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.