is there any way to adopt a memory resource for a vector?

  allocator, boost, c++, c++14, c++17

I have started using pmr::allocators in my project, and I have seen a lot of performance boost and advantage from using them. I use allocator very similar to what I show int the simple example below:

#include <array>
#include <boost/container/flat_map.hpp>
#include <cassert>
#include <iostream>
#include <memory_resource>
#include <string>
#include <vector>

struct MessageBody {
  using map_t = boost::container::flat_map<
      char, char, std::less<char>,
      std::pmr::polymorphic_allocator<std::pair<char, char>>>;

  using vector_t = std::vector<char, std::pmr::polymorphic_allocator<char>>;

  MessageBody(std::pmr::memory_resource& mem_v,
              std::pmr::memory_resource& mem_m)
      : vec_(0, &mem_v), map_(&mem_m) {}

  vector_t vec_;
  map_t map_;

int main() {
  std::array<char, 1000> buffer;
  std::pmr::monotonic_buffer_resource vec_mem(, 500,
  std::pmr::monotonic_buffer_resource map_mem( + 500, 500,
    MessageBody message(vec_mem, map_mem);
    assert(message.vec_.size() == 1);

    MessageBody message(vec_mem, map_mem);
    assert(message.vec_.size() == 1);   /// I want to adopt the previous class for a new class.

my question is if there is any way I can adopt the memory_resources for another class without having the repopulate entire data inside vector and map.

the only way I can think of doing that (and I know it is a terrible idea), is to implement a new class that inherits from std::vector, that class has an adopt data method that set the size inside the vector to the size of the previously used vector without modifying the buffer.

here is a link to godbolt for the example.

Source: Windows Questions C++