Skip to content

Commit 55a67fd

Browse files
committed
🐛 Fix dynamic span construct from array
Problem: - When a dynamic span is constructed from an array, the size is not populated. Solution: - Initialize the base (size) properly.
1 parent 58c741d commit 55a67fd

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

include/stdx/span.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ class span : public detail::span_base<T, Extent> {
9898
template <typename U, std::size_t N>
9999
// NOLINTNEXTLINE(google-explicit-constructor)
100100
constexpr span(std::array<U, N> &arr LIFETIMEBOUND) noexcept
101-
: ptr{std::data(arr)} {
101+
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
102102
static_assert(Extent == dynamic_extent or Extent <= N,
103103
"Span extends beyond available storage");
104104
}
105105

106106
template <typename U, std::size_t N>
107107
// NOLINTNEXTLINE(google-explicit-constructor)
108108
constexpr span(std::array<U, N> const &arr LIFETIMEBOUND) noexcept
109-
: ptr{std::data(arr)} {
109+
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
110110
static_assert(Extent == dynamic_extent or Extent <= N,
111111
"Span extends beyond available storage");
112112
}

test/span.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,22 @@ TEST_CASE("span is constructible from std::array (non const data)", "[span]") {
141141
STATIC_REQUIRE(std::size(s) == 4);
142142
}
143143

144+
TEST_CASE("dynamic span is constructible from std::array (const data)",
145+
"[span]") {
146+
constexpr static auto a = std::array{1, 2, 3, 4};
147+
auto s = [](stdx::span<int const> s) { return s; }(a);
148+
CHECK(std::data(s) == std::data(a));
149+
CHECK(std::size(s) == std::size(a));
150+
}
151+
152+
TEST_CASE("dynamic span is constructible from std::array (non const data)",
153+
"[span]") {
154+
auto a = std::array{1, 2, 3, 4};
155+
auto s = [](stdx::span<int> s) { return s; }(a);
156+
CHECK(std::data(s) == std::data(a));
157+
CHECK(std::size(s) == std::size(a));
158+
}
159+
144160
TEST_CASE("span is constructible from C-style array (const data)", "[span]") {
145161
constexpr static int a[] = {1, 2, 3, 4};
146162
constexpr auto s = stdx::span{a};

0 commit comments

Comments
 (0)