写了下面的代码vs13:为什么的std ::动未动
std::vector<std::string> myvector(1000);
std::fill(myvector.begin(), myvector.end(), "Hello World");
std::vector<std::string> pushto;
for (auto s: myvector)
pushto.push_back(std::move(*s));
作品,但没有动,叫字符串拷贝构造函数来代替。 myvector
最后还是有他的“Hello World”。 使用常规的C++ 98重复这样的:
std::vector<std::string> myvector(1000);
std::fill(myvector.begin(), myvector.end(), "Hello World");
std::vector<std::string> pushto;
for (auto s = myvector.begin(); s != myvector.end();s++)
pushto.push_back(std::move(*s));
实际工作,和移动被调用。 myvector
字符串是空的。 为什么第一个现代符号不起作用?
第一个例子是否真的编译?我不认为'std :: move(* s)'中应该有'*'。 – aschepler
请注意,没有人曾表示移动应该删除原始文件。移动后,原始文件处于*有效但未指定*状态。即使将'&&放在适当的位置,对原始对象具有与新对象相同的值(即,与之前相同,直到修改),这是完全有效的(并且,imho,有时是期望的)。 – lorro