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

source code of libcxx/test/std/containers/sequences/forwardlist/forwardlist.cons/deduct.pass.cpp