Skip to content

Commit 59493f0

Browse files
committed
[libc++] Implement P2447 "std::span convertible from std::initializer_list".
1 parent e462ff5 commit 59493f0

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ module std [system] {
735735
}
736736
module span {
737737
header "span"
738+
export initializer_list
738739
export ranges.__ranges.enable_borrowed_range
739740
export version
740741
}

libcxx/include/span

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public:
6868
constexpr span(const array<value_type, N>& arr) noexcept;
6969
template<class R>
7070
constexpr explicit(extent != dynamic_extent) span(R&& r);
71+
constexpr explicit(extent != dynamic_extent) span(initializer_list<value_type> il) noexcept;
7172
constexpr span(const span& other) noexcept = default;
7273
template <class OtherElementType, size_t OtherExtent>
7374
constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
@@ -138,6 +139,7 @@ template<class R>
138139
#include <__ranges/size.h>
139140
#include <array> // for array
140141
#include <cstddef> // for byte
142+
#include <initializer_list>
141143
#include <iterator> // for iterators
142144
#include <limits>
143145
#include <type_traits> // for remove_cv, etc
@@ -256,6 +258,10 @@ public:
256258
}
257259
#endif
258260

261+
constexpr explicit span(initializer_list<value_type> __il) requires is_const_v<element_type> : __data(__il.begin()) {
262+
_LIBCPP_ASSERT(__il.size() == _Extent, "size mismatch in span's constructor (initializer list)");
263+
}
264+
259265
template <class _OtherElementType>
260266
_LIBCPP_INLINE_VISIBILITY
261267
constexpr span(const span<_OtherElementType, _Extent>& __other,
@@ -440,6 +446,9 @@ public:
440446
constexpr span(_Range&& __r) : __data{ranges::data(__r)}, __size{ranges::size(__r)} {}
441447
# endif
442448

449+
constexpr span(initializer_list<value_type> __il) requires is_const_v<element_type>
450+
: __data(__il.begin()), __size(__il.size()) {}
451+
443452
template <class _OtherElementType, size_t _OtherExtent>
444453
_LIBCPP_INLINE_VISIBILITY
445454
constexpr span(const span<_OtherElementType, _OtherExtent>& __other,

libcxx/test/std/containers/views/span.cons/array.pass.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ constexpr bool testSpan()
9494
assert(s4.data() == val && s4.size() == 2);
9595

9696
std::span<const int> s5 = {{1,2}};
97-
std::span<const int, 2> s6 = {{1,2}};
9897
assert(s5.size() == 2); // and it dangles
99-
assert(s6.size() == 2); // and it dangles
10098

10199
return true;
102100
}

libcxx/test/std/containers/views/span.cons/initializer_list.pass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ constexpr int countn(std::span<const Sink, N> sp) {
2828

2929
constexpr bool test() {
3030
Sink a[10];
31-
assert(count({a}) == 10);
32-
assert(count({a, a+10}) == 10);
33-
assert(countn<10>({a}) == 10);
31+
assert(count({a}) == 1);
32+
assert(count({a, a+10}) == 2);
33+
assert(count({a, a+1, a+2}) == 3);
3434
return true;
3535
}
3636

0 commit comments

Comments
 (0)