新的 C++ 代码应该使用内存资源而不是分配器吗?

Should new C++ code use memory resources instead of allocators?

C++17 将为我们带来 std::pmr::memory_resource which is a clean interface for allocating and deallocating memory. Unlike the Allocator concept, it does just that and nothing more. There will also be std::pmr::polymorphic_allocator,它将内存资源包装到经典分配器中,因此它可以与现有容器一起使用。

如果我要编写一个针对 C++17 及更高版本的新容器(或其他需要大量内存的)类型,我应该继续针对 Allocator 概念进行编程还是而是直接使用更新更清晰的抽象?

到目前为止,我的想法是这样的。

继续使用分配器的原因:

开始使用内存资源而不是分配器的原因:

关于如何有效使用新库功能是否已经存在任何建议?

此时没有

C++ 中的分配器现在比以前容易得多。

它们同时提供 pmr(多态)和经典分配器支持。

更重要的是,基于 pmr 的分配多年来一直没有大量使用。任何弱点都可能会暴露出来。

基于快速池的分配器,甚至是固定缓冲区分配器或 sbo(小缓冲区优化)扩展,可能会注意到虚拟化开销。