| 1 |
// { dg-do compile { target c++11 } } |
| 2 |
|
| 3 |
#include <list> |
| 4 |
#include <testsuite_allocator.h> |
| 5 |
|
| 6 |
// An allocator that uses __gnu_cxx::_Pointer_adapter as its pointer type. |
| 7 |
template class std::list<int, __gnu_test::CustomPointerAlloc<int>>; |
| 8 |
|
| 9 |
// Unlike __gnu_cxx::_Pointer_adapter, this fancy pointer supports neither |
| 10 |
// implicit nor explicit conversions from raw pointers. The constructor from |
| 11 |
// a raw pointer is explicit and requires a second parameter. The only way for |
| 12 |
// containers to construct one of these pointers is pointer_traits::pointer_to. |
| 13 |
template<typename T> |
| 14 |
struct Pointer : __gnu_test::PointerBase<Pointer<T>, T> |
| 15 |
{ |
| 16 |
using Base = __gnu_test::PointerBase<Pointer<T>, T>; |
| 17 |
|
| 18 |
Pointer() = default; |
| 19 |
Pointer(std::nullptr_t) : Base() { } |
| 20 |
explicit Pointer(T* p, int) : Base(p) { } |
| 21 |
|
| 22 |
// Allow conversions to const_pointer and to void_pointer |
| 23 |
template<typename U, typename = typename std::enable_if< |
| 24 |
(!std::is_const<U>::value && std::is_same<T, const U>::value) |
| 25 |
|| (std::is_void<T>::value && std::is_convertible<U*, T*>::value) |
| 26 |
>::type> |
| 27 |
Pointer(const Pointer<U>& p) : Base(p.operator->()) { } |
| 28 |
|
| 29 |
template<typename U> |
| 30 |
static typename std::enable_if<std::is_same<U, T>::value, Pointer>::type |
| 31 |
pointer_to(U& t) |
| 32 |
{ return Pointer(std::addressof(t), 1); } |
| 33 |
}; |
| 34 |
|
| 35 |
// A minimal allocator that uses Pointer as its pointer type. |
| 36 |
template<typename T> |
| 37 |
struct Allocator |
| 38 |
{ |
| 39 |
using value_type = T; |
| 40 |
using pointer = Pointer<T>; |
| 41 |
|
| 42 |
Allocator() = default; |
| 43 |
template<typename U> |
| 44 |
Allocator(const Allocator<U>&) { } |
| 45 |
|
| 46 |
pointer allocate(std::size_t n) |
| 47 |
{ return pointer(std::allocator<T>().allocate(n), 1); } |
| 48 |
|
| 49 |
void deallocate(pointer p, std::size_t n) |
| 50 |
{ |
| 51 |
std::allocator<T>().deallocate(p.operator->(), n); |
| 52 |
} |
| 53 |
|
| 54 |
bool operator==(const Allocator&) const { return true; } |
| 55 |
bool operator!=(const Allocator&) const { return false; } |
| 56 |
}; |
| 57 |
|
| 58 |
template class std::list<int, Allocator<int>>; |
| 59 |
|
| 60 |
struct NonTrivial |
| 61 |
{ |
| 62 |
NonTrivial() { } |
| 63 |
NonTrivial(const NonTrivial&) { } |
| 64 |
~NonTrivial() { } |
| 65 |
bool operator==(const NonTrivial&) const { return true; } // for remove(T) |
| 66 |
bool operator<(const NonTrivial&) const { return false; } // for sort() |
| 67 |
}; |
| 68 |
|
| 69 |
template class std::list<NonTrivial, Allocator<NonTrivial>>; |
| 70 |
|
| 71 |
#include <testsuite_iterators.h> |
| 72 |
|
| 73 |
void |
| 74 |
test_template_members(__gnu_test::input_container<short>& c) |
| 75 |
{ |
| 76 |
// Use member functions that are not included in explicit instantiations. |
| 77 |
std::list<int, Allocator<int>> l(c.begin(), c.end()); |
| 78 |
l.assign(c.begin(), c.end()); |
| 79 |
l.insert(l.begin(), c.begin(), c.end()); |
| 80 |
l.emplace_front(1); |
| 81 |
l.emplace_back(1); |
| 82 |
l.emplace(l.begin(), 1); |
| 83 |
l.remove_if([](int) { return false; }); |
| 84 |
l.unique([](int, int) { return false; }); |
| 85 |
l.merge(l, [](int, int) { return false; }); |
| 86 |
l.merge(std::move(l), [](int, int) { return false; }); |
| 87 |
l.sort([](int, int) { return false; }); |
| 88 |
|
| 89 |
#ifdef __cpp_lib_ranges_to_container |
| 90 |
short arr[2]; |
| 91 |
__gnu_test::test_input_range<short> r(arr); |
| 92 |
std::list<int, Allocator<int>> l2(std::from_range, r); |
| 93 |
l2.assign_range(r); |
| 94 |
l2.prepend_range(r); |
| 95 |
l2.append_range(r); |
| 96 |
l2.insert_range(l2.begin(), r); |
| 97 |
#endif |
| 98 |
} |