• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    class
    <any>

    std::bad_any_cast

    namespace std {
      class bad_any_cast : public bad_cast;
    }
    

    概要

    std::bad_any_castは、std::any_cast関数の失敗時に発生する例外である。

    メンバ関数

    名前 説明 対応バージョン
    bad_any_cast(); デフォルトコンストラクタ C++17
    const char* what() const noexcept override; エラー理由となる実装依存文字列 C++17

    #include <iostream>
    #include <any>
    
    int main()
    {
      std::any x = 3;
      try {
        // int型の値が入っているのに、double型として取り出そうとした
        std::any_cast<double>(x);
      }
      catch (std::bad_any_cast& e) {
        std::cout << "exception : " << e.what() << std::endl;
      }
    }
    

    出力例

    exception : bad any cast
    

    バージョン

    言語

    • C++17

    処理系

    参照