Given a non-stateless custom allocator A
(say, arena allocator, binded to some contiguous memory chunk of runtime-known size) and class S
, containing a fileds of AllocatorAwareContainer types:
struct S
{
A() = default;
// another c-tors
std::vector< T > v;
std::shared_ptr< U > s;
};
I want to use A
for a subset (or even for all) AllocatorAwareContainer fields of class S
. It is clear, that I should to provide one another template parameter for class S
and change types of all the interesting data members correspondingly as follows:
template< typename A = std::allocator< void > >
struct S
{
// c-tors
template< typename X >
using allocator = typename std::allocator_traits< A >::template rebind< X >::other;
std::vector< T, allocator< T > > v;
std::shared_ptr< U, allocator< U > > s;
};
What are changes in present constructors and additional constructors (assume A
is not DefaultConstructible and any other possible restrictions may holds) should I made?
Should I store allocator A
in additional field of class S
?
What is a general tips to use custom allocators for classes?
std::scoped_allocator_adaptor
make sense here? – Auspex