Skip to content

Commit 86aae78

Browse files
committed
[libc++] [P0325] Implement to_array from LFTS with updates.
Summary: This patch implements https://wg21.link/P0325. Please mind that at it is my first contribution to libc++, so I may have forgotten to abide to some conventions. Reviewers: EricWF, mclow.lists, ldionne, lichray Reviewed By: ldionne, lichray Subscribers: lichray, dexonsmith, zoecarver, christof, ldionne, libcxx-commits Tags: #libc Differential Revision: https://reviews.llvm.org/D69882
1 parent 57b0d33 commit 86aae78

File tree

9 files changed

+248
-1
lines changed

9 files changed

+248
-1
lines changed

libcxx/docs/FeatureTestMacroTable.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ Status
199199
``__cpp_lib_ranges`` *unimplemented*
200200
------------------------------------------------- -----------------
201201
``__cpp_lib_three_way_comparison`` *unimplemented*
202+
------------------------------------------------- -----------------
203+
``__cpp_lib_to_array`` ``201907L``
202204
================================================= =================
203205

204206

libcxx/include/array

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,47 @@ get(const array<_Tp, _Size>&& __a) _NOEXCEPT
479479

480480
#endif // !_LIBCPP_CXX03_LANG
481481

482+
#if _LIBCPP_STD_VER > 17
483+
484+
template <typename _Tp, size_t _Size, size_t... _Index>
485+
_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
486+
__to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) {
487+
return {{__arr[_Index]...}};
488+
}
489+
490+
template <typename _Tp, size_t _Size, size_t... _Index>
491+
_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
492+
__to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) {
493+
return {{_VSTD::move(__arr[_Index])...}};
494+
}
495+
496+
template <typename _Tp, size_t _Size>
497+
_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
498+
to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) {
499+
static_assert(
500+
!is_array_v<_Tp>,
501+
"[array.creation]/1: to_array does not accept multidimensional arrays.");
502+
static_assert(
503+
is_constructible_v<_Tp, _Tp&>,
504+
"[array.creation]/1: to_array requires copy constructible elements.");
505+
return __to_array_lvalue_impl(__arr, make_index_sequence<_Size>());
506+
}
507+
508+
template <typename _Tp, size_t _Size>
509+
_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
510+
to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) {
511+
static_assert(
512+
!is_array_v<_Tp>,
513+
"[array.creation]/4: to_array does not accept multidimensional arrays.");
514+
static_assert(
515+
is_move_constructible_v<_Tp>,
516+
"[array.creation]/4: to_array requires move constructible elements.");
517+
return __to_array_rvalue_impl(_VSTD::move(__arr),
518+
make_index_sequence<_Size>());
519+
}
520+
521+
#endif // _LIBCPP_STD_VER > 17
522+
482523
_LIBCPP_END_NAMESPACE_STD
483524

484525
#endif // _LIBCPP_ARRAY

libcxx/include/version

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ __cpp_lib_shared_timed_mutex 201402L <shared_mutex>
101101
__cpp_lib_string_udls 201304L <string>
102102
__cpp_lib_string_view 201606L <string> <string_view>
103103
__cpp_lib_three_way_comparison 201711L <compare>
104+
__cpp_lib_to_array 201907L <array>
104105
__cpp_lib_to_chars 201611L <utility>
105106
__cpp_lib_transformation_trait_aliases 201304L <type_traits>
106107
__cpp_lib_transparent_operators 201510L <functional>
@@ -233,6 +234,7 @@ __cpp_lib_void_t 201411L <type_traits>
233234
# endif
234235
// # define __cpp_lib_list_remove_return_type 201806L
235236
// # define __cpp_lib_ranges 201811L
237+
# define __cpp_lib_to_array 201907L
236238
// # define __cpp_lib_three_way_comparison 201711L
237239
#endif
238240

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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++98, c++03, c++11, c++14, c++17
11+
12+
#include <array>
13+
14+
#include "test_macros.h"
15+
#include "MoveOnly.h"
16+
17+
int main(int, char**) {
18+
{
19+
char source[3][6] = {"hi", "world"};
20+
std::to_array(source); // expected-error {{here}}
21+
}
22+
23+
{
24+
MoveOnly mo[] = {MoveOnly{3}};
25+
std::to_array(mo); // expected-error {{here}}
26+
}
27+
28+
{
29+
const MoveOnly cmo[] = {MoveOnly{3}};
30+
std::to_array(std::move(cmo)); // expected-error {{here}}
31+
}
32+
33+
return 0;
34+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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++98, 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+
int main(int, char**) {
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+
// Test C99 compound literal.
85+
{
86+
auto arr = std::to_array((int[]){3, 4});
87+
ASSERT_SAME_TYPE(decltype(arr), std::array<int, 2>);
88+
assert(arr[0] == 3);
89+
assert(arr[1] == 4);
90+
}
91+
92+
// Test explicit type.
93+
{
94+
auto arr = std::to_array<long>({1, 2, 3});
95+
ASSERT_SAME_TYPE(decltype(arr), std::array<long, 3>);
96+
assert(arr[0] == 1);
97+
assert(arr[1] == 2);
98+
assert(arr[2] == 3);
99+
}
100+
101+
{
102+
struct A {
103+
int a;
104+
double b;
105+
};
106+
107+
auto arr = std::to_array<A>({{3, .1}});
108+
ASSERT_SAME_TYPE(decltype(arr), std::array<A, 1>);
109+
assert(arr[0].a == 3);
110+
assert(arr[0].b == .1);
111+
}
112+
113+
// Test constexpr.
114+
{
115+
constexpr std::array<int, 3> arr = std::to_array({1, 2, 3});
116+
static_assert(arr[0] == 1);
117+
static_assert(arr[1] == 2);
118+
static_assert(arr[2] == 3);
119+
}
120+
121+
return 0;
122+
}

libcxx/test/std/language.support/support.limits/support.limits.general/array.version.pass.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
__cpp_lib_array_constexpr 201603L [C++17]
1818
__cpp_lib_constexpr_misc 201811L [C++2a]
1919
__cpp_lib_nonmember_container_access 201411L [C++17]
20+
__cpp_lib_to_array 201907L [C++2a]
2021
*/
2122

2223
#include <array>
@@ -36,6 +37,10 @@
3637
# error "__cpp_lib_nonmember_container_access should not be defined before c++17"
3738
# endif
3839

40+
# ifdef __cpp_lib_to_array
41+
# error "__cpp_lib_to_array should not be defined before c++2a"
42+
# endif
43+
3944
#elif TEST_STD_VER == 14
4045

4146
# ifdef __cpp_lib_array_constexpr
@@ -50,6 +55,10 @@
5055
# error "__cpp_lib_nonmember_container_access should not be defined before c++17"
5156
# endif
5257

58+
# ifdef __cpp_lib_to_array
59+
# error "__cpp_lib_to_array should not be defined before c++2a"
60+
# endif
61+
5362
#elif TEST_STD_VER == 17
5463

5564
# ifndef __cpp_lib_array_constexpr
@@ -70,6 +79,10 @@
7079
# error "__cpp_lib_nonmember_container_access should have the value 201411L in c++17"
7180
# endif
7281

82+
# ifdef __cpp_lib_to_array
83+
# error "__cpp_lib_to_array should not be defined before c++2a"
84+
# endif
85+
7386
#elif TEST_STD_VER > 17
7487

7588
# ifndef __cpp_lib_array_constexpr
@@ -99,6 +112,13 @@
99112
# error "__cpp_lib_nonmember_container_access should have the value 201411L in c++2a"
100113
# endif
101114

115+
# ifndef __cpp_lib_to_array
116+
# error "__cpp_lib_to_array should be defined in c++2a"
117+
# endif
118+
# if __cpp_lib_to_array != 201907L
119+
# error "__cpp_lib_to_array should have the value 201907L in c++2a"
120+
# endif
121+
102122
#endif // TEST_STD_VER > 17
103123

104124
int main(int, char**) { return 0; }

libcxx/test/std/language.support/support.limits/support.limits.general/version.version.pass.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
__cpp_lib_string_udls 201304L [C++14]
8989
__cpp_lib_string_view 201606L [C++17]
9090
__cpp_lib_three_way_comparison 201711L [C++2a]
91+
__cpp_lib_to_array 201907L [C++2a]
9192
__cpp_lib_to_chars 201611L [C++17]
9293
__cpp_lib_transformation_trait_aliases 201304L [C++14]
9394
__cpp_lib_transparent_operators 201210L [C++14]
@@ -402,6 +403,10 @@
402403
# error "__cpp_lib_three_way_comparison should not be defined before c++2a"
403404
# endif
404405

406+
# ifdef __cpp_lib_to_array
407+
# error "__cpp_lib_to_array should not be defined before c++2a"
408+
# endif
409+
405410
# ifdef __cpp_lib_to_chars
406411
# error "__cpp_lib_to_chars should not be defined before c++17"
407412
# endif
@@ -794,6 +799,10 @@
794799
# error "__cpp_lib_three_way_comparison should not be defined before c++2a"
795800
# endif
796801

802+
# ifdef __cpp_lib_to_array
803+
# error "__cpp_lib_to_array should not be defined before c++2a"
804+
# endif
805+
797806
# ifdef __cpp_lib_to_chars
798807
# error "__cpp_lib_to_chars should not be defined before c++17"
799808
# endif
@@ -1390,6 +1399,10 @@
13901399
# error "__cpp_lib_three_way_comparison should not be defined before c++2a"
13911400
# endif
13921401

1402+
# ifdef __cpp_lib_to_array
1403+
# error "__cpp_lib_to_array should not be defined before c++2a"
1404+
# endif
1405+
13931406
# if !defined(_LIBCPP_VERSION)
13941407
# ifndef __cpp_lib_to_chars
13951408
# error "__cpp_lib_to_chars should be defined in c++17"
@@ -2136,6 +2149,13 @@
21362149
# endif
21372150
# endif
21382151

2152+
# ifndef __cpp_lib_to_array
2153+
# error "__cpp_lib_to_array should be defined in c++2a"
2154+
# endif
2155+
# if __cpp_lib_to_array != 201907L
2156+
# error "__cpp_lib_to_array should have the value 201907L in c++2a"
2157+
# endif
2158+
21392159
# if !defined(_LIBCPP_VERSION)
21402160
# ifndef __cpp_lib_to_chars
21412161
# error "__cpp_lib_to_chars should be defined in c++2a"

libcxx/utils/generate_feature_test_macro_components.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,12 @@ def add_version_header(tc):
586586
},
587587
"headers": ["bit"],
588588
},
589+
{"name": "__cpp_lib_to_array",
590+
"values": {
591+
"c++2a": 201907L,
592+
},
593+
"headers": ["array"],
594+
},
589595
]], key=lambda tc: tc["name"])
590596

591597
def get_std_dialects():

libcxx/www/cxx2a_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ <h3>Paper Status</h3>
157157
<tr><td><a href="https://wg21.link/P1464R1">P1464R1</a></td><td>LWG</td><td>Mandating the Standard Library: Clause 22 - Iterators library</td><td>Kona</td><td>Complete</td><td>9.0</td></tr>
158158

159159
<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>
160-
<tr><td><a href="https://wg21.link/P0325">P0325</a></td><td>LWG</td><td>to_array from LFTS with updates</td><td>Cologne</td><td></td><td></td></tr>
160+
<tr><td><a href="https://wg21.link/P0325">P0325</a></td><td>LWG</td><td>to_array from LFTS with updates</td><td>Cologne</td><td>Complete</td><td>10.0</td></tr>
161161
<tr><td><a href="https://wg21.link/P0408">P0408</a></td><td>LWG</td><td>Efficient Access to basic_stringbuf ’s Buffer</td><td>Cologne</td><td></td><td></td></tr>
162162
<tr><td><a href="https://wg21.link/P0466">P0466</a></td><td>LWG</td><td>Layout-compatibility and Pointer-interconvertibility Traits</td><td>Cologne</td><td></td><td></td></tr>
163163
<tr><td><a href="https://wg21.link/P0553">P0553</a></td><td>LWG</td><td>Bit operations</td><td>Cologne</td><td>Complete</td><td>9.0</td></tr>

0 commit comments

Comments
 (0)