This question already has an answer here:
I'm trying to understand what is wrong with following code where Result
is a class with a move constructor
:
Result&& GetResult(unsigned char* ptr)
{
Result res = Result(ptr);
return std::move(res);
}
Result ret = GetResult(ptr);
I know that the recommended way is to have return type Result
and return res;
and let compiler to call move constructor itself. But I want to understand what is going on in the case above with the lifetime of local variable res
, because it seems to be "dead" at the time it is passed to move constructor
in Result ret = GetResult(ptr);
. On the other hand Result&& res = GetResult(ptr);
keeps it alive.
Aucun commentaire:
Enregistrer un commentaire