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 // <forward_list>
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
13 //    forward_list(InputIterator, InputIterator, Allocator = Allocator())
14 //    -> forward_list<typename iterator_traits<InputIterator>::value_type, Allocator>;
15 //
16 
17 #include <algorithm>
18 #include <forward_list>
19 #include <iterator>
20 #include <cassert>
21 #include <cstddef>
22 #include <climits> // INT_MAX
23 
24 #include "deduction_guides_sfinae_checks.h"
25 #include "test_macros.h"
26 #include "test_iterators.h"
27 #include "test_allocator.h"
28 
29 struct A {};
30 
main(int,char **)31 int main(int, char**)
32 {
33 
34 //  Test the explicit deduction guides
35     {
36     const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
37     std::forward_list fwl(std::begin(arr), std::end(arr));
38 
39     static_assert(std::is_same_v<decltype(fwl), std::forward_list<int>>, "");
40     assert(std::equal(fwl.begin(), fwl.end(), std::begin(arr), std::end(arr)));
41     }
42 
43     {
44     const long arr[] = {INT_MAX, 1L, 2L, 3L };
45     std::forward_list fwl(std::begin(arr), std::end(arr), std::allocator<long>());
46     static_assert(std::is_same_v<decltype(fwl)::value_type, long>, "");
47     assert(std::distance(fwl.begin(), fwl.end()) == 4); // no size for forward_list
48     auto it = fwl.begin();
49     assert(*it++ == INT_MAX);
50     assert(*it++ == 1L);
51     assert(*it++ == 2L);
52     }
53 
54 //  Test the implicit deduction guides
55 
56     {
57 //  We don't expect this one to work.
58 //  std::forward_list fwl(std::allocator<int>()); // deque (allocator &)
59     }
60 
61     {
62     std::forward_list fwl(1, A{}); // deque (size_type, T)
63     static_assert(std::is_same_v<decltype(fwl)::value_type, A>, "");
64     static_assert(std::is_same_v<decltype(fwl)::allocator_type, std::allocator<A>>, "");
65     assert(std::distance(fwl.begin(), fwl.end()) == 1); // no size for forward_list
66     }
67 
68     {
69     std::forward_list fwl(1, A{}, test_allocator<A>()); // deque (size_type, T, allocator)
70     static_assert(std::is_same_v<decltype(fwl)::value_type, A>, "");
71     static_assert(std::is_same_v<decltype(fwl)::allocator_type, test_allocator<A>>, "");
72     assert(std::distance(fwl.begin(), fwl.end()) == 1); // no size for forward_list
73     }
74 
75     {
76     std::forward_list fwl{1U, 2U, 3U, 4U, 5U}; // deque(initializer-list)
77     static_assert(std::is_same_v<decltype(fwl)::value_type, unsigned>, "");
78     assert(std::distance(fwl.begin(), fwl.end()) == 5); // no size for forward_list
79     auto it = fwl.begin();
80     std::advance(it, 2);
81     assert(*it == 3U);
82     }
83 
84     {
85     std::forward_list fwl({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // deque(initializer-list, allocator)
86     static_assert(std::is_same_v<decltype(fwl)::value_type, double>, "");
87     static_assert(std::is_same_v<decltype(fwl)::allocator_type, test_allocator<double>>, "");
88     assert(std::distance(fwl.begin(), fwl.end()) == 4); // no size for forward_list
89     auto it = fwl.begin();
90     std::advance(it, 3);
91     assert(*it == 4.0);
92     }
93 
94     {
95     std::forward_list<long double> source;
96     std::forward_list fwl(source); // deque(deque &)
97     static_assert(std::is_same_v<decltype(fwl)::value_type, long double>, "");
98     static_assert(std::is_same_v<decltype(fwl)::allocator_type, std::allocator<long double>>, "");
99     assert(std::distance(fwl.begin(), fwl.end()) == 0); // no size for forward_list
100     }
101 
102     {
103         typedef test_allocator<short> Alloc;
104         typedef test_allocator<int> ConvertibleToAlloc;
105 
106         {
107         std::forward_list<short, Alloc> source;
108         std::forward_list fwl(source, Alloc(2));
109         static_assert(std::is_same_v<decltype(fwl), decltype(source)>);
110         }
111 
112         {
113         std::forward_list<short, Alloc> source;
114         std::forward_list fwl(source, ConvertibleToAlloc(2));
115         static_assert(std::is_same_v<decltype(fwl), decltype(source)>);
116         }
117 
118         {
119         std::forward_list<short, Alloc> source;
120         std::forward_list fwl(std::move(source), Alloc(2));
121         static_assert(std::is_same_v<decltype(fwl), decltype(source)>);
122         }
123 
124         {
125         std::forward_list<short, Alloc> source;
126         std::forward_list fwl(std::move(source), ConvertibleToAlloc(2));
127         static_assert(std::is_same_v<decltype(fwl), decltype(source)>);
128         }
129     }
130 
131     SequenceContainerDeductionGuidesSfinaeAway<std::forward_list, std::forward_list<int>>();
132 
133     return 0;
134 }
135