c++ - 为什么 valueType 的 std::any & operator= 不是有条件的 noexcept?
问题描述
这个问题很简单。
这是模板化 operator= for 的声明std::any
:
template<typename ValueType>
any& operator=( ValueType&& rhs );
我希望它是:
template<typename ValueType>
any& operator=( ValueType&& rhs ) noexcept(noexcept(std::declval<std::any>() = std::forward<ValueType>(std::declval<ValueType>()));
也就是说,如果您可以以 noexcept 方式将 ValueType 复制分配给 any,那么您应该能够拥有 noexcept。
也许我错过了一些东西。
解决方案
字面上的答案是这样的规范将是递归的(您是说noexcept
如果赋值是 ,则赋值应该是noexcept
)。
但是可能更有用的答案是,由于any
可能必须分配,因此您只能noexcept
在以下情况下decay_t<ValueType>
真正分配
- 足够小(以至于不需要分配),并且
- nothrow move 可构造,并且
- nothrow 可从
ValueType
指定条件的唯一方法是noexcept
要求您还指定“足够小”的含义 - 这将限制实现自由,以获得可疑的收益。
标准库通常不使用有条件的 noexcept - 那么为什么这是......异常?
推荐阅读
- php - 无法在 M1 Mac 上编译 php 模块 apcu
- m3u8 - m3u8 文件的问题 - 单独的音频和视频
- python - ImportError:没有系统模块'pywintypes'(pywintypes38.dll)
- laravel - 使用 vmware 和 bitbucket 存储库出现 500 错误
- python - 快速分组点的方法
- python - 在数组上使用 .join 时删除单引号(Python)
- reactjs - 如何在打字稿中打印反应组件?
- apache - 用于主机名的 Apache HTTP RewriteRule
- python - 如何在我的 Tkinter 程序中限制用户的连接?
- csv - 在 unix 系统上的 csv 文件中删除引号内的返回插入符号