Why C++ custom allocator needs comparison operators?

The very documentation you linked to contains the answer:

[operator ==(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.

So whenever you have an allocator for a certain type, you can check whether you can use it to deallocate memory of a different type without the need to rebind the allocator.


Read about the allocator requirements. operator== tells you whether one of the allocator object instances can free memory that was allocated by the other. That's important when you move the contents of one container into another. If the second container's allocator is == to the first container's, you can, in general, do the move by swapping a pointer or two, reusing the first container's memory in the second. If the allocators are not equal, then the copy operation has to copy each element, allocating memory in the second container as needed, and free the memory held by the first container.