我已经检查了很多移动构造函数/ vector/noexcept线程,但我仍然不确定当事情出错时会发生什么.我期望的时候不会产生错误,所以要么我的小测试错了,要么我对问题的理解是错误的.
我正在使用一个BufferTrio对象的向量,它定义了一个noexcept(false)移动构造函数,并删除了所有其他构造函数/赋值运算符,以便没有任何内容可以回退到:
BufferTrio(const BufferTrio&) = delete;
BufferTrio& operator=(const BufferTrio&) = delete;
BufferTrio& operator=(BufferTrio&& other) = delete;
BufferTrio(BufferTrio&& other) noexcept(false)
: vaoID(other.vaoID)
, vboID(other.vboID)
, eboID(other.eboID)
{
other.vaoID = 0;
other.vboID = 0;
other.eboID = 0;
}
Run Code Online (Sandbox Code Playgroud)
编译和运行的东西,但从https://xinhuang.github.io/posts/2013-12-31-when-to-use-noexcept-and-when-to-not.html:
std :: vector将在需要增加(或减少)容量时使用move,只要移动操作为noexcept即可.
或者来自优化的C++:经过验证的高性能技术作者:Kurt Guntheroth:
如果移动构造函数和移动赋值运算符未声明为noexcept,则std :: vector使用效率较低的复制操作.
既然我删除了那些,我的理解是应该在这里打破一些东西.但是这个载体的运行正常.所以我还创建了一个基本循环,将push_backs五十万次转换为一个虚拟向量,然后将该向量与另一个单元素虚拟向量交换.像这样:
vector<BufferTrio> thing;
int n = 500000;
while (n--)
{
thing.push_back(BufferTrio());
}
vector<BufferTrio> thing2;
thing2.push_back(BufferTrio());
thing.swap(thing2);
cout << "Sizes are " << thing.size() << " and " << …Run Code Online (Sandbox Code Playgroud)