xref: /llvm-project/libcxx/test/std/containers/container.adaptors/stack/stack.cons/deduct.pass.cpp (revision 5b8b8b5dce587f1e5a4a31cc24f09b18bd53ff9a)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <stack>
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 // UNSUPPORTED: libcpp-no-deduction-guides
13 
14 
15 // template<class Container>
16 //   stack(Container) -> stack<typename Container::value_type, Container>;
17 //
18 // template<class Container, class Allocator>
19 //   stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
20 
21 
22 #include <stack>
23 #include <vector>
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()
37 {
38 
39 //  Test the explicit deduction guides
40     {
41     std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
42     std::stack stk(v);
43 
44     static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "");
45     assert(stk.size() == v.size());
46     assert(stk.top() == v.back());
47     }
48 
49     {
50     std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
51     std::stack stk(l, test_allocator<long>(0,2)); // different allocator
52     static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "");
53     static_assert(std::is_same_v<decltype(stk)::value_type, long>, "");
54     assert(stk.size() == 10);
55     assert(stk.top() == 19);
56 //  I'd like to assert that we've gotten the right allocator in the stack, but
57 //  I don't know how to get at the underlying container.
58     }
59 
60 //  Test the implicit deduction guides
61 
62     {
63 //  We don't expect this one to work - no way to implicitly get value_type
64 //  std::stack stk(std::allocator<int>()); // stack (allocator &)
65     }
66 
67     {
68     std::stack<A> source;
69     std::stack stk(source); // stack(stack &)
70     static_assert(std::is_same_v<decltype(stk)::value_type, A>, "");
71     static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "");
72     assert(stk.size() == 0);
73     }
74 
75     {
76 //  This one is odd - you can pass an allocator in to use, but the allocator
77 //  has to match the type of the one used by the underlying container
78     typedef short T;
79     typedef test_allocator<T> A;
80     typedef std::deque<T, A> C;
81 
82     C c{0,1,2,3};
83     std::stack<T, C> source(c);
84     std::stack stk(source, A(2)); // stack(stack &, allocator)
85     static_assert(std::is_same_v<decltype(stk)::value_type, T>, "");
86     static_assert(std::is_same_v<decltype(stk)::container_type, C>, "");
87     assert(stk.size() == 4);
88     assert(stk.top() == 3);
89     }
90 
91 }
92