为什么最好使用std :: make_ *而不是构造函数? [英] Why is it better to use std::make_* instead of the constructor?
问题描述
STL中有一些函数以 make _
前缀开头,如 std :: make_pair
, std :: make_shared
, std :: make_unique
等为什么是更好的做法,使用它们,而不是简单地使用构造函数?
There are some functions in the STL which start with the make_
prefix like std::make_pair
, std::make_shared
, std::make_unique
etc. Why is it a better practice to use them instead of simply using the constructor ?
auto pair2 = std::pair< int, double >( 1, 2.0 );
auto pair3 = std::make_pair( 1, 2.0 );
std::shared_ptr< int > pointer1 = std::shared_ptr< int >( new int( 10 ) );
std::shared_ptr< int > pointer2 = std::make_shared< int >( 10 );
- 我只是看到这些函数使代码稍短一点,
- I just see that these functions make the code a little shorter, but is that all ?
- Are there any other advantages ?
- Are these functions safer to use ?
这些功能是否更安全使用?
推荐答案
除了启用参数扣除的优点(如其他答案中已经提到的),还有一些其他好处。
Aside from the benefit of enabling argument deduction (as already mentioned in other answers), there are also some other benefits.
std :: make_pair< T1,T2>
注意不是 c> std :: pair< T1,T2> 。如果你使用 std :: ref
传递一个值,那么返回的对不会存储 std :: reference_wrapper
,它将存储一个引用。
std::make_pair<T1, T2>
takes care to not simply return std::pair<T1, T2>
. If you pass in a value using std::ref
, then the returned pair won't store a std::reference_wrapper
, it will store a reference.
std :: make_shared
可以组合分配。 shared_ptr
需要一些地方来存放诸如refcount,弱指针列表等无法存储在 shared_ptr
直。
std::make_shared
can combine allocations. shared_ptr
needs some place to hold things like the refcount, weak pointer list, etc. that cannot be stored in the shared_ptr
directly. These can be combined with the object being created, in one slightly larger block rather than in two separate blocks.
std :: make_shared
和 std :: make_unique
都确保没有对象被抛出,如果抛出异常。如果你调用一个函数 f(std :: shared_ptr< int>(new int),std :: shared_ptr< int>(new int))
编译器首先分配两个 int
对象,然后构造两个 shared_ptr< int>
对象。如果第二分配失败,并且没有设置 shared_ptr< int>
对象以释放内存, 。 std :: make_shared
和 std :: make_unique
组合 int
并在函数调用中构造 std :: shared_ptr< int>
,另一个分配 int
和另一个函数调用中的 std :: shared_ptr< int>
的其他结构。函数调用不能重叠,所以如果第二次分配失败,已经有一个共享指针将被销毁,撤消第一次分配。
std::make_shared
and std::make_unique
both make sure that no object gets left behind if exceptions are thrown. If you call a function as f(std::shared_ptr<int>(new int), std::shared_ptr<int>(new int))
, then it's possible the compiler first allocates two int
objects, and then constructs two shared_ptr<int>
objects. If the second allocation fails, and no shared_ptr<int>
object is set up yet to release the memory on destruction, then you have a memory leak. std::make_shared
and std::make_unique
combine the allocation of int
and the construction of std::shared_ptr<int>
in a function call, and the other allocation of int
and the other construction of std::shared_ptr<int>
in another function call. Function calls cannot overlap, so if the second allocation fails, there is already a shared pointer that will be destroyed, undoing the first allocation as well.
这篇关于为什么最好使用std :: make_ *而不是构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!