xref: /llvm-project/libcxx/test/std/containers/container.adaptors/queue/queue.cons/deduct.pass.cpp (revision 87f3ff3e55e67709c3455a7ba105424be8ae84f2)
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 
37 struct A {};
38 
main(int,char **)39 int main(int, char**)
40 {
41 
42 //  Test the explicit deduction guides
43     {
44     std::list<int> l{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
45     std::queue que(l);
46 
47     static_assert(std::is_same_v<decltype(que), std::queue<int, std::list<int>>>, "");
48     assert(que.size() == l.size());
49     assert(que.back() == l.back());
50     }
51 
52     {
53     std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
54     std::queue que(l, test_allocator<long>(0,2)); // different allocator
55     static_assert(std::is_same_v<decltype(que)::container_type, std::list<long, test_allocator<long>>>, "");
56     static_assert(std::is_same_v<decltype(que)::value_type, long>, "");
57     assert(que.size() == 10);
58     assert(que.back() == 19);
59 //  I'd like to assert that we've gotten the right allocator in the queue, but
60 //  I don't know how to get at the underlying container.
61     }
62 
63 //  Test the implicit deduction guides
64     {
65 //  We don't expect this one to work - no way to implicitly get value_type
66 //  std::queue que(std::allocator<int>()); // queue (allocator &)
67     }
68 
69     {
70     std::queue<A> source;
71     std::queue que(source); // queue(queue &)
72     static_assert(std::is_same_v<decltype(que)::value_type, A>, "");
73     static_assert(std::is_same_v<decltype(que)::container_type, std::deque<A>>, "");
74     assert(que.size() == 0);
75     }
76 
77     {
78         typedef short T;
79         typedef test_allocator<T> Alloc;
80         typedef std::list<T, Alloc> Cont;
81         typedef test_allocator<int> ConvertibleToAlloc;
82         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
83                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
84 
85         {
86         Cont cont;
87         std::queue que(cont, Alloc(2));
88         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
89         }
90 
91         {
92         Cont cont;
93         std::queue que(cont, ConvertibleToAlloc(2));
94         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
95         }
96 
97         {
98         Cont cont;
99         std::queue que(std::move(cont), Alloc(2));
100         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
101         }
102 
103         {
104         Cont cont;
105         std::queue que(std::move(cont), ConvertibleToAlloc(2));
106         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
107         }
108     }
109 
110     {
111         typedef short T;
112         typedef test_allocator<T> Alloc;
113         typedef std::list<T, Alloc> Cont;
114         typedef test_allocator<int> ConvertibleToAlloc;
115         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
116                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
117 
118         {
119         std::queue<T, Cont> source;
120         std::queue que(source, Alloc(2));
121         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
122         }
123 
124         {
125         std::queue<T, Cont> source;
126         std::queue que(source, ConvertibleToAlloc(2));
127         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
128         }
129 
130         {
131         std::queue<T, Cont> source;
132         std::queue que(std::move(source), Alloc(2));
133         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
134         }
135 
136         {
137         std::queue<T, Cont> source;
138         std::queue que(std::move(source), ConvertibleToAlloc(2));
139         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
140         }
141     }
142 
143 #if TEST_STD_VER >= 23
144     {
145         typedef short T;
146         typedef test_allocator<T> Alloc;
147         std::list<T> a;
148         {
149         std::queue q(a.begin(), a.end());
150         static_assert(std::is_same_v<decltype(q), std::queue<T>>);
151         }
152         {
153         std::queue q(a.begin(), a.end(), Alloc());
154         static_assert(std::is_same_v<decltype(q), std::queue<T, std::deque<T, Alloc>>>);
155         }
156     }
157 
158     {
159       {
160         std::queue c(std::from_range, std::array<int, 0>());
161         static_assert(std::is_same_v<decltype(c), std::queue<int>>);
162       }
163 
164       {
165         using Alloc = test_allocator<int>;
166         std::queue c(std::from_range, std::array<int, 0>(), Alloc());
167         static_assert(std::is_same_v<decltype(c), std::queue<int, std::deque<int, Alloc>>>);
168       }
169     }
170 #endif
171 
172     ContainerAdaptorDeductionGuidesSfinaeAway<std::queue, std::queue<int>>();
173 
174     return 0;
175 }
176