• <bdo id='NiHI2'></bdo><ul id='NiHI2'></ul>
    <legend id='NiHI2'><style id='NiHI2'><dir id='NiHI2'><q id='NiHI2'></q></dir></style></legend>

      <small id='NiHI2'></small><noframes id='NiHI2'>

      <i id='NiHI2'><tr id='NiHI2'><dt id='NiHI2'><q id='NiHI2'><span id='NiHI2'><b id='NiHI2'><form id='NiHI2'><ins id='NiHI2'></ins><ul id='NiHI2'></ul><sub id='NiHI2'></sub></form><legend id='NiHI2'></legend><bdo id='NiHI2'><pre id='NiHI2'><center id='NiHI2'></center></pre></bdo></b><th id='NiHI2'></th></span></q></dt></tr></i><div id='NiHI2'><tfoot id='NiHI2'></tfoot><dl id='NiHI2'><fieldset id='NiHI2'></fieldset></dl></div>
    1. <tfoot id='NiHI2'></tfoot>
    2. 我们应该通过引用还是通过值传递 shared_ptr ?

      时间:2023-06-29

      <small id='rcRsV'></small><noframes id='rcRsV'>

          <tfoot id='rcRsV'></tfoot>
        1. <i id='rcRsV'><tr id='rcRsV'><dt id='rcRsV'><q id='rcRsV'><span id='rcRsV'><b id='rcRsV'><form id='rcRsV'><ins id='rcRsV'></ins><ul id='rcRsV'></ul><sub id='rcRsV'></sub></form><legend id='rcRsV'></legend><bdo id='rcRsV'><pre id='rcRsV'><center id='rcRsV'></center></pre></bdo></b><th id='rcRsV'></th></span></q></dt></tr></i><div id='rcRsV'><tfoot id='rcRsV'></tfoot><dl id='rcRsV'><fieldset id='rcRsV'></fieldset></dl></div>

            1. <legend id='rcRsV'><style id='rcRsV'><dir id='rcRsV'><q id='rcRsV'></q></dir></style></legend>
                <tbody id='rcRsV'></tbody>

              • <bdo id='rcRsV'></bdo><ul id='rcRsV'></ul>
                本文介绍了我们应该通过引用还是通过值传递 shared_ptr ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当一个函数接受一个 shared_ptr(来自 boost 或 C++11 STL)时,你是否通过了它:

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

                • 通过常量引用:void foo(const shared_ptr<T>& p)

                或按值:void foo(shared_ptr p) ?

                我更喜欢第一种方法,因为我怀疑它会更快.但这真的值得吗?还有其他问题吗?

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

                能否请您说明您选择的理由,或者如果情况如此,您认为这无关紧要的原因是什么.

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

                推荐答案

                Scott、Andrei 和 Herb 在 问我们任何问题 会话在 C++ 及 2011 年以后.从 4:34 开始观看 关于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 4:34 on shared_ptr performance and correctness.

                简而言之,没有理由传递值,除非目标是共享对象的所有权(例如,在不同的数据结构之间,或在不同的线程之间).

                Shortly, there is no reason to pass by value, unless the goal is to share ownership of an object (eg. between different data structures, or between different threads).

                除非您可以像 Scott Meyers 在上面链接的谈话视频中解释的那样对其进行移动优化,但这与您可以使用的 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 会议的 交互式面板:问我们任何问题!值得一看,尤其是来自 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 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:C++ - 为什么 boost::hash_combine 是组合散列值的最佳方式? 下一篇:Boost:asio 的最佳文档?

                相关文章

                <tfoot id='ZYNHf'></tfoot>

                1. <i id='ZYNHf'><tr id='ZYNHf'><dt id='ZYNHf'><q id='ZYNHf'><span id='ZYNHf'><b id='ZYNHf'><form id='ZYNHf'><ins id='ZYNHf'></ins><ul id='ZYNHf'></ul><sub id='ZYNHf'></sub></form><legend id='ZYNHf'></legend><bdo id='ZYNHf'><pre id='ZYNHf'><center id='ZYNHf'></center></pre></bdo></b><th id='ZYNHf'></th></span></q></dt></tr></i><div id='ZYNHf'><tfoot id='ZYNHf'></tfoot><dl id='ZYNHf'><fieldset id='ZYNHf'></fieldset></dl></div>

                2. <small id='ZYNHf'></small><noframes id='ZYNHf'>

                    <bdo id='ZYNHf'></bdo><ul id='ZYNHf'></ul>

                    <legend id='ZYNHf'><style id='ZYNHf'><dir id='ZYNHf'><q id='ZYNHf'></q></dir></style></legend>