We know that std::move does not actually move anything. It just cast an lvalue reference (&) to rvalue reference (&&).
Then how in the following example, the copy constructor is called? If there is not move constructor, how does constructing an object that is using std::move() falls back on copy constructor? Exactly how does this binding for variable b
happen?
struct Test {
// Default constructor
Test() {
std::cout << "Constructor is called." << std::endl;
mValue = 0;
}
// Copy constructor
Test(const Test& rhs) {
std::cout << "Copy Constructor is called." << std::endl;
mName = rhs.mName;
mValue = rhs.mValue;
}
std::string mName;
int mValue;
};
int main() {
Test a;
Test b = std::move(a);
return 0;
}
Output:
Constructor is called.
Copy Constructor is called.
Aucun commentaire:
Enregistrer un commentaire