0

有一个无状态内存池分配器类:

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 */
    /* POINT OF INTERREST HERE: */
    static auto & get_pool( void ) noexcept;
};

背后的逻辑是get_pool()成员函数的特殊化,它旨在返回定义类型的特定内存池,应在其中分配 T 的实例,例如:

class sample { ... };

在 .cpp 文件中:

memory_pool<sample, 10> sample_storage;  // memory pool capable of holding up to 10 instances of 'sample'

最后是 .cpp 文件中的 get_pool() 函数模板的特化:

template<>
auto & pool_allocator<sample>::get_pool( void ) noexcept {
    return sample_storage; // return the memory_pool instance defined above
}

问题是这样的模板特化仅在.cpp编译单元中可用,并且阻止auto get_pool()在其他编译单元中的使用(auto不能推断占位符的类型,因为get_pool()函数模板特化的主体不可用)

auto因此,我想以某种方式摆脱get_pool().

我面临的问题主要memory_pool是分配器本身不知道的大小。无论如何,memory_pool 也是我的实现,所以我可以进行任何需要的采用(例如,进一步using的声明或其他任何需要)。只是它的骨架:

template<typename T, size_t CAPACITY>
class memory_pool {
public:
    using element_type = T;
    using pointer = element_type *;

    constexpr size_t capacity( void ) noexcept {
        return CAPACITY;
    }
...
};
4

1 回答 1

0

这是我使用的解决方案 - 实现包含池大小信息的特征类:

template<typename T>
class memory_pool {
public:
    using traits = memory_pool_traits<T>;
    using element_type = typename traits::element_type;
    using pointer = element_type *;

    static constexpr size_t capacity { traits::capacity };
...
};

分配器:

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:
    static memory_pool<T> & get_pool( void ) noexcept;
};

对于任何特定类型,都应有特征类:

// Primary template
template<typename T> struct memory_pool_traits;

让有我想定义的样本memory_pool

class sample { ... };

...除此之外,相应的设置 - 特征 - memory_pool

template<>
struct memory_pool_traits<sample> {
    using element_type = sample;
    static constexpr size_t capacity { 10 };
};

.cpp文件中有池本身的定义和get_pool()函数:

memory_pool<sample> sample_storage;

template<>
memory_pool<sample> & pool_allocator<sample>::get_pool( void ) noexcept {
    return sample_storage;
}
于 2021-07-07T08:18:27.417 回答