std::unique_ptr and custom allocator deleter

后端 未结 2 1899
爱一瞬间的悲伤
爱一瞬间的悲伤 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:47

    Yes, there most certainly is a better way:
    Use a maker-function.

    template std::unique_ptr
    my_maker(size_t count, A&& allocator) {
        return {somePtr(allocator.AllocArray(count), MyArrDeleter(allocator, count)};
    }
    
    auto p = my_maker(42, allocator);
    

提交回复
热议问题