1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <list>
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 // UNSUPPORTED: libcpp-no-deduction-guides
13
14
15 // template <class InputIterator, class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
16 // list(InputIterator, InputIterator, Allocator = Allocator())
17 // -> list<typename iterator_traits<InputIterator>::value_type, Allocator>;
18 //
19
20
21 #include <list>
22 #include <iterator>
23 #include <cassert>
24 #include <cstddef>
25 #include <climits> // INT_MAX
26
27 #include "test_macros.h"
28 #include "test_iterators.h"
29 #include "test_allocator.h"
30
31 struct A {};
32
main()33 int main()
34 {
35
36 // Test the explicit deduction guides
37 {
38 const int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
39 std::list lst(std::begin(arr), std::end(arr));
40
41 static_assert(std::is_same_v<decltype(lst), std::list<int>>, "");
42 assert(std::equal(lst.begin(), lst.end(), std::begin(arr), std::end(arr)));
43 }
44
45 {
46 const long arr[] = {INT_MAX, 1L, 2L, 3L };
47 std::list lst(std::begin(arr), std::end(arr), std::allocator<long>());
48 static_assert(std::is_same_v<decltype(lst)::value_type, long>, "");
49 assert(lst.size() == 4);
50 auto it = lst.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::list lst(std::allocator<int>()); // list (allocator &)
61 }
62
63 {
64 std::list lst(1, A{}); // list (size_type, T)
65 static_assert(std::is_same_v<decltype(lst)::value_type, A>, "");
66 static_assert(std::is_same_v<decltype(lst)::allocator_type, std::allocator<A>>, "");
67 assert(lst.size() == 1);
68 }
69
70 {
71 std::list lst(1, A{}, test_allocator<A>()); // list (size_type, T, allocator)
72 static_assert(std::is_same_v<decltype(lst)::value_type, A>, "");
73 static_assert(std::is_same_v<decltype(lst)::allocator_type, test_allocator<A>>, "");
74 assert(lst.size() == 1);
75 }
76
77 {
78 std::list lst{1U, 2U, 3U, 4U, 5U}; // list(initializer-list)
79 static_assert(std::is_same_v<decltype(lst)::value_type, unsigned>, "");
80 assert(lst.size() == 5);
81 auto it = lst.begin();
82 std::advance(it, 2);
83 assert(*it == 3U);
84 }
85
86 {
87 std::list lst({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // list(initializer-list, allocator)
88 static_assert(std::is_same_v<decltype(lst)::value_type, double>, "");
89 static_assert(std::is_same_v<decltype(lst)::allocator_type, test_allocator<double>>, "");
90 assert(lst.size() == 4);
91 auto it = lst.begin();
92 std::advance(it, 3);
93 assert(*it == 4.0);
94 }
95
96 {
97 std::list<long double> source;
98 std::list lst(source); // list(list &)
99 static_assert(std::is_same_v<decltype(lst)::value_type, long double>, "");
100 static_assert(std::is_same_v<decltype(lst)::allocator_type, std::allocator<long double>>, "");
101 assert(lst.size() == 0);
102 }
103 }
104