| 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 | |
| 34 | int main(int, char**) { |
| 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: arr), std::end(arr: 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: arr), std::end(arr: 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(i&: it, n: 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 | #if TEST_STD_VER >= 23 |
| 133 | { |
| 134 | { |
| 135 | std::list c(std::from_range, std::array<int, 0>()); |
| 136 | static_assert(std::is_same_v<decltype(c), std::list<int>>); |
| 137 | } |
| 138 | |
| 139 | { |
| 140 | using Alloc = test_allocator<int>; |
| 141 | std::list c(std::from_range, std::array<int, 0>(), Alloc()); |
| 142 | static_assert(std::is_same_v<decltype(c), std::list<int, Alloc>>); |
| 143 | } |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | SequenceContainerDeductionGuidesSfinaeAway<std::list, std::list<int>>(); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |