Skip to content

Commit 4eb6a2b

Browse files
authored
Merge pull request #235 from elbeno/fix-dynamic-span-construct
🐛 Fix dynamic span construct from array
2 parents 58c741d + 9be4e7c commit 4eb6a2b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

include/stdx/span.hpp

Lines changed: 4 additions & 3 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
}
@@ -116,7 +116,8 @@ class span : public detail::span_base<T, Extent> {
116116

117117
template <std::size_t N>
118118
// NOLINTNEXTLINE(google-explicit-constructor)
119-
constexpr span(arr_t<N> arr LIFETIMEBOUND) noexcept : ptr{std::data(arr)} {
119+
constexpr span(arr_t<N> arr LIFETIMEBOUND) noexcept
120+
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
120121
static_assert(Extent == dynamic_extent or Extent <= N,
121122
"Span extends beyond available storage");
122123
}

test/span.cpp

Lines changed: 32 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> x) { return x; }(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> x) { return x; }(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};
@@ -159,6 +175,22 @@ TEST_CASE("span is constructible from C-style array (non const data)",
159175
CHECK(std::size(s) == std::size(a));
160176
}
161177

178+
TEST_CASE("dynamic span is constructible from C-style array (const data)",
179+
"[span]") {
180+
constexpr static int a[] = {1, 2, 3, 4};
181+
auto s = [](stdx::span<int const> x) { return x; }(a);
182+
CHECK(std::data(s) == std::data(a));
183+
CHECK(std::size(s) == std::size(a));
184+
}
185+
186+
TEST_CASE("dynamic span is constructible from C-style array (non const data)",
187+
"[span]") {
188+
int a[] = {1, 2, 3, 4};
189+
auto s = [](stdx::span<int> x) { return x; }(a);
190+
CHECK(std::data(s) == std::data(a));
191+
CHECK(std::size(s) == std::size(a));
192+
}
193+
162194
TEST_CASE("dynamic span is implicitly constructible from range (const data)",
163195
"[span]") {
164196
std::vector const v{1, 2, 3, 4};

0 commit comments

Comments
 (0)