为何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
相关产品推荐
相关产品推荐

