为什么对于std::vector::resize()的一个重载忽略了移动操作的抛出?

在 C++ 标准vector.capacity部分,它为 定义了两个重载resize()。(另见https://en.cppreference.com/w/cpp/container/vector/resize)

此重载要求类型 T 是MoveInsertableand DefaultInsertable

constexpr void resize(size_type sz);

另一个重载要求类型 T 是CopyInsertable

constexpr void resize(size_type sz, const T& c);

标准中提到的部分的第 16 条规定了第一次重载:

如果非 Cpp17CopyInsertable T的移动构造函数引发异常,则不会产生任何影响。

但是对于第二次重载,它指出:

如果抛出异常,则没有任何影响。

第二个重载中没有提到移动构造函数的抛出。为什么?

回答

第二个重载中没有提到移动构造函数的抛出。为什么?

因为第二个重载要求类型为 CopyInsertable。鉴于该要求,第二次重载不存在“非 Cpp17CopyInsertable T”的情况,因此无需提及该情况。

  • @Xlv "Prefer strong exception safety" is the uniform "culture" that you can see throughout the C++ standard. In this case, the strong exception safety can be achieved by using copy instead of potentially throwing move.

以上是为什么对于std::vector::resize()的一个重载忽略了移动操作的抛出?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>