1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
17 // template<class Container>
18 // stack(Container) -> stack<typename Container::value_type, Container>;
20 // template<class Container, class Allocator>
21 // stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
30 #include <climits> // INT_MAX
32 #include "test_macros.h"
33 #include "test_iterators.h"
34 #include "test_allocator.h"
41 // Test the explicit deduction guides
43 std::vector
<int> v
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
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());
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.
62 // Test the implicit deduction guides
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 &)
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);
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
81 typedef test_allocator
<T
> Alloc
;
82 typedef std::deque
<T
, Alloc
> Container
;
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);