In my project, I'm trying to use the std::pmr
allocator and monotonic_buffer_resource
. I'm using vector in various classes, and I need to use the same resource in all of them. I created a separate free function that returns the resource pointer and used it while declaring the vectors. Here is the code
#include <memory_resource>
#include <vector>
#include <array>
std::pmr::monotonic_buffer_resource *getMemoryResource()
{
static std::array<std::byte, 100000> buffer;
static std::pmr::monotonic_buffer_resource resource{buffer.data(), buffer.size()};
return &resource;
}
struct JsonData
{
std::pmr::vector<int> data{getMemoryResource()};
};
struct XMLData
{
std::pmr::vector<char> data{getMemoryResource()};
};
Is there anything I can do to improve the code, especially getMemoryResource
function?