Skip to content

Commit

Permalink
[libc++] Implement P2447 "std::span convertible from std::initializer…
Browse files Browse the repository at this point in the history
…_list".
  • Loading branch information
Quuxplusone committed Oct 3, 2021
1 parent e462ff5 commit 59493f0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ module std [system] {
}
module span {
header "span"
export initializer_list
export ranges.__ranges.enable_borrowed_range
export version
}
Expand Down
9 changes: 9 additions & 0 deletions libcxx/include/span
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public:
constexpr span(const array<value_type, N>& arr) noexcept;
template<class R>
constexpr explicit(extent != dynamic_extent) span(R&& r);
constexpr explicit(extent != dynamic_extent) span(initializer_list<value_type> il) noexcept;
constexpr span(const span& other) noexcept = default;
template <class OtherElementType, size_t OtherExtent>
constexpr explicit(Extent != dynamic_extent) span(const span<OtherElementType, OtherExtent>& s) noexcept;
Expand Down Expand Up @@ -138,6 +139,7 @@ template<class R>
#include <__ranges/size.h>
#include <array> // for array
#include <cstddef> // for byte
#include <initializer_list>
#include <iterator> // for iterators
#include <limits>
#include <type_traits> // for remove_cv, etc
Expand Down Expand Up @@ -256,6 +258,10 @@ public:
}
#endif

constexpr explicit span(initializer_list<value_type> __il) requires is_const_v<element_type> : __data(__il.begin()) {
_LIBCPP_ASSERT(__il.size() == _Extent, "size mismatch in span's constructor (initializer list)");
}

template <class _OtherElementType>
_LIBCPP_INLINE_VISIBILITY
constexpr span(const span<_OtherElementType, _Extent>& __other,
Expand Down Expand Up @@ -440,6 +446,9 @@ public:
constexpr span(_Range&& __r) : __data{ranges::data(__r)}, __size{ranges::size(__r)} {}
# endif

constexpr span(initializer_list<value_type> __il) requires is_const_v<element_type>
: __data(__il.begin()), __size(__il.size()) {}

template <class _OtherElementType, size_t _OtherExtent>
_LIBCPP_INLINE_VISIBILITY
constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
Expand Down
2 changes: 0 additions & 2 deletions libcxx/test/std/containers/views/span.cons/array.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ constexpr bool testSpan()
assert(s4.data() == val && s4.size() == 2);

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

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ constexpr int countn(std::span<const Sink, N> sp) {

constexpr bool test() {
Sink a[10];
assert(count({a}) == 10);
assert(count({a, a+10}) == 10);
assert(countn<10>({a}) == 10);
assert(count({a}) == 1);
assert(count({a, a+10}) == 2);
assert(count({a, a+1, a+2}) == 3);
return true;
}

Expand Down

0 comments on commit 59493f0

Please sign in to comment.