I want to have a pmr container of other pmr types. However, I do not want the same allocator to be used for the inner types, the reason for that is that in my real code they will be using different memory resources with different allocation strategies.
For example:
#include <vector>
#include <string>
#include <memory_resource>
int main()
{
std::pmr::polymorphic_allocator alloc_1;
std::pmr::polymorphic_allocator alloc_2;
std::pmr::vector<std::pmr::string> vec(alloc_1);
vec.emplace_back("test1 this is a long string", alloc_2);
vec.emplace_back("test2 this is a long string", alloc_2);
return 0;
}
The problem is that, if I understand correctly, the polymorphic allocator will automatically propagate the allocator to the constructor of the container elements. So when I try to do the above, I get the following compilation error (link to onlinegdb):
/usr/include/c++/9/memory:212:22: error: static assertion failed: construction with an allocator must be possible if uses_allocator is true
212 | static_assert(is_constructible_v<_Tp, _Args..., const _Alloc&>,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I believe what's happening is that it's essentially calling the std::pmr::string constructor with an extra argument like so: ("text", alloc_2, <propagated allocator>).
In case it matters, I'm using C++20.
Is there any way to tell the pmr allocator to not propagate itself to the constructors of the elements of the container? Or did I completely misunderstand the error?