返回rvalue的共享_ptr成员

returning shared_ptr member of an rvalue

本文关键字:ptr 成员 共享 rvalue 返回      更新时间:2023-10-16

在C 并发中,行动 - 实用的多线程页面167,有代码nipet

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

为什么我们必须先将RVALUE wait_pop_head()分配到const变量?有什么原因为什么我们不能概括遵循代码?

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

的确,没有理由无法使用您的选择。

临时寿命将足够长。

但是有些人只是更喜欢写出来。