通过引用或值shared_ptr的? [英] shared_ptr by reference or by value?

查看:102
本文介绍了通过引用或值shared_ptr的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在功能应采取的shared_ptr (从升压或C ++ STL 11),你向它传递

When a function should take a shared_ptr (from boost or C++11 STL), are you passing it


  • 通过const引用:无效美孚(常量的shared_ptr< T>&安培; P)

或值:<?code>无效美孚(shared_ptr的&LT; T&GT; P)

因为我怀疑它的速度更快我会preFER第一种方法。但是这真的值得,虽然还是有什么其他问题?

I would prefer the first method because I suspect it to be faster. But is this really worth a though or are there any additional issues?

您能给您的选择,或者如果这样,为何你认为这并不重要。

Could you please give the reasons for your choice or if the case, why you think that it does not matter.

推荐答案

这个问题已经讨论,并在<一个由Scott,安德烈和Herb回答href=\"http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything\">Ask我们什么在 C ++和超越2011 会话。从4分钟34sec <一块手表href=\"https://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything#time=04m34s\">on 的shared_ptr 性能和正确性。

This question has been discussed and answered by Scott, Andrei and Herb during Ask Us Anything session at C++ and Beyond 2011. Watch from 4min 34sec on shared_ptr performance and correctness.

不久,没有理由按值传递,除非出现多线程怎么回事,但是这需要单独考虑。

Shortly, there is no reason to pass by value, unless there is multi-threading going on, but this needs to be considered separately.

除非,你可以移动,优化它由斯科特迈尔斯在上文中的谈话录像解释,但毕竟是涉及到C ++,你可以使用实际的版本。

Unless, you can move-optimise it as explained by Scott Meyers in the talk video linked above, but that is related to actual version of C++ you can use.

一个重大更新到这一讨论中 GoingNative 2012 会议的<一个已经发生href=\"http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Interactive-Panel-Ask-Us-Anything-\">Interactive面板:!向我们什么这是值得关注的,特别是从<一个href=\"https://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Interactive-Panel-Ask-Us-Anything-#time=22m50s\">22:50.

A major update to this discussion has happened during GoingNative 2012 conference's Interactive Panel: Ask Us Anything! which is worth watching, especially from 22:50.

这篇关于通过引用或值shared_ptr的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆