Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/stdx/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ class span : public detail::span_base<T, Extent> {
template <typename U, std::size_t N>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(std::array<U, N> &arr LIFETIMEBOUND) noexcept
: ptr{std::data(arr)} {
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
static_assert(Extent == dynamic_extent or Extent <= N,
"Span extends beyond available storage");
}

template <typename U, std::size_t N>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(std::array<U, N> const &arr LIFETIMEBOUND) noexcept
: ptr{std::data(arr)} {
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
static_assert(Extent == dynamic_extent or Extent <= N,
"Span extends beyond available storage");
}
Expand All @@ -116,7 +116,8 @@ class span : public detail::span_base<T, Extent> {

template <std::size_t N>
// NOLINTNEXTLINE(google-explicit-constructor)
constexpr span(arr_t<N> arr LIFETIMEBOUND) noexcept : ptr{std::data(arr)} {
constexpr span(arr_t<N> arr LIFETIMEBOUND) noexcept
: base_t{std::data(arr), N}, ptr{std::data(arr)} {
static_assert(Extent == dynamic_extent or Extent <= N,
"Span extends beyond available storage");
}
Expand Down
32 changes: 32 additions & 0 deletions test/span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ TEST_CASE("span is constructible from std::array (non const data)", "[span]") {
STATIC_REQUIRE(std::size(s) == 4);
}

TEST_CASE("dynamic span is constructible from std::array (const data)",
"[span]") {
constexpr static auto a = std::array{1, 2, 3, 4};
auto s = [](stdx::span<int const> x) { return x; }(a);
CHECK(std::data(s) == std::data(a));
CHECK(std::size(s) == std::size(a));
}

TEST_CASE("dynamic span is constructible from std::array (non const data)",
"[span]") {
auto a = std::array{1, 2, 3, 4};
auto s = [](stdx::span<int> x) { return x; }(a);
CHECK(std::data(s) == std::data(a));
CHECK(std::size(s) == std::size(a));
}

TEST_CASE("span is constructible from C-style array (const data)", "[span]") {
constexpr static int a[] = {1, 2, 3, 4};
constexpr auto s = stdx::span{a};
Expand All @@ -159,6 +175,22 @@ TEST_CASE("span is constructible from C-style array (non const data)",
CHECK(std::size(s) == std::size(a));
}

TEST_CASE("dynamic span is constructible from C-style array (const data)",
"[span]") {
constexpr static int a[] = {1, 2, 3, 4};
auto s = [](stdx::span<int const> x) { return x; }(a);
CHECK(std::data(s) == std::data(a));
CHECK(std::size(s) == std::size(a));
}

TEST_CASE("dynamic span is constructible from C-style array (non const data)",
"[span]") {
int a[] = {1, 2, 3, 4};
auto s = [](stdx::span<int> x) { return x; }(a);
CHECK(std::data(s) == std::data(a));
CHECK(std::size(s) == std::size(a));
}

TEST_CASE("dynamic span is implicitly constructible from range (const data)",
"[span]") {
std::vector const v{1, 2, 3, 4};
Expand Down