1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <queue>
10// UNSUPPORTED: c++03, c++11, c++14
11
12// template<class Container>
13// queue(Container) -> queue<typename Container::value_type, Container>;
14//
15// template<class Container, class Allocator>
16// queue(Container, Allocator) -> queue<typename Container::value_type, Container>;
17//
18// template<ranges::input_range R>
19// queue(from_range_t, R&&) -> queue<ranges::range_value_t<R>>; // since C++23
20//
21// template<ranges::input_range R, class Allocator>
22// queue(from_range_t, R&&, Allocator)
23// -> queue<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
24
25#include <array>
26#include <queue>
27#include <list>
28#include <iterator>
29#include <cassert>
30#include <cstddef>
31
32#include "deduction_guides_sfinae_checks.h"
33#include "test_macros.h"
34#include "test_iterators.h"
35#include "test_allocator.h"
36
37struct A {};
38
39int main(int, char**) {
40 // Test the explicit deduction guides
41 {
42 std::list<int> l{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
43 std::queue que(l);
44
45 static_assert(std::is_same_v<decltype(que), std::queue<int, std::list<int>>>, "");
46 assert(que.size() == l.size());
47 assert(que.back() == l.back());
48 }
49
50 {
51 std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
52 std::queue que(l, test_allocator<long>(0, 2)); // different allocator
53 static_assert(std::is_same_v<decltype(que)::container_type, std::list<long, test_allocator<long>>>, "");
54 static_assert(std::is_same_v<decltype(que)::value_type, long>, "");
55 assert(que.size() == 10);
56 assert(que.back() == 19);
57 // I'd like to assert that we've gotten the right allocator in the queue, but
58 // I don't know how to get at the underlying container.
59 }
60
61 // Test the implicit deduction guides
62 {
63 // We don't expect this one to work - no way to implicitly get value_type
64 // std::queue que(std::allocator<int>()); // queue (allocator &)
65 }
66
67 {
68 std::queue<A> source;
69 std::queue que(source); // queue(queue &)
70 static_assert(std::is_same_v<decltype(que)::value_type, A>, "");
71 static_assert(std::is_same_v<decltype(que)::container_type, std::deque<A>>, "");
72 assert(que.size() == 0);
73 }
74
75 {
76 typedef short T;
77 typedef test_allocator<T> Alloc;
78 typedef std::list<T, Alloc> Cont;
79 typedef test_allocator<int> ConvertibleToAlloc;
80 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
81 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
82
83 {
84 Cont cont;
85 std::queue que(cont, Alloc(2));
86 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
87 }
88
89 {
90 Cont cont;
91 std::queue que(cont, ConvertibleToAlloc(2));
92 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
93 }
94
95 {
96 Cont cont;
97 std::queue que(std::move(cont), Alloc(2));
98 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
99 }
100
101 {
102 Cont cont;
103 std::queue que(std::move(cont), ConvertibleToAlloc(2));
104 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
105 }
106 }
107
108 {
109 typedef short T;
110 typedef test_allocator<T> Alloc;
111 typedef std::list<T, Alloc> Cont;
112 typedef test_allocator<int> ConvertibleToAlloc;
113 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
114 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
115
116 {
117 std::queue<T, Cont> source;
118 std::queue que(source, Alloc(2));
119 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
120 }
121
122 {
123 std::queue<T, Cont> source;
124 std::queue que(source, ConvertibleToAlloc(2));
125 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
126 }
127
128 {
129 std::queue<T, Cont> source;
130 std::queue que(std::move(source), Alloc(2));
131 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
132 }
133
134 {
135 std::queue<T, Cont> source;
136 std::queue que(std::move(source), ConvertibleToAlloc(2));
137 static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
138 }
139 }
140
141#if TEST_STD_VER >= 23
142 {
143 typedef short T;
144 typedef test_allocator<T> Alloc;
145 std::list<T> a;
146 {
147 std::queue q(a.begin(), a.end());
148 static_assert(std::is_same_v<decltype(q), std::queue<T>>);
149 }
150 {
151 std::queue q(a.begin(), a.end(), Alloc());
152 static_assert(std::is_same_v<decltype(q), std::queue<T, std::deque<T, Alloc>>>);
153 }
154 }
155
156 {
157 {
158 std::queue c(std::from_range, std::array<int, 0>());
159 static_assert(std::is_same_v<decltype(c), std::queue<int>>);
160 }
161
162 {
163 using Alloc = test_allocator<int>;
164 std::queue c(std::from_range, std::array<int, 0>(), Alloc());
165 static_assert(std::is_same_v<decltype(c), std::queue<int, std::deque<int, Alloc>>>);
166 }
167 }
168#endif
169
170 ContainerAdaptorDeductionGuidesSfinaeAway<std::queue, std::queue<int>>();
171
172 return 0;
173}
174

source code of libcxx/test/std/containers/container.adaptors/queue/queue.cons/deduct.pass.cpp