命名空间
变体
操作

std::experimental::any_cast

来自 cppreference.cn
< cpp‎ | experimental‎ | any
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行扩展 (parallelism TS)
并行扩展 2 (parallelism TS v2)
并发扩展 (concurrency TS)
并发扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions 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,则程序是非良构的。

[edit] 参数

operand - 目标 any 对象

[edit] 返回值

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 所含值的指针,否则返回空指针。

[edit] 异常

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