Skip to content

Commit 9e06070

Browse files
MemSpan: Fix MemSpan<void> to not construct from arrays of constant values.
1 parent 23eb3fc commit 9e06070

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

code/include/swoc/MemSpan.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ template <typename T> class MemSpan {
138138
*
139139
* @internal A non-const variant of this is needed because passing by CR means imposing constness
140140
* on the container which can then undesirably propagate that to the element type. Best example -
141-
* consstructing from @c std::string. Without this variant it's not possible to construct a @c char
141+
* constructing from @c std::string. Without this variant it's not possible to construct a @c char
142142
* span vs. a @c char @c const.
143143
*/
144144
template < typename C
@@ -1369,7 +1369,9 @@ constexpr MemSpan<void const>::MemSpan(U (&a)[N]) : _ptr(const_cast<std::remove_
13691369
}
13701370
}
13711371
template <auto N, typename U>
1372-
constexpr MemSpan<void>::MemSpan(U (&a)[N]) : super_type(a) {}
1372+
constexpr MemSpan<void>::MemSpan(U (&a)[N]) : super_type(a) {
1373+
static_assert(!std::is_const_v<U>, "Error: constructing non-constant view with constant data.");
1374+
}
13731375

13741376
template <typename C, typename>
13751377
constexpr MemSpan<void const>::MemSpan(C const &c)

unit_tests/test_MemSpan.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ TEST_CASE("MemSpan modifiers", "[libswoc][MemSpan]") {
148148
REQUIRE(0 == memcmp(span.clip_suffix(5), MemSpan<void>(post - 5, 5)));
149149
REQUIRE(0 == memcmp(span, MemSpan<void>(pre, text.size() - 5)));
150150

151-
MemSpan<void> s1{"Evil Dave Rulz"};
152-
REQUIRE(s1.size() == 14); // terminal nul is not in view.
151+
// By design, MemSpan<void> won't construct from a literal string because it's const.
152+
// MemSpan<void> s1{"Evil Dave Rulz"}; // Should not compile.
153+
153154
uint8_t bytes[]{5,4,3,2,1,0};
154155
MemSpan<void> s2{bytes};
155156
REQUIRE(s2.size() == sizeof(bytes)); // terminal nul is in view
@@ -195,9 +196,6 @@ TEST_CASE("MemSpan construct", "[libswoc][MemSpan]") {
195196
REQUIRE(span[4]._n == 56);
196197
span.destroy();
197198
REQUIRE(counter == 0);
198-
199-
MemSpan<void const> vc_span { "Evil Dave Rulz"};
200-
// MemSpan<void> v_span { "Evil Dave Rulz" }; // This should not compile.
201199
}
202200

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

0 commit comments

Comments
 (0)