std::any_cast

来自cppreference.com
< cpp‎ | utility‎ | any
 
 
 
 
在标头 <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) 如果 is_constructible_v<T, const U&>false,那么程序非良构。
2) 如果 is_constructible_v<T, U&>false,那么程序非良构。
3) 如果 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 不是空指针,且请求的 Ttypeid 匹配 operand 内容的 typeid,那么返回指向所含值的指针,否则返回空指针。

异常

1-3) 如果请求的 Ttypeid 不匹配 operand 内容的 typeid,那么就会抛出 std::bad_any_cast

示例

#include <any>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
 
int main()
{
    // 简单示例
    auto a1 = std::any(12);
    std::cout << "1) a1 是 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 是 int:" << *i << '\n';
    else if (std::string* s = std::any_cast<std::string>(&a1))
        std::cout << "3) a1 是 std::string:" << *s << '\n';
    else
        std::cout << "3) a1 是另一类型,或者没有设置\n";
 
    // 进阶示例
    a1 = std::string("hello");
    auto& ra = std::any_cast<std::string&>(a1); // 引用
    ra[1] = 'o';
 
    std::cout << "4) a1 是字符串:"
              << 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 是 int:12
2) bad any_cast
3) a1 是 int:12
4) a1 是 string:hollo
5) a1.size():0
6) s1:hollo

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告应用于出版时的行为正确行为
LWG 3305C++17Tvoid(4,5) 的行为不明确此时程序非良构