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 // 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 17 // template<class Container> 18 // stack(Container) -> stack<typename Container::value_type, Container>; 19 // 20 // template<class Container, class Allocator> 21 // stack(Container, Allocator) -> stack<typename Container::value_type, Container>; 22 23 24 #include <stack> 25 #include <deque> 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 39 int main(int, char**) 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 typedef short T; 80 typedef test_allocator<T> Alloc; 81 typedef std::list<T, Alloc> Cont; 82 typedef test_allocator<int> ConvertibleToAlloc; 83 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> && 84 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>); 85 86 { 87 Cont cont; 88 std::stack stk(cont, Alloc(2)); 89 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 90 } 91 92 { 93 Cont cont; 94 std::stack stk(cont, ConvertibleToAlloc(2)); 95 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 96 } 97 98 { 99 Cont cont; 100 std::stack stk(std::move(cont), Alloc(2)); 101 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 102 } 103 104 { 105 Cont cont; 106 std::stack stk(std::move(cont), ConvertibleToAlloc(2)); 107 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 108 } 109 } 110 111 { 112 typedef short T; 113 typedef test_allocator<T> Alloc; 114 typedef std::list<T, Alloc> Cont; 115 typedef test_allocator<int> ConvertibleToAlloc; 116 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> && 117 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>); 118 119 { 120 std::stack<T, Cont> source; 121 std::stack stk(source, Alloc(2)); 122 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 123 } 124 125 { 126 std::stack<T, Cont> source; 127 std::stack stk(source, ConvertibleToAlloc(2)); 128 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 129 } 130 131 { 132 std::stack<T, Cont> source; 133 std::stack stk(std::move(source), Alloc(2)); 134 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 135 } 136 137 { 138 std::stack<T, Cont> source; 139 std::stack stk(std::move(source), ConvertibleToAlloc(2)); 140 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 141 } 142 } 143 144 return 0; 145 } 146