boost :: variant类型的自定义线程局部分配器

时间:2017-03-27 23:25:01

标签: c++ boost-variant memory-pool

我已经实现了一个基于c ++标准分配器接口的内存池。

template <typename T, size_t Cap = 8>
class SingularAllocator {

  // member declarations ...

  public:

    inline SingularAllocator();                             // Constructor.
    inline ~SingularAllocator();                            // Destructor.

    inline T* allocate(const size_t n=1) ;                  // Allocate an entry of type T.
    inline void deallocate(T*, const size_t n=1);           // Deallocate an entry of type T.

    template <typename... ArgsT>    
    inline void construct(T*, ArgsT&&...);                  // Construct an item.
    inline void destroy(T*);                                // Destroy an item.

  private:

    thread_local Mempool _slot;                                          

    // member implementations ...
}

为简单起见,上面的代码段只显示了界面。我们的想法是使用thread_local关键字为每个线程启用内存池。我的问题是如何使用它来从我的池中创建每个boost::variant对象?对于像std::vector这样的STL,它非常简单,因为模板参数需要额外的分配器字段。但是,我在boost::variant中没有看到这一点。

boost::variant<int, string, OtherClass> MyStuf; // How to create this variant from my memory pool (per-thread).

0 个答案:

没有答案
相关问题