std::experimental::any_cast

< cpp‎ | experimental‎ | any
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 不是空指针,且请求的 ValueTypetypeid 匹配 operandtypeid,则为指向 operand 所含值的指针,否则为空指针。

异常

1-3) 若请求的 ValueTypetypeid 不匹配 operand 内容的 typeid,则抛出 bad_any_cast