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