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 // <forward_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 // deque(InputIterator, InputIterator, Allocator = Allocator()) 17 // -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; 18 // 19 20 21 #include <forward_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 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::forward_list fwl(std::begin(arr), std::end(arr)); 40 41 static_assert(std::is_same_v<decltype(fwl), std::forward_list<int>>, ""); 42 assert(std::equal(fwl.begin(), fwl.end(), std::begin(arr), std::end(arr))); 43 } 44 45 { 46 const long arr[] = {INT_MAX, 1L, 2L, 3L }; 47 std::forward_list fwl(std::begin(arr), std::end(arr), std::allocator<long>()); 48 static_assert(std::is_same_v<decltype(fwl)::value_type, long>, ""); 49 assert(std::distance(fwl.begin(), fwl.end()) == 4); // no size for forward_list 50 auto it = fwl.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::forward_list fwl(std::allocator<int>()); // deque (allocator &) 61 } 62 63 { 64 std::forward_list fwl(1, A{}); // deque (size_type, T) 65 static_assert(std::is_same_v<decltype(fwl)::value_type, A>, ""); 66 static_assert(std::is_same_v<decltype(fwl)::allocator_type, std::allocator<A>>, ""); 67 assert(std::distance(fwl.begin(), fwl.end()) == 1); // no size for forward_list 68 } 69 70 { 71 std::forward_list fwl(1, A{}, test_allocator<A>()); // deque (size_type, T, allocator) 72 static_assert(std::is_same_v<decltype(fwl)::value_type, A>, ""); 73 static_assert(std::is_same_v<decltype(fwl)::allocator_type, test_allocator<A>>, ""); 74 assert(std::distance(fwl.begin(), fwl.end()) == 1); // no size for forward_list 75 } 76 77 { 78 std::forward_list fwl{1U, 2U, 3U, 4U, 5U}; // deque(initializer-list) 79 static_assert(std::is_same_v<decltype(fwl)::value_type, unsigned>, ""); 80 assert(std::distance(fwl.begin(), fwl.end()) == 5); // no size for forward_list 81 auto it = fwl.begin(); 82 std::advance(it, 2); 83 assert(*it == 3U); 84 } 85 86 { 87 std::forward_list fwl({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // deque(initializer-list, allocator) 88 static_assert(std::is_same_v<decltype(fwl)::value_type, double>, ""); 89 static_assert(std::is_same_v<decltype(fwl)::allocator_type, test_allocator<double>>, ""); 90 assert(std::distance(fwl.begin(), fwl.end()) == 4); // no size for forward_list 91 auto it = fwl.begin(); 92 std::advance(it, 3); 93 assert(*it == 4.0); 94 } 95 96 { 97 std::forward_list<long double> source; 98 std::forward_list fwl(source); // deque(deque &) 99 static_assert(std::is_same_v<decltype(fwl)::value_type, long double>, ""); 100 static_assert(std::is_same_v<decltype(fwl)::allocator_type, std::allocator<long double>>, ""); 101 assert(std::distance(fwl.begin(), fwl.end()) == 0); // no size for forward_list 102 } 103 } 104