vendredi 30 mars 2018

returning shared_ptr member of an rvalue

In C++ Concurrency In Action - Practical MultiThreading page 167, there's the code snipet

 std::shared_ptr<T> wait_and_pop()
 {
 std::unique_ptr<node> const old_head=wait_pop_head();
 return old_head->data;
 }

I want to make sure I understand why it can't simply be like following. I understand that wait_pop_head() is an rvalue, thus will get destroyed when function returns, and std::shared_ptr<T> is not copyable, but the compiler would use move constructor to move things out of the shared_ptr. But that move operation happens after the rvalue gets destroyed. Is that the reason why we can't shorthand the code to following? Thank you in advance!

std::shared_ptr<T> wait_and_pop()
{
return wait_pop_head()->data;
}

Aucun commentaire:

Enregistrer un commentaire