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