In the following examples from cpp reference:
#include <iostream>
#include <utility>
#include <vector>
#include <string>
int main()
{
std::string str = "Hello";
std::vector<std::string> v;
// uses the push_back(const T&) overload, which means
// we'll incur the cost of copying str
v.push_back(str);
std::cout << "After copy, str is \"" << str << "\"\n";
// uses the rvalue reference push_back(T&&) overload,
// which means no strings will be copied; instead, the contents
// of str will be moved into the vector. This is less
// expensive, but also means str might now be empty.
v.push_back(std::move(str));
std::cout << "After move, str is \"" << str << "\"\n";
std::cout << "The contents of the vector are \"" << v[0]
<< "\", \"" << v[1] << "\"\n";
}
Using std::move may cause the original value be loosed. To me, it looks like
v.push_back(std::move(str))
causes a new member v[1] being created. Then,
&v[1]=&str
But why should it damage the value in str? It does not make sense.
There are many complicated tutorials about std::move which are harder than my own question to understand.
Could any one please write
v.push_back(std::move(str))
in its equivalent using c++03?
I look for an explanation whose understanding is easy and do not contain prerequisites such as x-value , static_cast, remove_reference, etc, as they themselves require to understand std::move first. Please avoid this circular dependency.
Also these links do not answer my question: 7510182, 3413470
Because I am interested in knowing how str is harm and not what happens to v[1].
Psudo code is also welcome as far as it is as simple as c++03.
Aucun commentaire:
Enregistrer un commentaire