C++ boost::make_shared 现在过时了吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1712701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:58:19  来源:igfitidea点击:

Is boost::make_shared obsolete now?

c++boost

提问by Steve

Is boost::make_sharedobsolete now? Haven't found its definition in 1.35.

现在boost::make_shared过时了吗?在 1.35 中没有找到它的定义。

采纳答案by Ryan Cook

Its in the 1.4 docs: http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/make_shared.html

它在 1.4 文档中:http: //www.boost.org/doc/libs/1_40_0/libs/smart_ptr/make_shared.html

It appears to have been added in version 1.39

它似乎已在 1.39 版中添加

回答by nosid

std::make_sharedis also available in C++11. Please note that make_sharedis more than just a convenience function. Take a look at the following code fragment:

std::make_shared在 C++11 中也可用。请注意,这make_shared不仅仅是一个方便的功能。看看下面的代码片段:

make_shared<foobar>(1, 2);
shared_ptr<foobar>(new foobar(1, 2));

Both statements create a foobarobject and construct a shared_ptr. However, the former avoids a memory allocation for the shared counter, because a single memory chunk will be used for the counter and the foobar object. This is not possible with the second statement, because the memory for foobaris allocated before the shared_ptris constructed.

这两个语句都创建了一个foobar对象并构造了一个shared_ptr. 然而,前者避免了为共享计数器分配内存,因为单个内存块将用于计数器和 foobar 对象。这在第二条语句中是不可能的,因为在构造foobar之前分配了内存shared_ptr

What I want to say: No, make_sharedis not obsolete, because it provides a very useful optimization.

我想说的是:不,make_shared并没有过时,因为它提供了非常有用的优化。

回答by Alex Z

Did a bit of research today, and it seems that make_shared actually was added to 1.36.0 (in 1.35.0 there is no such header), but the interesting thing is that there is no single mention in What's new about this change - at least I could not find it

今天做了一些研究,似乎make_shared实际上是添加到1.36.0中的(在1.35.0中没有这样的标题),但有趣的是,在What's new about this change - at至少我找不到它