有人能告诉我为什么这段代码没有输出任何东西吗? 我假设它与 move 线有关...
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> v{66,79,154,24,76,13,7};
v = move(v);
for(auto i: v)
cout << i << " ";
}
更新:所以我添加了 system("pause");帮助自己。我是否需要它不是我关注的重点。当我在 Visual Studio 2013 中再次运行代码时,它成功了。但是,当我使用 C++14 通过 Ideone 运行它时,它没有输出任何内容。现在有点困惑。
最佳答案
使用xvalue
参数调用的标准库函数可能假定参数是对对象的唯一引用;如果它是从带有 std::move
的 lvalue
构造的,则不进行别名检查。特别是,这意味着标准库 move 赋值运算符不必执行自赋值检查:
std::vector<int> v = {2, 3, 3};
v = std::move(v); // undefined behavior
更多详情请引用std::move和 this question
关于c++ - move 使用无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42188844/