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:

  1. Default constructed, in which case it uses std::pmr::get_default_resource() return value, which is a memory_resource*.

  2. Pass it a memory_resource*.

  3. copy from another polymorphic_allocator.

So the point of customization for a polymorphic_allocator is creating a class that inherits from memory_resource and implementing its methods, or using one of the pre-declared memory_resources that are defined in std::pmr: (un)synchronized_pool_resource and monotonic_buffer_resource as types, or std::pmr::new_delete_resource() / std::pmr::null_memory_resource().

Suppose you want a your memory to be allocated using a strategy different than the 5 provided in std::pmr. Then you make a class that inherits from std::pmr::memory_resource, and pass it to a container which uses a polymorphic_allocator.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)