Why is it better to use std::make_* instead of the constructor?

前端 未结 3 971
我在风中等你
我在风中等你 2021-02-18 16:56

There are some functions in the STL which start with the make_ prefix like std::make_pair, std::make_shared, std::make_unique

3条回答
  •  感动是毒
    2021-02-18 17:45

    Aside from the benefit of enabling argument deduction (as already mentioned in other answers), there are also some other benefits.

    std::make_pair takes care to not simply return std::pair. 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 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 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(new int), std::shared_ptr(new int)), then it's possible the compiler first allocates two int objects, and then constructs two shared_ptr objects. If the second allocation fails, and no shared_ptr 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 in a function call, and the other allocation of int and the other construction of std::shared_ptr 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.

提交回复
热议问题