std::scoped_allocator_adaptor

Hi, I'm having a bit of trouble understanding how to correctly use std::scoped_allocator_adaptor

For example I created my own allocator that acts like std::allocator
1
2
3
4
5
6
template<typename T> Pool_alloc
{
        //...
        Pool_alloc() = default;
        //...
};


What if now I want to create
1. vector of string's so that they both would use Pool_alloc
2. vector of string's so that only vector would use Pool_alloc
3. vector of string's so that vector would use its default allocator but string would use Pool_alloc

I read that if I create a container with a
std::scoped_allocator_adaptor<typename OuterA, typename... InnerA>
OuterA is used to allocate this container's elements but if container's elements are themselves a containers than InnerA would be used for them.

I will show you how I see the solutions to these 3 questions but the problem is they will be different from solution in my book but at least they make sense to me. Maybe someone might point out my mistakes afterwards.

1. vector of string's so that they both would use Pool_alloc


1
2
3
4
using pool_string = std::basic_string<char, std::char_traits<char>, Pool_alloc<char>>;
std::vector<pool_string, std::scoped_allocator_adaptor<Pool_alloc<pool_string>, Pool_alloc<char>>> vec;
//OuterA   : Pool_alloc<pool_string>  :  for vectors elements that are pool_string
//InnerA   : Pool_alloc<char>         :  for pool_strings elements 


2. vector of string's so that only vector would use Pool_alloc


As far as I see it I dont even need scoped allocator adaptor here
std::vector<std::string, Pool_alloc<std::string>> vec;


3. vector of string's so that vector would use its default allocator but string would use Pool_alloc


1
2
3
4
using pool_string = std::basic_string<char, std::char_traits<char>, Pool_alloc<char>>;

//as I dont know how to specify that vector should use default allocator I will just use std::allocator ar OuterA
std::vector<pool_string, std::scoped_allocator_adaptor<std::allocator<pool_string>, Pool_alloc<char>>>


Are my versions wrong in some way? Only the 2nd version was exactly like in my book, 1st and 3rd are different.
Last edited on
Topic archived. No new replies allowed.