#include <QVector>#include <QTextStream>struct MoveTest{ int i; Movetest() {} MoveTest(const MoveTest& other) {QTextStream(stdout) << "constr copy" << endl;} MoveTest(MoveTest &&other) {QTextStream(stdout) << "constr move" << endl;} ~Movetest() {} inline MoveTest& operator= (const MoveTest& other) {QTextStream(stdout) << "copy" << endl;} inline MoveTest& operator= (MoveTest &&other) {QTextStream(stdout) << "move" << endl;}};int main(int argc,char *argv[]){ QTextStream(stdout) << "std::move:" << endl; MoveTest t1; MoveTest t2(std::move(t1)); t1 = std::move(t2); QTextStream(stdout) << "QVector:" << endl; QVector<MoveTest> qmTest(5); qmTest.insert(qmTest.begin(),Movetest()); QTextStream(stdout) << "std::vector:" << endl; std::vector<MoveTest> mTest(5); mTest.insert(mTest.begin(),Movetest()); return 0;}
我的输出是gcc 4.7.2,QMAKE_CXXFLAGS = -std = c 0x:
std::move:constr movemoveQVector:constr copyconstr copyconstr copyconstr copyconstr copyconstr copycopycopycopycopycopycopystd::vector:constr moveconstr copyconstr copyconstr copyconstr copyconstr copy
如何在不复制的情况下插入具有内部移位的元素?需要什么GCC标志?
解决方法 由于您的move运算符可以抛出异常,因此std :: vector无法使用它.如果运算符在调整大小过程中途抛出异常,它会怎么做?如果它不能抛出异常并且向量实现可以使用它,则声明它为 noexcept. 总结以上是内存溢出为你收集整理的c – 内部移动std :: vector元素和QVector全部内容,希望文章能够帮你解决c – 内部移动std :: vector元素和QVector所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)