mercredi 9 juin 2021

std::move does not move for rvalue reference

I am new to move semantics.

When I run the following code:

std::string h{"move"};
auto r = std::move(h);
std::cout << h << std::endl;
std::cout << r << std::endl;

I get the expected result:

{empty line} 
move

However when I run this:

std::string h{"move"};
auto&& r = std::move(h);
std::cout << h << std::endl;
std::cout << r << std::endl;

While I expected the same output as above, I get the output:

move
move

Why was not the move constructor called in this case and the value was set to some unspecified value?

Aucun commentaire:

Enregistrer un commentaire