【发布时间】:2018-01-17 13:32:03
【问题描述】:
我的本地版本的 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 似乎也不支持不安全的 any 演员表。
为什么C++17标准没有引入std::unsafe_any_cast?
如果找不到确切的原因(或者如果根本没有提出),那么不提供对存储在 std::any 对象中的值的不安全访问的最有说服力的论点是什么?
【问题讨论】: