#cpp17
7 posts tagged #cpp17.
Making your own type allocator-aware needs a second constructor
Put a pmr::string inside your own struct and it will allocate from the default resource unless the struct is allocator-aware: an allocator_type, an optional allocator argument in every constructor, and forwarding to members. This is uses-allocator construction, shown reaching a string nested in a pmr::vector. Episode 7 of the pmr series.
The pmr crash is the buffer that died first
The most common pmr crash rarely comes from a custom resource; it is lifetime. The container deallocates back into its resource on destruction, so the resource must outlive it. A live demo shows the destruction order and the declaration-order rule that keeps it correct. Episode 6 of the pmr series.
Move-assigning pmr containers across arenas deep-copies
Move assignment should be an O(1) pointer steal. For pmr containers with different resources it cannot steal the buffer, so it allocates in the destination and copies every element, turning a move into an O(n) copy the type system cannot warn about. Episode 5 of the pmr series.
Copying a pmr::vector silently drops its allocator
A pmr::vector looks like a value type, but copy construction does not copy its allocator: select_on_container_copy_construction returns the default resource, so a copied container silently allocates from the global heap. The propagation traits make the allocator sticky. Episode 4 of the pmr series.
A pmr::vector on a stack buffer never calls new
The HFT and game-loop promise of pmr, made testable: override operator new to count heap allocations, then fill a pmr::vector from a monotonic_buffer_resource backed by a stack buffer with null_memory_resource upstream. Ten thousand pushes, zero heap calls. Episode 2 of the pmr series.
std::pmr is one abstract class with three functions
The whole std::pmr framework is one abstract class, std::pmr::memory_resource, with three functions to override: allocate, deallocate, is_equal. A dozen-line logging resource shows a pmr::vector's allocations, then a monotonic_buffer_resource placed in front of it collapses them to zero. Episode 1 of the pmr series.
Boost and Abseil start deleting their C++14 shims
Two release notes nine days apart point the same way. Boost 1.92 tells Heap and Lockfree users that C++14 support ends here, and Abseil's August LTS deprecates absl::void_t in favour of the standard one. Both are removing code that only ever existed because C++17 was not yet safe to assume.