samedi 23 juillet 2016

Why std::move didn't move

Wrote the following code in vs13:

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));

Works but didn't move, it called string copy ctor instead. myvector still had his "Hello World"s at the end. Using regular c++ 98 iteration like this:

 std::vector<std::string> myvector(1000);
 std::fill(myvector.begin(), myvector.end(), "Hello World");
 std::vector<std::string> pushto1;
 for (auto s = myvector.begin(); s != myvector.end();s++)
    pushto1.push_back(std::move(*s));

Actually worked, and move was called. myvector strings were empty. Why the first more modern notation doesnt work?

Aucun commentaire:

Enregistrer un commentaire