| 1 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do compile { target c++17 } } |
| 19 | |
| 20 | #include <queue> |
| 21 | #include <deque> |
| 22 | #include <vector> |
| 23 | #include <testsuite_iterators.h> |
| 24 | #include <testsuite_allocator.h> |
| 25 | |
| 26 | template<typename T, typename U> struct require_same; |
| 27 | template<typename T> struct require_same<T, T> { using type = void; }; |
| 28 | |
| 29 | template<typename T, typename U> |
| 30 | typename require_same<T, U>::type |
| 31 | check_type(U&) { } |
| 32 | |
| 33 | void |
| 34 | test01() |
| 35 | { |
| 36 | std::priority_queue<unsigned> s0; |
| 37 | |
| 38 | std::priority_queue s1 = s0; |
| 39 | check_type<std::priority_queue<unsigned>>(s1); |
| 40 | |
| 41 | std::priority_queue s2 = std::move(s0); |
| 42 | check_type<std::priority_queue<unsigned>>(s2); |
| 43 | |
| 44 | const std::priority_queue s3 = s0; |
| 45 | check_type<const std::priority_queue<unsigned>>(s3); |
| 46 | |
| 47 | const std::priority_queue s4 = s3; |
| 48 | check_type<const std::priority_queue<unsigned>>(s4); |
| 49 | |
| 50 | std::allocator<unsigned> a; |
| 51 | std::priority_queue s5(s0, a); |
| 52 | check_type<std::priority_queue<unsigned>>(s5); |
| 53 | |
| 54 | std::priority_queue s6(std::move(s0), a); |
| 55 | check_type<std::priority_queue<unsigned>>(s6); |
| 56 | |
| 57 | const std::priority_queue s7(s3, a); |
| 58 | check_type<const std::priority_queue<unsigned>>(s7); |
| 59 | } |
| 60 | |
| 61 | template<typename T> |
| 62 | using input_iterator_seq |
| 63 | = __gnu_test::test_container<T, __gnu_test::input_iterator_wrapper>; |
| 64 | |
| 65 | void |
| 66 | test02() |
| 67 | { |
| 68 | using Deque = std::deque<int>; |
| 69 | Deque d; |
| 70 | using Vector = std::vector<short>; |
| 71 | Vector v; |
| 72 | using Cmp = std::greater<long>; |
| 73 | Cmp cmp; |
| 74 | |
| 75 | std::priority_queue s1(cmp, d); |
| 76 | check_type<std::priority_queue<int, Deque, Cmp>>(s1); |
| 77 | |
| 78 | std::priority_queue s2(cmp, d, d.get_allocator()); |
| 79 | check_type<std::priority_queue<int, Deque, Cmp>>(s2); |
| 80 | |
| 81 | std::priority_queue s3(cmp, std::move(d)); |
| 82 | check_type<std::priority_queue<int, Deque, Cmp>>(s3); |
| 83 | |
| 84 | std::priority_queue s4(cmp, std::move(d), d.get_allocator()); |
| 85 | check_type<std::priority_queue<int, Deque, Cmp>>(s4); |
| 86 | |
| 87 | std::priority_queue s5(cmp, v); |
| 88 | check_type<std::priority_queue<short, Vector, Cmp>>(s5); |
| 89 | |
| 90 | std::priority_queue s6(cmp, v, v.get_allocator()); |
| 91 | check_type<std::priority_queue<short, Vector, Cmp>>(s6); |
| 92 | |
| 93 | std::priority_queue s7(cmp, std::move(v)); |
| 94 | check_type<std::priority_queue<short, Vector, Cmp>>(s7); |
| 95 | |
| 96 | std::priority_queue s8(cmp, std::move(v), v.get_allocator()); |
| 97 | check_type<std::priority_queue<short, Vector, Cmp>>(s8); |
| 98 | |
| 99 | short a[1] = {}; |
| 100 | input_iterator_seq<short> seq(a); |
| 101 | |
| 102 | std::priority_queue s9(seq.begin(), seq.end()); |
| 103 | check_type<std::priority_queue<short>>(s9); |
| 104 | |
| 105 | std::priority_queue s10(seq.begin(), seq.end(), {}); |
| 106 | check_type<std::priority_queue<short>>(s10); |
| 107 | |
| 108 | std::priority_queue s11(seq.begin(), seq.end(), {}, {}); |
| 109 | check_type<std::priority_queue<short>>(s11); |
| 110 | |
| 111 | std::priority_queue s12(seq.begin(), seq.end(), cmp); |
| 112 | check_type<std::priority_queue<short, Vector, Cmp>>(s12); |
| 113 | |
| 114 | std::priority_queue s13(seq.begin(), seq.end(), cmp, {}); |
| 115 | check_type<std::priority_queue<short, Vector, Cmp>>(s13); |
| 116 | |
| 117 | std::priority_queue s14(seq.begin(), seq.end(), cmp, std::deque<short>{}); |
| 118 | check_type<std::priority_queue<short, std::deque<short>, Cmp>>(s14); |
| 119 | } |
| 120 | |
| 121 | struct Pool; |
| 122 | |
| 123 | template<typename T> |
| 124 | struct Alloc : __gnu_test::SimpleAllocator<T> |
| 125 | { |
| 126 | Alloc(Pool*) { } |
| 127 | |
| 128 | template<typename U> |
| 129 | Alloc(const Alloc<U>&) { } |
| 130 | }; |
| 131 | |
| 132 | void |
| 133 | test_p1518r2() |
| 134 | { |
| 135 | // P1518R2 - Stop overconstraining allocators in container deduction guides. |
| 136 | // This is a C++23 feature but we support it for C++17 too. |
| 137 | |
| 138 | using Vector = std::vector<short, Alloc<short>>; |
| 139 | using Cmp = std::greater<long>; |
| 140 | Pool* p = nullptr; |
| 141 | Vector v(p); |
| 142 | Cmp cmp; |
| 143 | |
| 144 | std::priority_queue q1(cmp, v, p); |
| 145 | check_type<std::priority_queue<short, Vector, Cmp>>(q1); |
| 146 | |
| 147 | std::priority_queue q2(cmp, std::move(v), p); |
| 148 | check_type<std::priority_queue<short, Vector, Cmp>>(q2); |
| 149 | |
| 150 | std::priority_queue q3(q1, p); |
| 151 | check_type<std::priority_queue<short, Vector, Cmp>>(q3); |
| 152 | |
| 153 | std::priority_queue q4(std::move(q1), p); |
| 154 | check_type<std::priority_queue<short, Vector, Cmp>>(q4); |
| 155 | } |