std::vector::shrink_to_fit() does not check allocator equality

Viewed 107

I have my own stack allocator which works with the test cases I have except with std::vector::shrink_to_fit()

shrink_to_fit() tries to deallocate memory with a different stateful allocator than the memory was originally allocated from.

My understanding is that a container must check allocators for equality before memory allocated from one allocator is deallocated trough a different one. Am I wrong?

Quote from: https://en.cppreference.com/w/cpp/named_req/Allocator

a1 == a2 returns true only if the storage allocated by the allocator a1 can be deallocated through a2. Establishes reflexive, symmetric, and transitive relationship. Does not throw exceptions.

In my example, the equality check operators are not even called.

I am using g++ 5.4.0. A full code example can be found here: https://onlinegdb.com/ryG9W5sx4

1 Answers

Copies of an allocator must compare equal. Yours don't.

(It follows that even a stateful allocator cannot have embedded storage.)

Related