我的本地版本的 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));
}
即使在线文档甚至不承认它们的存在: http: //www.boost.org/doc/libs/1_59_0/doc/html/any/reference.html
我注意到std::any
似乎也不支持不安全的任何演员。
为什么没有引入 C++17 标准std::unsafe_any_cast
?
如果找不到确切的原因(或者如果根本没有提出),那么不提供对存储在std::any
对象中的值的不安全访问的最令人信服的论点是什么?