xref: /llvm-project/libcxx/test/std/containers/container.adaptors/stack/stack.cons/deduct.pass.cpp (revision 87f3ff3e55e67709c3455a7ba105424be8ae84f2)
15b8b8b5dSMarshall Clow //===----------------------------------------------------------------------===//
25b8b8b5dSMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65b8b8b5dSMarshall Clow //
75b8b8b5dSMarshall Clow //===----------------------------------------------------------------------===//
85b8b8b5dSMarshall Clow 
95b8b8b5dSMarshall Clow // <stack>
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
115b8b8b5dSMarshall Clow 
125b8b8b5dSMarshall Clow // template<class Container>
135b8b8b5dSMarshall Clow //   stack(Container) -> stack<typename Container::value_type, Container>;
145b8b8b5dSMarshall Clow //
155b8b8b5dSMarshall Clow // template<class Container, class Allocator>
165b8b8b5dSMarshall Clow //   stack(Container, Allocator) -> stack<typename Container::value_type, Container>;
17*87f3ff3eSvarconst //
18*87f3ff3eSvarconst // template<ranges::input_range R>
19*87f3ff3eSvarconst //   stack(from_range_t, R&&) -> stack<ranges::range_value_t<R>>; // since C++23
20*87f3ff3eSvarconst //
21*87f3ff3eSvarconst // template<ranges::input_range R, class Allocator>
22*87f3ff3eSvarconst //   stack(from_range_t, R&&, Allocator)
23*87f3ff3eSvarconst //     -> stack<ranges::range_value_t<R>, deque<ranges::range_value_t<R>, Allocator>>; // since C++23
245b8b8b5dSMarshall Clow 
25f3aed369SNikolas Klauser #include <array>
265b8b8b5dSMarshall Clow #include <stack>
27dd15c272SArthur O'Dwyer #include <deque>
285b8b8b5dSMarshall Clow #include <vector>
295b8b8b5dSMarshall Clow #include <list>
305b8b8b5dSMarshall Clow #include <iterator>
315b8b8b5dSMarshall Clow #include <cassert>
325b8b8b5dSMarshall Clow #include <cstddef>
335b8b8b5dSMarshall Clow #include <climits> // INT_MAX
345b8b8b5dSMarshall Clow 
3568072a71SKonstantin Varlamov #include "deduction_guides_sfinae_checks.h"
365b8b8b5dSMarshall Clow #include "test_macros.h"
375b8b8b5dSMarshall Clow #include "test_iterators.h"
385b8b8b5dSMarshall Clow #include "test_allocator.h"
395b8b8b5dSMarshall Clow 
405b8b8b5dSMarshall Clow struct A {};
415b8b8b5dSMarshall Clow 
main(int,char **)422df59c50SJF Bastien int main(int, char**)
435b8b8b5dSMarshall Clow {
445b8b8b5dSMarshall Clow 
455b8b8b5dSMarshall Clow //  Test the explicit deduction guides
465b8b8b5dSMarshall Clow     {
475b8b8b5dSMarshall Clow     std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
485b8b8b5dSMarshall Clow     std::stack stk(v);
495b8b8b5dSMarshall Clow 
505b8b8b5dSMarshall Clow     static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "");
515b8b8b5dSMarshall Clow     assert(stk.size() == v.size());
525b8b8b5dSMarshall Clow     assert(stk.top() == v.back());
535b8b8b5dSMarshall Clow     }
545b8b8b5dSMarshall Clow 
555b8b8b5dSMarshall Clow     {
565b8b8b5dSMarshall Clow     std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
575b8b8b5dSMarshall Clow     std::stack stk(l, test_allocator<long>(0,2)); // different allocator
585b8b8b5dSMarshall Clow     static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "");
595b8b8b5dSMarshall Clow     static_assert(std::is_same_v<decltype(stk)::value_type, long>, "");
605b8b8b5dSMarshall Clow     assert(stk.size() == 10);
615b8b8b5dSMarshall Clow     assert(stk.top() == 19);
625b8b8b5dSMarshall Clow //  I'd like to assert that we've gotten the right allocator in the stack, but
635b8b8b5dSMarshall Clow //  I don't know how to get at the underlying container.
645b8b8b5dSMarshall Clow     }
655b8b8b5dSMarshall Clow 
665b8b8b5dSMarshall Clow //  Test the implicit deduction guides
675b8b8b5dSMarshall Clow 
685b8b8b5dSMarshall Clow     {
695b8b8b5dSMarshall Clow //  We don't expect this one to work - no way to implicitly get value_type
705b8b8b5dSMarshall Clow //  std::stack stk(std::allocator<int>()); // stack (allocator &)
715b8b8b5dSMarshall Clow     }
725b8b8b5dSMarshall Clow 
735b8b8b5dSMarshall Clow     {
745b8b8b5dSMarshall Clow     std::stack<A> source;
755b8b8b5dSMarshall Clow     std::stack stk(source); // stack(stack &)
765b8b8b5dSMarshall Clow     static_assert(std::is_same_v<decltype(stk)::value_type, A>, "");
775b8b8b5dSMarshall Clow     static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "");
785b8b8b5dSMarshall Clow     assert(stk.size() == 0);
795b8b8b5dSMarshall Clow     }
805b8b8b5dSMarshall Clow 
815b8b8b5dSMarshall Clow     {
825b8b8b5dSMarshall Clow         typedef short T;
83c479e0c9SLouis Dionne         typedef test_allocator<T> Alloc;
84dd15c272SArthur O'Dwyer         typedef std::list<T, Alloc> Cont;
85dd15c272SArthur O'Dwyer         typedef test_allocator<int> ConvertibleToAlloc;
86dd15c272SArthur O'Dwyer         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
87dd15c272SArthur O'Dwyer                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
885b8b8b5dSMarshall Clow 
89dd15c272SArthur O'Dwyer         {
90dd15c272SArthur O'Dwyer         Cont cont;
91dd15c272SArthur O'Dwyer         std::stack stk(cont, Alloc(2));
92dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
935b8b8b5dSMarshall Clow         }
945b8b8b5dSMarshall Clow 
95dd15c272SArthur O'Dwyer         {
96dd15c272SArthur O'Dwyer         Cont cont;
97dd15c272SArthur O'Dwyer         std::stack stk(cont, ConvertibleToAlloc(2));
98dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
99dd15c272SArthur O'Dwyer         }
100dd15c272SArthur O'Dwyer 
101dd15c272SArthur O'Dwyer         {
102dd15c272SArthur O'Dwyer         Cont cont;
103dd15c272SArthur O'Dwyer         std::stack stk(std::move(cont), Alloc(2));
104dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
105dd15c272SArthur O'Dwyer         }
106dd15c272SArthur O'Dwyer 
107dd15c272SArthur O'Dwyer         {
108dd15c272SArthur O'Dwyer         Cont cont;
109dd15c272SArthur O'Dwyer         std::stack stk(std::move(cont), ConvertibleToAlloc(2));
110dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
111dd15c272SArthur O'Dwyer         }
112dd15c272SArthur O'Dwyer     }
113dd15c272SArthur O'Dwyer 
114dd15c272SArthur O'Dwyer     {
115dd15c272SArthur O'Dwyer         typedef short T;
116dd15c272SArthur O'Dwyer         typedef test_allocator<T> Alloc;
117dd15c272SArthur O'Dwyer         typedef std::list<T, Alloc> Cont;
118dd15c272SArthur O'Dwyer         typedef test_allocator<int> ConvertibleToAlloc;
119dd15c272SArthur O'Dwyer         static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
120dd15c272SArthur O'Dwyer                       !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
121dd15c272SArthur O'Dwyer 
122dd15c272SArthur O'Dwyer         {
123dd15c272SArthur O'Dwyer         std::stack<T, Cont> source;
124dd15c272SArthur O'Dwyer         std::stack stk(source, Alloc(2));
125dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
126dd15c272SArthur O'Dwyer         }
127dd15c272SArthur O'Dwyer 
128dd15c272SArthur O'Dwyer         {
129dd15c272SArthur O'Dwyer         std::stack<T, Cont> source;
130dd15c272SArthur O'Dwyer         std::stack stk(source, ConvertibleToAlloc(2));
131dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
132dd15c272SArthur O'Dwyer         }
133dd15c272SArthur O'Dwyer 
134dd15c272SArthur O'Dwyer         {
135dd15c272SArthur O'Dwyer         std::stack<T, Cont> source;
136dd15c272SArthur O'Dwyer         std::stack stk(std::move(source), Alloc(2));
137dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
138dd15c272SArthur O'Dwyer         }
139dd15c272SArthur O'Dwyer 
140dd15c272SArthur O'Dwyer         {
141dd15c272SArthur O'Dwyer         std::stack<T, Cont> source;
142dd15c272SArthur O'Dwyer         std::stack stk(std::move(source), ConvertibleToAlloc(2));
143dd15c272SArthur O'Dwyer         static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
144dd15c272SArthur O'Dwyer         }
145dd15c272SArthur O'Dwyer     }
1462df59c50SJF Bastien 
147*87f3ff3eSvarconst #if TEST_STD_VER >= 23
148f3aed369SNikolas Klauser     {
149f3aed369SNikolas Klauser         typedef short T;
150f3aed369SNikolas Klauser         typedef test_allocator<T> Alloc;
151f3aed369SNikolas Klauser         std::list<T> a;
152f3aed369SNikolas Klauser         {
153f3aed369SNikolas Klauser         std::stack s(a.begin(), a.end());
154f3aed369SNikolas Klauser         static_assert(std::is_same_v<decltype(s), std::stack<T>>);
155f3aed369SNikolas Klauser         }
156f3aed369SNikolas Klauser         {
157f3aed369SNikolas Klauser         std::stack s(a.begin(), a.end(), Alloc());
158f3aed369SNikolas Klauser         static_assert(std::is_same_v<decltype(s), std::stack<T, std::deque<T, Alloc>>>);
159f3aed369SNikolas Klauser         }
160f3aed369SNikolas Klauser     }
161*87f3ff3eSvarconst 
162*87f3ff3eSvarconst     {
163*87f3ff3eSvarconst       {
164*87f3ff3eSvarconst         std::stack c(std::from_range, std::array<int, 0>());
165*87f3ff3eSvarconst         static_assert(std::is_same_v<decltype(c), std::stack<int>>);
166*87f3ff3eSvarconst       }
167*87f3ff3eSvarconst 
168*87f3ff3eSvarconst       {
169*87f3ff3eSvarconst         using Alloc = test_allocator<int>;
170*87f3ff3eSvarconst         std::stack c(std::from_range, std::array<int, 0>(), Alloc());
171*87f3ff3eSvarconst         static_assert(std::is_same_v<decltype(c), std::stack<int, std::deque<int, Alloc>>>);
172*87f3ff3eSvarconst       }
173*87f3ff3eSvarconst     }
174f3aed369SNikolas Klauser #endif
175f3aed369SNikolas Klauser 
176*87f3ff3eSvarconst     ContainerAdaptorDeductionGuidesSfinaeAway<std::stack, std::stack<int>>();
177*87f3ff3eSvarconst 
1782df59c50SJF Bastien     return 0;
1795b8b8b5dSMarshall Clow }
180