I rewrote my library based on polymorphic memory resources. I think they're amazing, however there are edge cases that are not really covered by the standard.
For example just now I have the following problem: A class type holds an unique_ptr to a pmr-allocated resource. Now, how to deal with this in the copy constructor? The underlying pmr-resource (a pmr::vector) will have to be constructed using the pmr-resource (which is passed down automatically using the pmr allocators I think). So I'm using the new_object() and delete_object() facilities of the allocator to feed my unique ptr. But it fails because 1) allocators are not delete-functors (don't provide operator() overload for delete) and/or 2) I can't use a function directly because those functions are non-static. How do I resolve this?
#include <memory_resource>
#include <memory>
struct profile
{
using allocator_type = std::pmr::polymorphic_allocator<std::byte>;
profile(allocator_type allocator = {})
: allocator_{allocator}
{}
profile(const profile& other, allocator_type allocator = {})
: allocator_{ allocator }
{
// Deep copy vector (how to do this?)
auto f = std::pmr::polymorphic_allocator<std::byte>::delete_object;
vals_ = std::unique_ptr<profile, decltype(&f)>{ allocator_.new_object<profile>(), f };
}
allocator_type get_allocator() {
return allocator_;
}
allocator_type allocator_;
std::unique_ptr<std::pmr::vector<double>> vals_;
};
int main()
{
profile p;
profile o{p};
}
Error:
<source>:15:62: error: call to non-static member function without an object argument
auto f = std::pmr::polymorphic_allocator<std::byte>::delete_object;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
1 error generated.