#include <iostream>
#include <vector>
struct Widget {
Widget() = default;
Widget(const Widget&) { std::cout << "copy "; }
Widget(Widget&&) { /* no noexcept */ }
};
struct SafeWidget {
SafeWidget() = default;
SafeWidget(SafeWidget&&) noexcept {}
}; #include <iostream>
#include <vector>
struct Widget {
Widget() = default;
Widget(const Widget&) { std::cout << "copy "; }
Widget(Widget&&) { /* no noexcept */ }
};
struct SafeWidget {
SafeWidget() = default;
SafeWidget(SafeWidget&&) noexcept {}
}; std::vector<Widget>扩容时会使用移动构造函数
std::vector<Widget>扩容时会使用拷贝构造函数(如果有),因为移动构造函数不是noexcept
noexcept对vector的扩容策略没有影响
两个类的vector扩容行为完全相同

这道题你会答吗?花几分钟告诉大家答案吧!