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

前端 未结 3 969
我在风中等你
我在风中等你 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:21

    make_unique hides from you "raw" pointer, what's usually a good thing - it's less error prone. make_shared may improve memory allocation for shared_ptr instances. Normally when you use shared_ptr constructor it will allocate memory twice, first for instance of X and second for it's internal data (e.g. reference counter). make_shared enables optimization - it will create single, internal structure comprising both X and reference counter, hence it will perform single memory allocation. And same as before it hides raw pointers.

提交回复
热议问题