Skip to content

Commit 886e92c

Browse files
committed
[libc++][test] Silence allocator conversion warnings
... by accepting `std::size_t` instead of `int` in `allocate` and `deallocate` functions. Drive-by: To conform to the allocator requirements, the `Allocator` types in these tests need to have (1) converting constructors and (2) cross-specialization `==` that returns `true` at least for copies of the same allocator. Differential Revision: https://reviews.llvm.org/D141334
1 parent 356c718 commit 886e92c

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

libcxx/test/libcxx/containers/sequences/vector/robust_against_adl.pass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// <vector>
1212

13+
#include <cstddef>
1314
#include <vector>
1415

1516
#include "test_macros.h"
@@ -22,8 +23,8 @@ struct MyAlloc {
2223
using value_type = T;
2324
MyAlloc() = default;
2425
template<class U> MyAlloc(const MyAlloc<U>&) {}
25-
T *allocate(int n) { return std::allocator<T>().allocate(n); }
26-
void deallocate(T *p, int n) { return std::allocator<T>().deallocate(p, n); }
26+
T *allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
27+
void deallocate(T *p, std::size_t n) { return std::allocator<T>().deallocate(p, n); }
2728
};
2829

2930
int main(int, char**)

libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// (bug report: https://llvm.org/PR58392)
1212
// Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
1313

14+
#include <cstddef>
1415
#include <type_traits>
1516
#include <vector>
1617

@@ -30,10 +31,11 @@ struct Allocator {
3031
throw 0;
3132
}
3233

33-
T* allocate(int n) { return std::allocator<T>().allocate(n); }
34-
void deallocate(T* ptr, int n) { std::allocator<T>().deallocate(ptr, n); }
34+
T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
35+
void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
3536

36-
friend bool operator==(const Allocator&, const Allocator&) { return false; }
37+
template <class U>
38+
friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
3739
};
3840

3941
template <class IterCat>

libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// (bug report: https://llvm.org/PR58392)
1212
// Check that vector constructors don't leak memory when an operation inside the constructor throws an exception
1313

14+
#include <cstddef>
1415
#include <type_traits>
1516
#include <vector>
1617

@@ -22,15 +23,19 @@ struct Allocator {
2223
using value_type = T;
2324
using is_always_equal = std::false_type;
2425

26+
template <class U>
27+
Allocator(const Allocator<U>&) {}
28+
2529
Allocator(bool should_throw = true) {
2630
if (should_throw)
2731
throw 0;
2832
}
2933

30-
T* allocate(int n) { return std::allocator<T>().allocate(n); }
31-
void deallocate(T* ptr, int n) { std::allocator<T>().deallocate(ptr, n); }
34+
T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); }
35+
void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
3236

33-
friend bool operator==(const Allocator&, const Allocator&) { return false; }
37+
template <class U>
38+
friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
3439
};
3540

3641
struct ThrowingT {

0 commit comments

Comments
 (0)