lundi 4 mai 2015

Making boost::fast_pool_allocator work with variadic templates (emplace)

I'm trying to use boost::fast_pool_allocator as the allocator for a std::list, but it fails to find the overload for construct() that uses variadic templates.

#include <list>
#include <utility>

#include <boost/pool/pool_alloc.hpp>

int main()
{
    typedef std::pair<int, int> Pair;
    std::list<Pair, boost::fast_pool_allocator<Pair>> list;

    list.emplace(list.begin(), 1, 2);
}

This fails to compile with the following error (shortened):

stl_list.h:514:8: error: no matching function for call to ‘boost::fast_pool_allocator<blah>::construct(std::list<bleh>::_Node*&, int, int)'

Looking at the header file, it seems boost::fast_pool_allocator only has the pre-C++11 version of construct() (a pointer and a const_reference).

Note that defining the list as std::list<Pair> (i.e. using the default allocator) works fine.

Is there a workaround for this? Any adaptor or some way of defining the allocator traits? I'm new to allocators so this is kind of a dark land for me.

I can make it work with

list.emplace(list.begin(), Pair(1, 2));

but 1st) the actual class I'm using in production is much more complex than the Pair I used for the example and performance is paramount (so I could really use in-place construction), and 2nd) ideally, I'd like to have a drop-in replacement for std::allocator, so I could measure the performance difference with a one-line change.

I'm compiling in Cygwin with g++ 4.9.2 and boost 1.58.0, and I have the same problem in a linux environment (RHEL5.5) with g++ 4.8.3 and boost 1.55.0.

Aucun commentaire:

Enregistrer un commentaire