Skip to content

Commit

Permalink
MemSpan: Fix MemSpan<void> to not construct from arrays of constant v…
Browse files Browse the repository at this point in the history
…alues.
  • Loading branch information
SolidWallOfCode committed Jul 25, 2023
1 parent 23eb3fc commit 9e06070
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 4 additions & 2 deletions code/include/swoc/MemSpan.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ template <typename T> class MemSpan {
*
* @internal A non-const variant of this is needed because passing by CR means imposing constness
* on the container which can then undesirably propagate that to the element type. Best example -
* consstructing from @c std::string. Without this variant it's not possible to construct a @c char
* constructing from @c std::string. Without this variant it's not possible to construct a @c char
* span vs. a @c char @c const.
*/
template < typename C
Expand Down Expand Up @@ -1369,7 +1369,9 @@ constexpr MemSpan<void const>::MemSpan(U (&a)[N]) : _ptr(const_cast<std::remove_
}
}
template <auto N, typename U>
constexpr MemSpan<void>::MemSpan(U (&a)[N]) : super_type(a) {}
constexpr MemSpan<void>::MemSpan(U (&a)[N]) : super_type(a) {
static_assert(!std::is_const_v<U>, "Error: constructing non-constant view with constant data.");
}

template <typename C, typename>
constexpr MemSpan<void const>::MemSpan(C const &c)
Expand Down
8 changes: 3 additions & 5 deletions unit_tests/test_MemSpan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ TEST_CASE("MemSpan modifiers", "[libswoc][MemSpan]") {
REQUIRE(0 == memcmp(span.clip_suffix(5), MemSpan<void>(post - 5, 5)));
REQUIRE(0 == memcmp(span, MemSpan<void>(pre, text.size() - 5)));

MemSpan<void> s1{"Evil Dave Rulz"};
REQUIRE(s1.size() == 14); // terminal nul is not in view.
// By design, MemSpan<void> won't construct from a literal string because it's const.
// MemSpan<void> s1{"Evil Dave Rulz"}; // Should not compile.

uint8_t bytes[]{5,4,3,2,1,0};
MemSpan<void> s2{bytes};
REQUIRE(s2.size() == sizeof(bytes)); // terminal nul is in view
Expand Down Expand Up @@ -195,9 +196,6 @@ TEST_CASE("MemSpan construct", "[libswoc][MemSpan]") {
REQUIRE(span[4]._n == 56);
span.destroy();
REQUIRE(counter == 0);

MemSpan<void const> vc_span { "Evil Dave Rulz"};
// MemSpan<void> v_span { "Evil Dave Rulz" }; // This should not compile.
}

TEST_CASE("MemSpan<void>", "[libswoc][MemSpan]")
Expand Down

0 comments on commit 9e06070

Please sign in to comment.