名前空間
変種
操作

std::any_cast

From cppreference.com
< cpp‎ | utility‎ | any
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
ヘッダー <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以降)

格納されたオブジェクトに型安全にアクセスします。

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

1) std::is_constructible_v<T, const U&>false の場合、プログラムは不適切です。
2) std::is_constructible_v<T, U&>false の場合、プログラムは不適切です。
3) std::is_constructible_v<T, U>false の場合、プログラムは不適切です。
4,5) std::is_void_v<T>true の場合、プログラムは不適切です。

目次

[編集] パラメータ

operand - 対象の `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 がヌルポインタではなく、要求された Ttypeidoperand の内容の typeid と一致する場合、オペランドによって保持されている値へのポインタ。それ以外の場合はヌルポインタ。

[編集] 例外

1-3) 要求された Ttypeidoperand の内容の typeid と一致しない場合、std::bad_any_cast をスローします。

[編集]

#include <any>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
 
int main()
{
    // Simple example
    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); // throws
    }
    catch (const std::bad_any_cast& e)
    {
        std::cout << "2) " << e.what() << '\n';
    }
 
    // Pointer example
    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";
 
    // Advanced example
    a1 = std::string("hello");
    auto& ra = std::any_cast<std::string&>(a1); // reference
    ra[1] = 'o';
 
    std::cout << "4) a1 is string: "
              << std::any_cast<const std::string&>(a1) << '\n'; // const reference
 
    auto s1 = std::any_cast<std::string&&>(std::move(a1)); // rvalue reference
    // Note: “s1” is a move-constructed std::string:
    static_assert(std::is_same_v<decltype(s1), std::string>);
 
    // Note: the std::string in “a1” is left in valid but unspecified state
    std::cout << "5) a1.size(): "
              << std::any_cast<std::string>(&a1)->size() // pointer
              << '\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) の動作は、Tvoid の場合に不明瞭でした。 この場合、プログラムは不適切です。
English 日本語 中文(简体) 中文(繁體)