Namespaces
Variants

std::any_cast

ja.cppreference.net から
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変長引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
Swap と 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通のボキャブラリ型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
ヘッダ <any>
template< class T >
T any_cast( const any& operand );
で定義 (1) (C++17以上)
template< class T >
T any_cast( any& operand );
(2) (C++17以上)
template< class T >
T any_cast( any&& operand );
(3) (C++17以上)
template< class T >
const T* any_cast( const any* operand ) noexcept;
(4) (C++17以上)
template< class T >
T* any_cast( any* operand ) noexcept;
(5) (C++17以上)

格納されているオブジェクトへの型安全なアクセスを実行します。

U を std::remove_cv_t<std::remove_reference_t<T>> とします。

1) std::is_constructible_v<T, const U&> が false である場合、プログラムは ill-formed です。
2) std::is_constructible_v<T, U&> が false である場合、プログラムは ill-formed です。
3) std::is_constructible_v<T, U> が false である場合、プログラムは ill-formed です。
4,5) std::is_void_v<T> が true である場合、プログラムは ill-formed です。

パラメータ

オペランド - ターゲット any オブジェクト

戻り値

1,2) static_cast < T > ( * std :: any_cast < U > ( & operand ) ) を返します。
3) 返り値 static_cast < T > ( std :: move ( * std :: any_cast < U > ( & operand ) ) ) .
4,5) もし operand がnullポインタでなく、要求された typeid が operand の内容の型と一致する場合、operandが保持する値へのポインタ、そうでなければnullポインタ。

例外

1-3) std::bad_any_cast をスローする - 要求された typeid が T の内容と一致しない場合。
変更点: - 「Throws」を「をスローする」に翻訳 - 条件文を自然な日本語の語順に調整 - HTMLタグ、属性、コード内のテキストはそのまま保持 - C++固有の用語(std::bad_any_cast, typeid, T)は翻訳せず保持 - 専門的な技術文書として正確な翻訳を実施

例

#include <any>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
int main()
{
    // シンプルな例
    auto a1 = std::any(12);
    std::cout << "1) a1 is int: " << std::any_cast<int>(a1) << '\n';
    try
    {
        auto s = std::any_cast<std::string>(a1); // 例外をスロー
    }
    catch (const std::bad_any_cast& e)
    {
        std::cout << "2) " << e.what() << '\n';
    }
    // ポインタの例
    if (int* i = std::any_cast<int>(&a1))
        std::cout << "3) a1 is int: " << *i << '\n';
    else if (std::string* s = std::any_cast<std::string>(&a1))
        std::cout << "3) a1 is std::string: " << *s << '\n';
    else
        std::cout << "3) a1 is another type or unset\n";
    // 高度な例
    a1 = std::string("hello");
    auto& ra = std::any_cast<std::string&>(a1); // 参照
    ra[1] = 'o';
    std::cout << "4) a1 is string: "
              << std::any_cast<const std::string&>(a1) << '\n'; // const参照
    auto s1 = std::any_cast<std::string&&>(std::move(a1)); // 右辺値参照
    // 注: "s1" はムーブ構築された std::string
    static_assert(std::is_same_v<decltype(s1), std::string>);
    // 注: "a1" 内の std::string は有効だが未規定の状態になる
    std::cout << "5) a1.size(): "
              << std::any_cast<std::string>(&a1)->size() // ポインタ
              << '\n'
              << "6) s1: " << s1 << '\n';
}

出力例:

1) a1 is int: 12
2) bad any_cast
3) a1 is int: 12
4) a1 is string: hollo
5) a1.size(): 0
6) s1: hollo

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3305 C++17 オーバーロード ( 4,5 ) の動作は、 T が void の場合に不明確であった この場合、プログラムは不適格