C++ 为什么std::any没有不安全的\u any \u演员阵容?

C++ 为什么std::any没有不安全的\u any \u演员阵容?,c++,boost,c++17,stdany,C++,Boost,C++17,Stdany,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 t

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));
}
//注意:任何\u cast的“不安全”版本都不是
//公共接口,可随时删除。他们是
//我们知道存储在any中的类型,但无法
//使用typeid()比较,例如,当我们的类型可能在
//不同的共享库。
模板
内联ValueType*unsafe\u any\u强制转换(any*操作数)BOOST\u NOEXCEPT
{
返回和静态转换

我注意到,似乎也不支持不安全的any cast

为什么C++17标准没有引入
std::unsafe\u any\u cast


如果找不到确切的原因(或者根本没有提出过),那么不提供对存储在
std::any
对象中的值的不安全访问的最有说服力的参数是什么?

std::any
是任何类型的单个值的类型安全的容器

请注意,从您发布的代码片段中的评论中可以看出,Boost的
不安全\u any \u cast
不是公共界面的一部分。它是一个实现细节,不打算供最终用户使用。这就是为什么文档中没有提到它的原因


将其升级到公共接口将首先破坏使用类型安全容器的目的。

我不同意上一句话,std::any确实有调用包含的对象构造函数或类型安全复制等用例。提供不安全转换不会破坏这些优点。(缺少不安全的_cast是我仍然坚持boost::any的原因)@ViktorSehr感谢您的评论。请注意,最后一段不是答案中的要点。要点是功能
unsafe\u any\u cast
不是Boost的公共界面的一部分,这意味着它可能在将来的版本中不可用。最后一段从设计角度添加了一个视图:拥有一个public函数的唯一目的是破坏类的契约,这不是一个好的设计决策。如果您需要一个不安全的强制转换,
std::any
(或者
boost::any
,就这一点而言)可能不是一个好办法。