我已经设置好了,类似于:
有一个类似于vector的类(它使用std :: vector实现)。
它包含指向int的指针。
我正在使用自己的自定义分配器。
向量不会创建元素,但是会破坏元素。
要销毁它,需要调用非静态方法Allocator::deallocate(int *p)
。
如果通过手动实时管理进行操作,则可以手动致电Allocator::deallocate(int *p)
。这有效,但RAII无效。
或者,我可以将std::unique_ptr
与自定义删除器一起使用。但是,如果这样做,则数组的大小将变成两倍,因为每个std::unique_ptr
必须包含指向分配器的指针。
在不增加向量大小的情况下,有什么方法可以做到吗?
请注意,我不想对课程进行模板化。
这是我出现的最好的RAII代码。
#include <functional>
#include <cstdlib>
#include <memory>
struct MallocAllocator{
template<class T>
static T *allocate(size_t size = sizeof(T) ) noexcept{
return reinterpret_cast<T *>( malloc(size) );
}
// this is deliberately not static method
void deallocate(void *p) noexcept{
return ::free(p);
}
// this is deliberately not static method
auto getDeallocate() noexcept{
return [this](void *p){
deallocate(p);
};
}
};
struct S{
std::function<void(void *)> fn;
S(std::function<void(void *)> fn) : fn(fn){}
auto operator()() const{
auto f = [this](void *p){
fn(p);
};
return std::unique_ptr<int,decltype(f)>{ (int *) malloc(sizeof(int)),f };
}
};
int main(){
MallocAllocator m;
S s{ m.getDeallocate() };
auto x = s();
printf("%zu\n",sizeof(x));
}