4

I was tring to understand boost::container::allocator_traits when I encountered boost::container::allocator_traits::is_partially_propagable.

I can't find any other document about it online, and I can understand all the other members of boost::container::allocator_traits except is_partially_propagable and storage_is_unpropagable.

Edit:

And, how they're implemented and how to use them when writing a container?

JiaHao Xu
  • 2,452
  • 16
  • 31

1 Answers1

5

It (is_partially_propagable) means that the allocator uses internal storage (it has state), and not all memory allocated by it can be deallocated by another allocator, even if both compare equal. (Allocators of the same type are supposed to always compare equal and be interchangeable)

What storage_is_unpropagable does is take a pointer to allocated memory and return true if this is memory that is not propagable. (Since not all memory has to be unpropagable)

This allocator (small_vector_allocator) is used as a space optimization in small_vector.

https://www.boost.org/doc/libs/1_65_0/boost/container/small_vector.hpp

imreal
  • 10,178
  • 2
  • 32
  • 48
  • Can you explain how they are implemented? – JiaHao Xu May 12 '18 at 02:13
  • Also, can you talk more about how to use them when writing a container? – JiaHao Xu May 12 '18 at 02:28
  • @JiaHaoXu I am not sure about this particular allocator. But I have used other allocators that have an internal buffer (i.e. a byte array member with the size of some template parameter) and return pointers to this internal buffer when allocating until it is depleted, at which point they simply forward the `allocate()` call to the default allocator (i.e. `new`). These allocators are good for containers that need many small allocations (and no deallocations). – imreal May 13 '18 at 00:44
  • About ```Allocators of the same type are supposed to always compare equal and be interchangeable```, there's no restriction on equality of objects of same type that in boost::container::allocator_traits. – JiaHao Xu May 13 '18 at 06:36
  • @JiaHaoXu The restriction is in the C++ Standard, and this exception to the rule is boost specific. – imreal May 14 '18 at 14:55
  • It's just for ```std::allocator```, there's no restriction on custom allocator. So, things are more complicated. – JiaHao Xu May 15 '18 at 05:45