命名空间
变体
操作

std::experimental::any_cast

来自 cppreference.cn
< cpp‎ | experimental‎ | any
 
 
实验性
技术规范
文件系统库 (文件系统 TS)
库基础 (库基础 TS)
库基础 2 (库基础 TS v2)
库基础 3 (库基础 TS v3)
并行性扩展 (并行性 TS)
并行性扩展 2 (并行性 TS v2)
并发性扩展 (并发性 TS)
并发扩展 2 (concurrency TS v2)
概念 (概念 TS)
范围 (范围 TS)
反射 (反射 TS)
数学特殊函数 (特殊函数 TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
 
template<class ValueType>
    ValueType any_cast(const any& operand);
(1) (库基础 TS)
template<class ValueType>
    ValueType any_cast(any& operand);
(2) (库基础 TS)
template<class ValueType>
    ValueType any_cast(any&& operand);
(3) (库基础 TS)
template<class ValueType>
    const ValueType* any_cast(const any* operand) noexcept;
(4) (库基础 TS)
template<class ValueType>
    ValueType* any_cast(any* operand) noexcept;
(5) (库基础 TS)

对所包含的对象执行类型安全的访问。

对于 (1-3),如果 ValueType 不是引用且 std::is_copy_constructible<ValueType>::valuefalse,则程序格式错误。

[编辑] 参数

operand - 目标 any 对象

[编辑] 返回值

1) 返回 *any_cast<std::add_const_t<std::remove_reference_t<ValueType>>>(&operand)
2,3) 返回 *any_cast<std::remove_reference_t<ValueType>>(&operand)
4,5) 如果 operand 不是空指针,并且请求的 ValueTypetypeidoperand 内容的 typeid 匹配,则返回指向 operand 所包含值的指针,否则返回空指针。

[编辑] 异常

1-3) 如果请求的 ValueTypetypeidoperand 内容的 typeid 不匹配,则抛出 bad_any_cast