You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何C++17标准未引入std::unsafe_any_cast?Boost相关参考

关于std::any未引入unsafe_any_cast的疑问

我本地使用的Boost头文件版本为1.56.0,其boost/any.hpp中定义了如下非公开函数,原文复制如下:

// Note: The "unsafe" versions of any_cast are not part of the
// public interface and may be removed at any time. They are
// required where we know what type is stored in the any and can't
// use typeid() comparison, e.g., when our types may travel across
// different shared libraries.
template<typename ValueType>
inline ValueType * unsafe_any_cast(any * operand) BOOST_NOEXCEPT
{
    return &static_cast<any::holder<ValueType> *>(operand->content)->held;
}
template<typename ValueType>
inline const ValueType * unsafe_any_cast(const any * operand) BOOST_NOEXCEPT
{
    return unsafe_any_cast<ValueType>(const_cast<any *>(operand));
}

但Boost 1.59.0的文档并未提及该函数的存在,同时我注意到C++17标准中的std::any也未提供unsafe_any_cast特性。现问询:

  • 为何C++17标准未引入std::unsafe_any_cast?
  • 若无法找到确切原因(或该特性从未被提议),不提供std::any存储值的不安全访问的最具说服力的论点是什么?

内容的提问来源于stack exchange,提问作者Maarten Bamelis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:28:04