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 // <array> 10 // UNSUPPORTED: c++03, c++11, c++14, c++17 11 12 // template <typename T, size_t Size> 13 // constexpr auto to_array(T (&arr)[Size]) 14 // -> array<remove_cv_t<T>, Size>; 15 16 // template <typename T, size_t Size> 17 // constexpr auto to_array(T (&&arr)[Size]) 18 // -> array<remove_cv_t<T>, Size>; 19 20 #include <array> 21 #include <cassert> 22 23 #include "test_macros.h" 24 #include "MoveOnly.h" 25 26 constexpr bool tests() 27 { 28 // Test deduced type. 29 { 30 auto arr = std::to_array({1, 2, 3}); 31 ASSERT_SAME_TYPE(decltype(arr), std::array<int, 3>); 32 assert(arr[0] == 1); 33 assert(arr[1] == 2); 34 assert(arr[2] == 3); 35 } 36 37 { 38 const long l1 = 42; 39 auto arr = std::to_array({1L, 4L, 9L, l1}); 40 ASSERT_SAME_TYPE(decltype(arr)::value_type, long); 41 static_assert(arr.size() == 4, ""); 42 assert(arr[0] == 1); 43 assert(arr[1] == 4); 44 assert(arr[2] == 9); 45 assert(arr[3] == l1); 46 } 47 48 { 49 auto arr = std::to_array("meow"); 50 ASSERT_SAME_TYPE(decltype(arr), std::array<char, 5>); 51 assert(arr[0] == 'm'); 52 assert(arr[1] == 'e'); 53 assert(arr[2] == 'o'); 54 assert(arr[3] == 'w'); 55 assert(arr[4] == '\0'); 56 } 57 58 { 59 double source[3] = {4.0, 5.0, 6.0}; 60 auto arr = std::to_array(source); 61 ASSERT_SAME_TYPE(decltype(arr), std::array<double, 3>); 62 assert(arr[0] == 4.0); 63 assert(arr[1] == 5.0); 64 assert(arr[2] == 6.0); 65 } 66 67 { 68 double source[3] = {4.0, 5.0, 6.0}; 69 auto arr = std::to_array(std::move(source)); 70 ASSERT_SAME_TYPE(decltype(arr), std::array<double, 3>); 71 assert(arr[0] == 4.0); 72 assert(arr[1] == 5.0); 73 assert(arr[2] == 6.0); 74 } 75 76 { 77 MoveOnly source[] = {MoveOnly{0}, MoveOnly{1}, MoveOnly{2}}; 78 79 auto arr = std::to_array(std::move(source)); 80 ASSERT_SAME_TYPE(decltype(arr), std::array<MoveOnly, 3>); 81 for (int i = 0; i < 3; ++i) 82 assert(arr[i].get() == i && source[i].get() == 0); 83 } 84 85 // Test C99 compound literal. 86 { 87 auto arr = std::to_array((int[]){3, 4}); 88 ASSERT_SAME_TYPE(decltype(arr), std::array<int, 2>); 89 assert(arr[0] == 3); 90 assert(arr[1] == 4); 91 } 92 93 // Test explicit type. 94 { 95 auto arr = std::to_array<long>({1, 2, 3}); 96 ASSERT_SAME_TYPE(decltype(arr), std::array<long, 3>); 97 assert(arr[0] == 1); 98 assert(arr[1] == 2); 99 assert(arr[2] == 3); 100 } 101 102 { 103 struct A { 104 int a; 105 double b; 106 }; 107 108 auto arr = std::to_array<A>({{3, .1}}); 109 ASSERT_SAME_TYPE(decltype(arr), std::array<A, 1>); 110 assert(arr[0].a == 3); 111 assert(arr[0].b == .1); 112 } 113 114 return true; 115 } 116 117 int main(int, char**) 118 { 119 tests(); 120 static_assert(tests(), ""); 121 return 0; 122 } 123