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
26constexpr bool tests() {
27 // Test deduced type.
28 {
29 auto arr = std::to_array({1, 2, 3});
30 ASSERT_SAME_TYPE(decltype(arr), std::array<int, 3>);
31 assert(arr[0] == 1);
32 assert(arr[1] == 2);
33 assert(arr[2] == 3);
34 }
35
36 {
37 const long l1 = 42;
38 auto arr = std::to_array({1L, 4L, 9L, l1});
39 ASSERT_SAME_TYPE(decltype(arr)::value_type, long);
40 static_assert(arr.size() == 4, "");
41 assert(arr[0] == 1);
42 assert(arr[1] == 4);
43 assert(arr[2] == 9);
44 assert(arr[3] == l1);
45 }
46
47 {
48 auto arr = std::to_array("meow");
49 ASSERT_SAME_TYPE(decltype(arr), std::array<char, 5>);
50 assert(arr[0] == 'm');
51 assert(arr[1] == 'e');
52 assert(arr[2] == 'o');
53 assert(arr[3] == 'w');
54 assert(arr[4] == '\0');
55 }
56
57 {
58 double source[3] = {4.0, 5.0, 6.0};
59 auto arr = std::to_array(source);
60 ASSERT_SAME_TYPE(decltype(arr), std::array<double, 3>);
61 assert(arr[0] == 4.0);
62 assert(arr[1] == 5.0);
63 assert(arr[2] == 6.0);
64 }
65
66 {
67 double source[3] = {4.0, 5.0, 6.0};
68 auto arr = std::to_array(std::move(source));
69 ASSERT_SAME_TYPE(decltype(arr), std::array<double, 3>);
70 assert(arr[0] == 4.0);
71 assert(arr[1] == 5.0);
72 assert(arr[2] == 6.0);
73 }
74
75 {
76 MoveOnly source[] = {MoveOnly{0}, MoveOnly{1}, MoveOnly{2}};
77
78 auto arr = std::to_array(std::move(source));
79 ASSERT_SAME_TYPE(decltype(arr), std::array<MoveOnly, 3>);
80 for (int i = 0; i < 3; ++i)
81 assert(arr[i].get() == i && source[i].get() == 0);
82 }
83
84#ifndef _MSVC_STL_VERSION
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#endif // ! _MSVC_STL_VERSION
93
94 // Test explicit type.
95 {
96 auto arr = std::to_array<long>({1, 2, 3});
97 ASSERT_SAME_TYPE(decltype(arr), std::array<long, 3>);
98 assert(arr[0] == 1);
99 assert(arr[1] == 2);
100 assert(arr[2] == 3);
101 }
102
103 {
104 struct A {
105 int a;
106 double b;
107 };
108
109 auto arr = std::to_array<A>({{3, .1}});
110 ASSERT_SAME_TYPE(decltype(arr), std::array<A, 1>);
111 assert(arr[0].a == 3);
112 assert(arr[0].b == .1);
113 }
114
115 return true;
116}
117
118int main(int, char**) {
119 tests();
120 static_assert(tests(), "");
121 return 0;
122}
123

source code of libcxx/test/std/containers/sequences/array/array.creation/to_array.pass.cpp