Skip to content

Commit

Permalink
Test: Don't derive CustomAllocator from std::allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire committed Jul 3, 2024
1 parent 410256e commit ed581bd
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions test/test_encoding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,13 @@ void test_utf_to_utf()

/// Allocator that reports when it has been used in a static variable
template<typename T>
struct CustomAllocator : std::allocator<T> {
struct CustomAllocator {
using value_type = T;
template<typename U>
struct rebind {
typedef CustomAllocator<U> other;
};

CustomAllocator(const int id = 1) : id(id) {}
template<typename U>
CustomAllocator(const CustomAllocator<U>& other) : id(other.id)
Expand All @@ -471,16 +477,32 @@ struct CustomAllocator : std::allocator<T> {
T* allocate(size_t n)
{
usedId += id;
return std::allocator<T>::allocate(n);
return base.allocate(n);
}

void deallocate(T* p, size_t n)
{
usedId += id;
return base.deallocate(p, n);
}
template<typename U>
struct rebind {
typedef CustomAllocator<U> other;
};

int id;
static int usedId;
int id;

private:
std::allocator<T> base;
};
template<class T, class U>
bool operator==(const CustomAllocator<T>&, const CustomAllocator<U>&)
{
return true;
}

template<class T, class U>
bool operator!=(const CustomAllocator<T>&, const CustomAllocator<U>&)
{
return false;
}
template<typename T>
int CustomAllocator<T>::usedId = 0;

Expand Down

0 comments on commit ed581bd

Please sign in to comment.