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