std::any_cast
来自cppreference.com
| 在标头 <any> 定义
|
||
| |
(1) | (C++17 起) |
| |
(2) | (C++17 起) |
| |
(3) | (C++17 起) |
| (4) | (C++17 起) | |
| |
(5) | (C++17 起) |
进行对所含有对象的类型安全访问。
令 U 为 std::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)))。异常
示例
Run this code
#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 3305 | C++17 | 当 T 是 void 时 (4,5) 的行为不明确
|
此时程序非良构 |