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: clang-5, apple-clang-9
13 // UNSUPPORTED: libcpp-no-deduction-guides
14 // Clang 5 will generate bad implicit deduction guides
15 //	Specifically, for the copy constructor.
16 
17 
18 // template<class Container>
19 //   stack(Container) -> stack<typename Container::value_type, Container>;
20 //
21 // template<class Container, class Allocator>
22 //   stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
23 
24 
25 #include <stack>
26 #include <vector>
27 #include <list>
28 #include <iterator>
29 #include <cassert>
30 #include <cstddef>
31 #include <climits> // INT_MAX
32 
33 #include "test_macros.h"
34 #include "test_iterators.h"
35 #include "test_allocator.h"
36 
37 struct A {};
38 
main()39 int main()
40 {
41 
42 //  Test the explicit deduction guides
43     {
44     std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
45     std::stack stk(v);
46 
47     static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "");
48     assert(stk.size() == v.size());
49     assert(stk.top() == v.back());
50     }
51 
52     {
53     std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
54     std::stack stk(l, test_allocator<long>(0,2)); // different allocator
55     static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "");
56     static_assert(std::is_same_v<decltype(stk)::value_type, long>, "");
57     assert(stk.size() == 10);
58     assert(stk.top() == 19);
59 //  I'd like to assert that we've gotten the right allocator in the stack, but
60 //  I don't know how to get at the underlying container.
61     }
62 
63 //  Test the implicit deduction guides
64 
65     {
66 //  We don't expect this one to work - no way to implicitly get value_type
67 //  std::stack stk(std::allocator<int>()); // stack (allocator &)
68     }
69 
70     {
71     std::stack<A> source;
72     std::stack stk(source); // stack(stack &)
73     static_assert(std::is_same_v<decltype(stk)::value_type, A>, "");
74     static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "");
75     assert(stk.size() == 0);
76     }
77 
78     {
79 //  This one is odd - you can pass an allocator in to use, but the allocator
80 //  has to match the type of the one used by the underlying container
81     typedef short T;
82     typedef test_allocator<T> A;
83     typedef std::deque<T, A> C;
84 
85     C c{0,1,2,3};
86     std::stack<T, C> source(c);
87     std::stack stk(source, A(2)); // stack(stack &, allocator)
88     static_assert(std::is_same_v<decltype(stk)::value_type, T>, "");
89     static_assert(std::is_same_v<decltype(stk)::container_type, C>, "");
90     assert(stk.size() == 4);
91     assert(stk.top() == 3);
92     }
93 
94 }
95