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