Why C++ custom allocator needs comparison operators?
Asked Answered
T

3

9

While implementing custom C++ allocator, one needs to define:

  • operator== for the allocator with different value_type
  • operator!= for the allocator with different value_type

You can see the example implementation of custom allocator in documentation of Allocator concept:

#include <cstdlib>
#include <new>
template <class T>
struct Mallocator {
  typedef T value_type;
  Mallocator() = default;
  template <class U> constexpr Mallocator(const Mallocator<U>&) noexcept {}
  T* allocate(std::size_t n) {
    if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc();
    if(auto p = static_cast<T*>(std::malloc(n*sizeof(T)))) return p;
    throw std::bad_alloc();
  }
  void deallocate(T* p, std::size_t) noexcept { std::free(p); }
};
template <class T, class U>
bool operator==(const Mallocator<T>&, const Mallocator<U>&) { return true; }
template <class T, class U>
bool operator!=(const Mallocator<T>&, const Mallocator<U>&) { return false; }

The question is what are purposes of those 2 operators? When are they being used?

EDIT:

Please note I am not asking how to implement such operators (explained in the link), I am asking why it is necessary to implement them, so when those are being used.

Tendency answered 8/11, 2017 at 18:1 Comment(2)
The explanation is provided in the row that mentions the operator== function. 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. Is that not sufficient explanation?Dappled
@RSahu no, that doesn't explain why such operator is necessaryNathalienathan
V
15

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.

Valeda answered 8/11, 2017 at 18:9 Comment(2)
@jaskmar Isn't his answer a satisfactory one? Seriously?Vassaux
@Nik-Lz -- the other two answers have the text from the standard.Valeda
W
2

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.

Wizened answered 8/11, 2017 at 18:5 Comment(1)
That doesn't explain why such operator is necessaryNathalienathan
P
0

Docs also says

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.

Purchase answered 8/11, 2017 at 18:6 Comment(4)
That doesn't explain why such operator is necessaryNathalienathan
@jaskmar You original question did not contain this question. To downvote this answer which explains at least an aspect of your original question is not the right reaction.Wizened
@Wizened This is the quote from original question: "The question is what are purposes of those 2 operators? When are they being used?" So it DID contain :)Nathalienathan
@jaskmar However your question was meant, a downvote certainly is not appropriate. Nothing more to add.Wizened

© 2022 - 2024 — McMap. All rights reserved.