Are there any downsides with using make_shared to create a shared_ptr

前端 未结 5 861
栀梦
栀梦 2020-12-12 20:33

Are there any downsides with using make_shared() instead of using shared_ptr(new T).

Boost documentation states

5条回答
  •  再見小時候
    2020-12-12 21:11

    With make shared you can not specify how allocation and deallocation of the held object will be done.

    When that is desired, use std::allocate_shared instead:

    std::vector> avec; 
    std::allocator aAllocator;
    avec.push_back(std::allocate_shared(aAllocator,"hi there!"));
    

    Note that the vector does not need to be informed about the allocator!

    For making a custom allocator, have a look here https://stackoverflow.com/a/542339/1149664

提交回复
热议问题