简体   繁体   English

返回右值的shared_ptr成员

[英]returning shared_ptr member of an rvalue

In C++ Concurrency In Action - Practical MultiThreading page 167, there's the code snipet 在《 C ++并发操作中的实用多线程》第167页中,有代码片段

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

Why do we have to move assign rvalue wait_pop_head() to a const variable first? 为什么我们必须首先将赋值右值wait_pop_head()移至const变量? Is there a reason why we can't shorthand the code to following? 为什么我们不能简写下面的代码?

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

Indeed, there is no reason that your alternative could not be used. 确实,没有理由不能使用您的替代方案。

The temporary will live long enough. 该临时文件将生存足够长的时间。

But some people just prefer to write it out. 但是有些人只喜欢写出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM