diff --git a/code/include/swoc/MemSpan.h b/code/include/swoc/MemSpan.h index 30373cf..53d7a3f 100644 --- a/code/include/swoc/MemSpan.h +++ b/code/include/swoc/MemSpan.h @@ -138,7 +138,7 @@ template 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 @@ -1369,7 +1369,9 @@ constexpr MemSpan::MemSpan(U (&a)[N]) : _ptr(const_cast -constexpr MemSpan::MemSpan(U (&a)[N]) : super_type(a) {} +constexpr MemSpan::MemSpan(U (&a)[N]) : super_type(a) { + static_assert(!std::is_const_v, "Error: constructing non-constant view with constant data."); +} template constexpr MemSpan::MemSpan(C const &c) diff --git a/unit_tests/test_MemSpan.cc b/unit_tests/test_MemSpan.cc index 0b75393..a24fa33 100644 --- a/unit_tests/test_MemSpan.cc +++ b/unit_tests/test_MemSpan.cc @@ -148,8 +148,9 @@ TEST_CASE("MemSpan modifiers", "[libswoc][MemSpan]") { REQUIRE(0 == memcmp(span.clip_suffix(5), MemSpan(post - 5, 5))); REQUIRE(0 == memcmp(span, MemSpan(pre, text.size() - 5))); - MemSpan s1{"Evil Dave Rulz"}; - REQUIRE(s1.size() == 14); // terminal nul is not in view. + // By design, MemSpan won't construct from a literal string because it's const. + // MemSpan s1{"Evil Dave Rulz"}; // Should not compile. + uint8_t bytes[]{5,4,3,2,1,0}; MemSpan s2{bytes}; REQUIRE(s2.size() == sizeof(bytes)); // terminal nul is in view @@ -195,9 +196,6 @@ TEST_CASE("MemSpan construct", "[libswoc][MemSpan]") { REQUIRE(span[4]._n == 56); span.destroy(); REQUIRE(counter == 0); - - MemSpan vc_span { "Evil Dave Rulz"}; - // MemSpan v_span { "Evil Dave Rulz" }; // This should not compile. } TEST_CASE("MemSpan", "[libswoc][MemSpan]")