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>;
175b8b8b5dSMarshall Clow
185b8b8b5dSMarshall Clow
19*f3aed369SNikolas Klauser #include <array>
205b8b8b5dSMarshall Clow #include <stack>
21dd15c272SArthur O'Dwyer #include <deque>
225b8b8b5dSMarshall Clow #include <vector>
235b8b8b5dSMarshall Clow #include <list>
245b8b8b5dSMarshall Clow #include <iterator>
255b8b8b5dSMarshall Clow #include <cassert>
265b8b8b5dSMarshall Clow #include <cstddef>
275b8b8b5dSMarshall Clow #include <climits> // INT_MAX
285b8b8b5dSMarshall Clow
2968072a71SKonstantin Varlamov #include "deduction_guides_sfinae_checks.h"
305b8b8b5dSMarshall Clow #include "test_macros.h"
315b8b8b5dSMarshall Clow #include "test_iterators.h"
325b8b8b5dSMarshall Clow #include "test_allocator.h"
335b8b8b5dSMarshall Clow
345b8b8b5dSMarshall Clow struct A {};
355b8b8b5dSMarshall Clow
main(int,char **)362df59c50SJF Bastien int main(int, char**)
375b8b8b5dSMarshall Clow {
385b8b8b5dSMarshall Clow
395b8b8b5dSMarshall Clow // Test the explicit deduction guides
405b8b8b5dSMarshall Clow {
415b8b8b5dSMarshall Clow std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
425b8b8b5dSMarshall Clow std::stack stk(v);
435b8b8b5dSMarshall Clow
445b8b8b5dSMarshall Clow static_assert(std::is_same_v<decltype(stk), std::stack<int, std::vector<int>>>, "");
455b8b8b5dSMarshall Clow assert(stk.size() == v.size());
465b8b8b5dSMarshall Clow assert(stk.top() == v.back());
475b8b8b5dSMarshall Clow }
485b8b8b5dSMarshall Clow
495b8b8b5dSMarshall Clow {
505b8b8b5dSMarshall Clow std::list<long, test_allocator<long>> l{10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
515b8b8b5dSMarshall Clow std::stack stk(l, test_allocator<long>(0,2)); // different allocator
525b8b8b5dSMarshall Clow static_assert(std::is_same_v<decltype(stk)::container_type, std::list<long, test_allocator<long>>>, "");
535b8b8b5dSMarshall Clow static_assert(std::is_same_v<decltype(stk)::value_type, long>, "");
545b8b8b5dSMarshall Clow assert(stk.size() == 10);
555b8b8b5dSMarshall Clow assert(stk.top() == 19);
565b8b8b5dSMarshall Clow // I'd like to assert that we've gotten the right allocator in the stack, but
575b8b8b5dSMarshall Clow // I don't know how to get at the underlying container.
585b8b8b5dSMarshall Clow }
595b8b8b5dSMarshall Clow
605b8b8b5dSMarshall Clow // Test the implicit deduction guides
615b8b8b5dSMarshall Clow
625b8b8b5dSMarshall Clow {
635b8b8b5dSMarshall Clow // We don't expect this one to work - no way to implicitly get value_type
645b8b8b5dSMarshall Clow // std::stack stk(std::allocator<int>()); // stack (allocator &)
655b8b8b5dSMarshall Clow }
665b8b8b5dSMarshall Clow
675b8b8b5dSMarshall Clow {
685b8b8b5dSMarshall Clow std::stack<A> source;
695b8b8b5dSMarshall Clow std::stack stk(source); // stack(stack &)
705b8b8b5dSMarshall Clow static_assert(std::is_same_v<decltype(stk)::value_type, A>, "");
715b8b8b5dSMarshall Clow static_assert(std::is_same_v<decltype(stk)::container_type, std::deque<A>>, "");
725b8b8b5dSMarshall Clow assert(stk.size() == 0);
735b8b8b5dSMarshall Clow }
745b8b8b5dSMarshall Clow
755b8b8b5dSMarshall Clow {
765b8b8b5dSMarshall Clow typedef short T;
77c479e0c9SLouis Dionne typedef test_allocator<T> Alloc;
78dd15c272SArthur O'Dwyer typedef std::list<T, Alloc> Cont;
79dd15c272SArthur O'Dwyer typedef test_allocator<int> ConvertibleToAlloc;
80dd15c272SArthur O'Dwyer static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
81dd15c272SArthur O'Dwyer !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
825b8b8b5dSMarshall Clow
83dd15c272SArthur O'Dwyer {
84dd15c272SArthur O'Dwyer Cont cont;
85dd15c272SArthur O'Dwyer std::stack stk(cont, Alloc(2));
86dd15c272SArthur O'Dwyer static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
875b8b8b5dSMarshall Clow }
885b8b8b5dSMarshall Clow
89dd15c272SArthur O'Dwyer {
90dd15c272SArthur O'Dwyer Cont cont;
91dd15c272SArthur O'Dwyer std::stack stk(cont, ConvertibleToAlloc(2));
92dd15c272SArthur O'Dwyer static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
93dd15c272SArthur O'Dwyer }
94dd15c272SArthur O'Dwyer
95dd15c272SArthur O'Dwyer {
96dd15c272SArthur O'Dwyer Cont cont;
97dd15c272SArthur O'Dwyer std::stack stk(std::move(cont), Alloc(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), ConvertibleToAlloc(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 {
109dd15c272SArthur O'Dwyer typedef short T;
110dd15c272SArthur O'Dwyer typedef test_allocator<T> Alloc;
111dd15c272SArthur O'Dwyer typedef std::list<T, Alloc> Cont;
112dd15c272SArthur O'Dwyer typedef test_allocator<int> ConvertibleToAlloc;
113dd15c272SArthur O'Dwyer static_assert(std::uses_allocator_v<Cont, ConvertibleToAlloc> &&
114dd15c272SArthur O'Dwyer !std::is_same_v<typename Cont::allocator_type, ConvertibleToAlloc>);
115dd15c272SArthur O'Dwyer
116dd15c272SArthur O'Dwyer {
117dd15c272SArthur O'Dwyer std::stack<T, Cont> source;
118dd15c272SArthur O'Dwyer std::stack stk(source, Alloc(2));
119dd15c272SArthur O'Dwyer static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
120dd15c272SArthur O'Dwyer }
121dd15c272SArthur O'Dwyer
122dd15c272SArthur O'Dwyer {
123dd15c272SArthur O'Dwyer std::stack<T, Cont> source;
124dd15c272SArthur O'Dwyer std::stack stk(source, ConvertibleToAlloc(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(std::move(source), Alloc(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), ConvertibleToAlloc(2));
137dd15c272SArthur O'Dwyer static_assert(std::is_same_v<decltype(stk), std::stack<T, Cont>>);
138dd15c272SArthur O'Dwyer }
139dd15c272SArthur O'Dwyer }
1402df59c50SJF Bastien
14168072a71SKonstantin Varlamov // Deduction guides should be SFINAE'd away when given:
14268072a71SKonstantin Varlamov // - a "bad" allocator (that is, a type not qualifying as an allocator);
14368072a71SKonstantin Varlamov // - an allocator instead of a container;
14468072a71SKonstantin Varlamov // - an allocator and a container that uses a different allocator.
14568072a71SKonstantin Varlamov {
14668072a71SKonstantin Varlamov using Cont = std::list<int>;
14768072a71SKonstantin Varlamov using Alloc = std::allocator<int>;
14868072a71SKonstantin Varlamov using DiffAlloc = test_allocator<int>;
149*f3aed369SNikolas Klauser using Iter = int;
15068072a71SKonstantin Varlamov
151*f3aed369SNikolas Klauser struct NotIter {};
152*f3aed369SNikolas Klauser struct NotAlloc {};
15368072a71SKonstantin Varlamov
154*f3aed369SNikolas Klauser static_assert(SFINAEs_away<std::stack, Alloc, Alloc>);
155*f3aed369SNikolas Klauser static_assert(SFINAEs_away<std::stack, Cont, NotAlloc>);
15668072a71SKonstantin Varlamov static_assert(SFINAEs_away<std::stack, Cont, DiffAlloc>);
157*f3aed369SNikolas Klauser static_assert(SFINAEs_away<std::stack, Iter, NotIter>);
158*f3aed369SNikolas Klauser #if TEST_STD_VER > 20
159*f3aed369SNikolas Klauser static_assert(SFINAEs_away<std::stack, Iter, NotIter, Alloc>);
160*f3aed369SNikolas Klauser static_assert(SFINAEs_away<std::stack, Iter, Iter, NotAlloc>);
161*f3aed369SNikolas Klauser #endif
16268072a71SKonstantin Varlamov }
16368072a71SKonstantin Varlamov
164*f3aed369SNikolas Klauser #if TEST_STD_VER > 20
165*f3aed369SNikolas Klauser {
166*f3aed369SNikolas Klauser typedef short T;
167*f3aed369SNikolas Klauser typedef test_allocator<T> Alloc;
168*f3aed369SNikolas Klauser std::list<T> a;
169*f3aed369SNikolas Klauser {
170*f3aed369SNikolas Klauser std::stack s(a.begin(), a.end());
171*f3aed369SNikolas Klauser static_assert(std::is_same_v<decltype(s), std::stack<T>>);
172*f3aed369SNikolas Klauser }
173*f3aed369SNikolas Klauser {
174*f3aed369SNikolas Klauser std::stack s(a.begin(), a.end(), Alloc());
175*f3aed369SNikolas Klauser static_assert(std::is_same_v<decltype(s), std::stack<T, std::deque<T, Alloc>>>);
176*f3aed369SNikolas Klauser }
177*f3aed369SNikolas Klauser }
178*f3aed369SNikolas Klauser #endif
179*f3aed369SNikolas Klauser
1802df59c50SJF Bastien return 0;
1815b8b8b5dSMarshall Clow }
182