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