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 "deduction_guides_sfinae_checks.h" 29 #include "test_macros.h" 30 #include "test_iterators.h" 31 #include "test_allocator.h" 32 33 struct A {}; 34 35 int main(int, char**) 36 { 37 38 // Test the explicit deduction guides 39 { 40 std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 41 std::stack stk(v); 42 43 static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, ""); 44 assert(stk.size() == v.size()); 45 assert(stk.top() == v.back()); 46 } 47 48 { 49 std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; 50 std::stack stk(l, test_allocator<long>(0,2)); // different allocator 51 static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, ""); 52 static_assert(std::is_same_v<decltype(stk)::value_type, long>, ""); 53 assert(stk.size() == 10); 54 assert(stk.top() == 19); 55 // I'd like to assert that we've gotten the right allocator in the stack, but 56 // I don't know how to get at the underlying container. 57 } 58 59 // Test the implicit deduction guides 60 61 { 62 // We don't expect this one to work - no way to implicitly get value_type 63 // std::stack stk(std::allocator<int>()); // stack (allocator &) 64 } 65 66 { 67 std::stack<A> source; 68 std::stack stk(source); // stack(stack &) 69 static_assert(std::is_same_v<decltype(stk)::value_type, A>, ""); 70 static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, ""); 71 assert(stk.size() == 0); 72 } 73 74 { 75 typedef short T; 76 typedef test_allocator<T> Alloc; 77 typedef std::list<T, Alloc> Cont; 78 typedef test_allocator<int> ConvertibleToAlloc; 79 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> && 80 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>); 81 82 { 83 Cont cont; 84 std::stack stk(cont, Alloc(2)); 85 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 86 } 87 88 { 89 Cont cont; 90 std::stack stk(cont, ConvertibleToAlloc(2)); 91 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 92 } 93 94 { 95 Cont cont; 96 std::stack stk(std::move(cont), Alloc(2)); 97 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 98 } 99 100 { 101 Cont cont; 102 std::stack stk(std::move(cont), ConvertibleToAlloc(2)); 103 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 104 } 105 } 106 107 { 108 typedef short T; 109 typedef test_allocator<T> Alloc; 110 typedef std::list<T, Alloc> Cont; 111 typedef test_allocator<int> ConvertibleToAlloc; 112 static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> && 113 !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>); 114 115 { 116 std::stack<T, Cont> source; 117 std::stack stk(source, Alloc(2)); 118 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 119 } 120 121 { 122 std::stack<T, Cont> source; 123 std::stack stk(source, ConvertibleToAlloc(2)); 124 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 125 } 126 127 { 128 std::stack<T, Cont> source; 129 std::stack stk(std::move(source), Alloc(2)); 130 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 131 } 132 133 { 134 std::stack<T, Cont> source; 135 std::stack stk(std::move(source), ConvertibleToAlloc(2)); 136 static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>); 137 } 138 } 139 140 // Deduction guides should be SFINAE'd away when given: 141 // - a "bad" allocator (that is, a type not qualifying as an allocator); 142 // - an allocator instead of a container; 143 // - an allocator and a container that uses a different allocator. 144 { 145 using Cont = std::list<int>; 146 using Alloc = std::allocator<int>; 147 using DiffAlloc = test_allocator<int>; 148 149 struct BadAlloc {}; 150 using AllocAsCont = Alloc; 151 152 // (cont, alloc) 153 // 154 // Cannot deduce from (ALLOC_as_cont, alloc) 155 static_assert(SFINAEs_away<std::stack, AllocAsCont, Alloc>); 156 // Cannot deduce from (cont, BAD_alloc) 157 static_assert(SFINAEs_away<std::stack, Cont, BadAlloc>); 158 // Cannot deduce from (cont, DIFFERENT_alloc) 159 static_assert(SFINAEs_away<std::stack, Cont, DiffAlloc>); 160 } 161 162 return 0; 163 } 164