boostorg / multi_index

Boost.org multi_index module
http://boost.org/libs/multi_index
45 stars 59 forks source link

support allocator awareness #30

Closed star-e closed 4 years ago

star-e commented 4 years ago

Currently, we cannot use multi_index with c++17 std::pmr::polymorphic_allocator, because multi_index didn't provide copy/move constructors with an extra allocator parameter.

For example, as shown in https://www.boost.org/doc/libs/1_72_0/libs/multi_index/doc/reference/multi_index_container.html#constructors), multi_index only provided the following copy/move constructors multi_index_container(const multi_index_container<Value,IndexSpecifierList,Allocator>& x); multi_index_container(multi_index_container<Value,IndexSpecifierList,Allocator>&& x);

The following two constructors are not provided. multi_index_container(const multi_index_container<Value,IndexSpecifierList,Allocator>& x, const allocator_type& alloc); multi_index_container(multi_index_container<Value,IndexSpecifierList,Allocator>&& x, const allocator_type& alloc);

According to https://en.cppreference.com/w/cpp/named_req/AllocatorAwareContainer, these two special constructors are required when used with nested containers, such as std::pmr::vector<boost::multi_index::multi_index_container<T, ..., std::pmr::polymorphic_allocator<T>>>.

Could you please add these special constructors?

I have tested the following code in boost 1.71. It seems working, but I am not very sure about it. ` multi_index_container( const multi_index_container<Value,IndexSpecifierList,Allocator>& x, const Allocator& al): bfm_allocator(al), bfm_header(), super(x), node_count(0) { copy_map_type map(bfm_allocator::member,x.size(),x.header(),header()); for(const_iterator it=x.begin(),it_end=x.end();it!=it_end;++it){ map.clone(it.getnode()); } super::copy(x,map); map.release(); node_count=x.size(); /* Not until this point are the indices required to be consistent,

Thanks a lot!

joaquintides commented 4 years ago

Yes, Boost.MultiIndex is not AllocatorAware, I can add this to the lib backlog.

As for the allocator-aware ctors you sketched:

Besides, as you know, there's more to being AllocatorAware than providing these two ctors (like handling propagate_on_container_* traits).

star-e commented 4 years ago

Yes, making container allocator aware is not an easy job. Thank you for your suggestion and I really appreciate your works!

joaquintides commented 4 years ago

Hi, allocator awareness has been added in the sequence of commits:

You can download the current develop branch code at:

https://github.com/boostorg/multi_index/tree/81dbe86f25c23e075ac31702b2f4d1239d5f1a10

Care to test locally and report any problems you find?

Best regards,

star-e commented 4 years ago

Sure!I will test it with my codebase.

Thanks a lot!

Best regards,

star-e commented 4 years ago

I have tested the code, it works as expected. Great job! If I encounter any problem in the future, I will post it here asap.

Best regards,