c++ - boost make_shared 的用例

标签 c++

我正在浏览一个 cpp 代码并有以下问题(几乎没有接触过 boost 库)

bool xxxx::calcYYY ()
    {
        bool retStatus = false;
        boost::shared_ptr<DblMatrix> price  = boost::make_shared<DblMatrix>(xxx, xxx); 

.....
            retStatus = true;
        }
        return retStatus;
    }

为什么局部作用域指针被实例化为共享?
在高性能代码中,必须有额外的开销来维护引用计数。
在这里正确执行此操作的替代方案是什么?

最佳答案

Why are local scoped pointers instantiated as shared?

因为它们指向可以共享的对象。例如,它们可以传递给可以延长它们引用的对象的生命周期的函数。

There must be an additional overhead to maintain reference counting,in a high performing code.

可能吧。编译器也有可能对其进行优化。有很多技巧,例如通过 const 引用传递指针以避免必须更改引用计数,在可能的情况下使用 std::move 等等可以使额外的开销可以忽略不计许多用例。但是,据推测,它被使用是因为它有一些好处。仅凭您显示的代码我们无法判断。

What is the boost alternative to do this correctly here?

没有显示不正确的内容。

关于c++ - boost make_shared 的用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46804567/

相关文章:

c++ - if(a == b) 不适用于 for 循环中的 double

c++ - C++ 中奇怪的 null ref

c++ - MATLAB mex API 与 MATLAB 引擎 API 之间的区别

c++ - C++目录中的文件

c++ - 编写此代码的更有效方法是什么?

c++ - Windows 中的 C++ 何时需要 "extern C "?

c++ - .open 在 C++ 中使用 argc 和 argv 文件

c++ - 如何使用 union 成员的大小计算编译时值?

C++ vector 不像数组

c++ - 我的 C++ Floyd 的全对最短路径程序有什么问题?