原始內存我想有它管理原始內存與此API管理與C++的std ::的unique_ptr
template<class allocator = std::allocator<char> >
class raw_memory
{
static_assert(std::is_same<char, typename allocator::value_type>::value,
"raw_memory: allocator must deal in char");
public:
raw_memory() = default;
raw_memory(raw_memory&&) = default;
raw_memory&operator=(raw_memory&&) = default;
explicit raw_memory(size_t, allocator const& = allocator());
~raw_memory(); // deletes any memory
char*get(); // returns pter to (begin of) memory
void resize(size_t); // re-allocates if necessary, may delete old data
size_t size() const; // returns number of bytes currently hold
raw_memory(raw_memory const&) = delete;
raw_memory&operator=(raw_memory const&) = delete;
raw_memory(raw_memory&) = delete;
raw_memory&operator=(raw_memory&) = delete;
};
模板參數allocator
允許不同的內存對齊選項有點類。
我正在考慮使用std::unique_ptr<char, Deleter>
作爲成員(或基數)(加上保存字節數的size_t
)。作爲Deleter使用什麼?還是有更好的方法來實現這一切?
將自定義刪除器作爲raw_memory對象的一部分? –
如果你不需要分配器支持,你可以使用'std :: unique_ptr p(std :: malloc(n),std :: free);'。 –
我想你的意思是'typename allocator :: value_type'? – juanchopanza