我想要一个用这个 API 管理原始内存的小类
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
保存字节数)。什么用作删除器?还是有更好的方法来实现这一切?