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++98, 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 
main(int,char **)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 //  This one is odd - you can pass an allocator in to use, but the allocator
76 //  has to match the type of the one used by the underlying container
77     typedef short T;
78     typedef test_allocator<T> A;
79     typedef std::deque<T, A> C;
80 
81     C c{0,1,2,3};
82     std::queue<T, C> source(c);
83     std::queue que(source, A(2)); // queue(queue &, allocator)
84     static_assert(std::is_same_v<decltype(que)::value_type, T>, "");
85     static_assert(std::is_same_v<decltype(que)::container_type, C>, "");
86     assert(que.size() == 4);
87     assert(que.back() == 3);
88     }
89 
90 
91   return 0;
92 }
93