std::unique_ptr and custom allocator deleter

后端 未结 2 1888
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 13:11

I am trying to use std::unique_ptr with custom memory allocators. Basically, I have custom allocators that are subclasses of IAllocator,

2条回答
  •  一整个雨季
    2021-01-04 13:43

    T* doesn't contain such information, neither unique_ptr knows about the size of the array (since it uses directly a delete [] as you stated). You could let the T be a unique_ptr to manage the destruction automatically but this could not be possible if the whole contiguous T* is managed by a memory allocator (and not a single T* object). Eg:

    unique_ptr[]> data;
    data.reset(new unique_ptr[50]);
    data[0].reset(new Foo());
    

提交回复
热议问题