问题描述
我很惊讶没有看到标准或其他任何人提供适配器/包装器,以使标准分配器看起来像std::memory_resource
。我想念什么吗?只是
template <typename Byteallocator>
class AllocatorMemoryResource : public std::memory_resource {
Byteallocator m_allocator;
void* do_allocate(std::size_t bytes,std::size_t alignment) override {
// Do something to align the storage???
return static_cast<void*>(m_allocator.allocate(bytes));
}
void do_deallocate(void* p,std::size_t bytes,std::size_t alignment) override {
m_allocator.deallocate(p,bytes);
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
return this == &other; // Right?
}
};
std
或Boost或其他地方提供了这种包装吗?我没找到。我正在使用tbb::scalable_allocator
,并且希望能够与std::pmr::vector<T>
一起使用。
解决方法
目前,我认为它仍处于试验阶段:https://en.cppreference.com/w/cpp/experimental/resource_adaptor。
从最简单的意义上讲,您的实现似乎是正确的。您可以通过动态转换并返回
的结果来使其更高级 this->m_allocator == cast_other->m_allocator