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 // UNSUPPORTED: clang-5, apple-clang-9
12 // UNSUPPORTED: libcpp-no-deduction-guides
13 // Clang 5 will generate bad implicit deduction guides
14 //  Specifically, for the copy constructor.
15 
16 // template<class Container>
17 //   queue(Container) -> queue<typename Container::value_type, Container>;
18 //
19 // template<class Container, class Allocator>
20 //   queue(Container, Allocator) -> queue<typename Container::value_type, Container>;
21 
22 
23 #include <queue>
24 #include <list>
25 #include <iterator>
26 #include <cassert>
27 #include <cstddef>
28 #include <climits> // INT_MAX
29 
30 #include "test_macros.h"
31 #include "test_iterators.h"
32 #include "test_allocator.h"
33 
34 struct A {};
35 
36 int main(int, char**)
37 {
38 
39 //  Test the explicit deduction guides
40     {
41     std::list<int> l{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
42     std::queue que(l);
43 
44     static_assert(std::is_same_v<decltype(que), std::queue<int, std::list<int>>>, "");
45     assert(que.size() == l.size());
46     assert(que.back() == l.back());
47     }
48 
49     {
50     std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
51     std::queue que(l, test_allocator<long>(0,2)); // different allocator
52     static_assert(std::is_same_v<decltype(que)::container_type, std::list<long, test_allocator<long>>>, "");
53     static_assert(std::is_same_v<decltype(que)::value_type, long>, "");
54     assert(que.size() == 10);
55     assert(que.back() == 19);
56 //  I'd like to assert that we've gotten the right allocator in the queue, but
57 //  I don't know how to get at the underlying container.
58     }
59 
60 //  Test the implicit deduction guides
61     {
62 //  We don't expect this one to work - no way to implicitly get value_type
63 //  std::queue que(std::allocator<int>()); // queue (allocator &)
64     }
65 
66     {
67     std::queue<A> source;
68     std::queue que(source); // queue(queue &)
69     static_assert(std::is_same_v<decltype(que)::value_type, A>, "");
70     static_assert(std::is_same_v<decltype(que)::container_type, std::deque<A>>, "");
71     assert(que.size() == 0);
72     }
73 
74     {
75         typedef short T;
76         typedef test_allocator<T> Alloc;
77         typedef std::list<T, Alloc> Cont;
78         typedef test_allocator<int> ConvertibleToAlloc;
79         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
80                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
81 
82         {
83         Cont cont;
84         std::queue que(cont, Alloc(2));
85         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
86         }
87 
88         {
89         Cont cont;
90         std::queue que(cont, ConvertibleToAlloc(2));
91         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
92         }
93 
94         {
95         Cont cont;
96         std::queue que(std::move(cont), Alloc(2));
97         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
98         }
99 
100         {
101         Cont cont;
102         std::queue que(std::move(cont), ConvertibleToAlloc(2));
103         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
104         }
105     }
106 
107     {
108         typedef short T;
109         typedef test_allocator<T> Alloc;
110         typedef std::list<T, Alloc> Cont;
111         typedef test_allocator<int> ConvertibleToAlloc;
112         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
113                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
114 
115         {
116         std::queue<T, Cont> source;
117         std::queue que(source, Alloc(2));
118         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
119         }
120 
121         {
122         std::queue<T, Cont> source;
123         std::queue que(source, ConvertibleToAlloc(2));
124         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
125         }
126 
127         {
128         std::queue<T, Cont> source;
129         std::queue que(std::move(source), Alloc(2));
130         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
131         }
132 
133         {
134         std::queue<T, Cont> source;
135         std::queue que(std::move(source), ConvertibleToAlloc(2));
136         static_assert(std::is_same_v<decltype(que), std::queue<T, Cont>>);
137         }
138     }
139 
140     return 0;
141 }
142