What is the purpose and usage of `memory_resource`?

A polymorphic_allocator is intended to let you have an allocator whose behavior is dynamically determined at runtime. The only way to create a polymorphic_allocator is: Default constructed, in which case it uses std::pmr::get_default_resource() return value, which is a memory_resource*. Pass it a memory_resource*. copy from another polymorphic_allocator. So the point of customization for a polymorphic_allocator … Read more

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

At this point no. Allocators in C++ currently are much easier than they used to be. They provide both pmr (polymorphic) and classic allocator support. More importantly, pmr based allocation has not been in heavy use for years. Any weaknesses may still come to light. Fast pool based allocators, or even fixed buffer ones or … Read more

Why is allocator::rebind necessary when we have template template parameters?

A quoted text from Foundations of Algorithms in C++11, Volume 1, chap 4, p. 35 : template <typename T> struct allocator { template <typename U> using rebind = allocator<U>; }; sample usage : allocator<int>::rebind<char> x; In The C++ Programming Language, 4th edition, section 34.4.1, p. 998, commenting the ‘classical’ rebind member in default allocator class … Read more

Questions about Hinnant’s stack allocator

I’ve been using Howard Hinnant’s stack allocator and it works like a charm, but some details of the implementation are a little unclear to me. Glad it’s been working for you. 1. Why are global operators new and delete used? The allocate() and deallocate() member functions use ::operator new and ::operator delete respectively. Similarly, the … Read more

What is the purpose of std::scoped_allocator_adaptor?

If you want a container of strings and want to use the same allocator for the container and its elements (so they are all allocated in the same arena, as TemplateRex describes) then you can do that manually: template<typename T> using Allocator = SomeFancyAllocator<T>; using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>; using Vector = std::vector<String, Allocator<String>>; … Read more

Stack-buffer based STL allocator?

It’s definitely possible to create a fully C++11/C++14 conforming stack allocator*. But you need to consider some of the ramifications about the implementation and the semantics of stack allocation and how they interact with standard containers. Here’s a fully C++11/C++14 conforming stack allocator (also hosted on my github): #include <functional> #include <memory> template <class T, … Read more