为什么std :: vector尽管声明为noexcept(false),但仍使用move构造函数

编程入门 行业动态 更新时间:2024-10-25 21:22:41
本文介绍了为什么std :: vector尽管声明为noexcept(false),但仍使用move构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

无论我在网上读什么书,都强烈建议如果我希望我的班级与std::vector一起正常工作(即,我班级的移动语义被std::vector使用),我应该将care构造函数的delcare移动为'noexcept '(或noexcept(true)).

Wherever I read in the internet, it is strongly adviced that if I want my class to be working well with std::vector (i.e. move semantics from my class were used by std::vector) I should delcare move constructor as 'noexcept' ( or noexcept(true) ).

#include <iostream> #include <vector> using std::cout; struct T { T() { cout <<"T()\n"; } T(const T&) { cout <<"T(const T&)\n"; } T& operator= (const T&) { cout <<"T& operator= (const T&)\n"; return *this; } ~T() { cout << "~T()\n"; } T& operator=(T&&) noexcept(false) { cout <<"T& operator=(T&&)\n"; return *this; } T(T&&) noexcept(false) { cout << "T(T&&)\n"; } }; int main() { std::vector<T> t_vec; t_vec.push_back(T()); }

输出:

T() T(T&&) ~T() ~T()

为什么? 我做错了什么?

Why ? What did I do wrong ?

在gcc 4.8.2上编译,并将CXX_FLAGS设置为:

Compiled on gcc 4.8.2 with CXX_FLAGS set to:

--std=c++11 -O0 -fno-elide-constructors

推荐答案

您没有做错任何事情.

您只是错误地认为push_back必须避免抛出move-ctor:至少在构造新元素时不需要这样做.

You just wrongly thought push_back had to avoid a throwing move-ctor: It does not, at least for constructing the new element.

必须避免唯一的抛出动圈/移动分配的地方是重新分配向量,以避免移动一半的元素,而其余的保留在其原始位置.

The only place where throwing move-ctors / move-assignments must be shunned is on re-allocation of the vector, to avoid having half the elements moved, and the rest in their original places.

该功能具有强大的异常安全保证:

The function has the strong exception-safety guarantee:

要么操作成功,要么操作失败,但没有任何改变.

Either the operation succeeds, or it fails and nothing has changed.

更多推荐

为什么std :: vector尽管声明为noexcept(false),但仍使用move构造函数

本文发布于:2023-11-16 18:10:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但仍   函数   声明   vector   std

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!