This question already has an answer here:
I was studying the difference between x value and l value from stackoverflow, github and cppreference and yet not convinced about the difference between l value and x value.
One problem is that to understand x value, one should understand std::move and to understand std::move you should know x value [ref]. Circular!
Also, according to cppref example:
#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.
Therefore,
1- How does std::move harm the original value? (through which mechanism)
2- How does x value differ from l value? Is there any practical example for it? (Not a dummy example)
Aucun commentaire:
Enregistrer un commentaire