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 // <vector> 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 // deque(InputIterator, InputIterator, Allocator = Allocator()) 16 // -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>; 17 // 18 19 20 #include <vector> 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::vector vec(std::begin(arr), std::end(arr)); 39 40 static_assert(std::is_same_v<decltype(vec), std::vector<int>>, ""); 41 assert(std::equal(vec.begin(), vec.end(), std::begin(arr), std::end(arr))); 42 } 43 44 { 45 const long arr[] = {INT_MAX, 1L, 2L, 3L }; 46 std::vector vec(std::begin(arr), std::end(arr), std::allocator<long>()); 47 static_assert(std::is_same_v<decltype(vec)::value_type, long>, ""); 48 assert(vec.size() == 4); 49 assert(vec[0] == INT_MAX); 50 assert(vec[1] == 1L); 51 assert(vec[2] == 2L); 52 } 53 54 // Test the implicit deduction guides 55 56 { 57 // We don't expect this one to work. 58 // std::vector vec(std::allocator<int>()); // vector (allocator &) 59 } 60 61 { 62 std::vector vec(1, A{}); // vector (size_type, T) 63 static_assert(std::is_same_v<decltype(vec)::value_type, A>, ""); 64 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<A>>, ""); 65 assert(vec.size() == 1); 66 } 67 68 { 69 std::vector vec(1, A{}, test_allocator<A>()); // vector (size_type, T, allocator) 70 static_assert(std::is_same_v<decltype(vec)::value_type, A>, ""); 71 static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<A>>, ""); 72 assert(vec.size() == 1); 73 } 74 75 { 76 std::vector vec{1U, 2U, 3U, 4U, 5U}; // vector(initializer-list) 77 static_assert(std::is_same_v<decltype(vec)::value_type, unsigned>, ""); 78 assert(vec.size() == 5); 79 assert(vec[2] == 3U); 80 } 81 82 { 83 std::vector vec({1.0, 2.0, 3.0, 4.0}, test_allocator<double>()); // vector(initializer-list, allocator) 84 static_assert(std::is_same_v<decltype(vec)::value_type, double>, ""); 85 static_assert(std::is_same_v<decltype(vec)::allocator_type, test_allocator<double>>, ""); 86 assert(vec.size() == 4); 87 assert(vec[3] == 4.0); 88 } 89 90 { 91 std::vector<long double> source; 92 std::vector vec(source); // vector(vector &) 93 static_assert(std::is_same_v<decltype(vec)::value_type, long double>, ""); 94 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<long double>>, ""); 95 assert(vec.size() == 0); 96 } 97 98 99 // A couple of vector<bool> tests, too! 100 { 101 std::vector vec(3, true); // vector(initializer-list) 102 static_assert(std::is_same_v<decltype(vec)::value_type, bool>, ""); 103 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, ""); 104 assert(vec.size() == 3); 105 assert(vec[0] && vec[1] && vec[2]); 106 } 107 108 { 109 std::vector<bool> source; 110 std::vector vec(source); // vector(vector &) 111 static_assert(std::is_same_v<decltype(vec)::value_type, bool>, ""); 112 static_assert(std::is_same_v<decltype(vec)::allocator_type, std::allocator<bool>>, ""); 113 assert(vec.size() == 0); 114 } 115 116 return 0; 117 } 118