There is an allocator:
template<typename T>
class pool_allocator {
public:
using value_type = T;
using pointer = value_type *;
/* Default constructor */
constexpr pool_allocator( void ) noexcept = default;
/* Converting constructor used for rebinding */
template<typename U>
constexpr pool_allocator( const pool_allocator<U> & ) noexcept {}
[[nodiscard]] pointer allocate( size_t n, [[maybe_unused]] const pointer hint = nullptr ) const noexcept {
return get_pool().allocate( n );
}
void deallocate( pointer ptr, size_t n ) const noexcept {
get_pool().deallocate( ptr, n );
}
private:
/* Must be defined in particular .cpp files */
static auto & get_pool( void ) noexcept;
};
which expects the definition of get_pool()
for particular types in .cpp files. The pool_allocator
allocates the instance within the memory pool.
struct cpu { ... };
To define the pool itself - the area in memory where the instances are located may look like:
memory_pool<cpu, 4> cpu_storage;
template<>
auto & pool_allocator<cpu>::get_pool( void ) noexcept {
return cpu_storage;
}
While attempting to use that like:
class process {
unique_ptr<cpu> m_cpu { nullptr };
};
I face the trouble GCC reports:
error: use of 'static auto& ReVolta::pool_allocator<T>::get_pool() [with T = cpu]' before deduction of 'auto'
get_pool().deallocate( ptr, n ); // line of code within the pool_allocator<T>::deallocate(...) implemnetation
May I kindly ask for help?