• <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 ?

      Should we pass a shared_ptr by reference or by value?(我们应该通过引用还是通过值传递 shared_ptr ?)

      <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 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                <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>

                        <tbody id='ZYNHf'></tbody>

                      <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>