Skip to content

Commit f937564

Browse files
committed
Fix hardened precondition for copy constructor (#89, thanks @T-Bakker)
If the destination's extent is non-dynamic, the source size must match the destination size.
1 parent 5ac676a commit f937564

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

include/nonstd/span.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ class span
11641164
: data_( other.data() )
11651165
, size_( other.size() )
11661166
{
1167-
span_EXPECTS( OtherExtent == dynamic_extent || other.size() == to_size(OtherExtent) );
1167+
span_EXPECTS( Extent == dynamic_extent || Extent != dynamic_extent && other.size() == to_size(Extent) );
11681168
}
11691169

11701170
// 26.7.3.3 Subviews [span.sub]

test/span.t.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2239,6 +2239,16 @@ CASE( "[hide][issue-69: constructor from iterators is not properly constrained]"
22392239
#endif
22402240
}
22412241

2242+
CASE( "[hide][issue-89: disallow construction of static-sized span from dynamic span with different size: precondition violation]" )
2243+
{
2244+
int data3[] = { 1, 2, 3 };
2245+
2246+
nonstd::span<int > dynamic_spn3( data3 );
2247+
nonstd::span<int, 2> static_spn2( dynamic_spn3 ); // <== disallow: smaller static extent
2248+
nonstd::span<int, 3> static_spn3( dynamic_spn3 ); // <== allow same extent, dynamic size
2249+
nonstd::span<int, 4> static_spn4( dynamic_spn3 ); // <== disallow: larger static extent
2250+
}
2251+
22422252
CASE( "tweak header: reads tweak header if supported " "[tweak]" )
22432253
{
22442254
#if span_HAVE( TWEAK_HEADER )

0 commit comments

Comments
 (0)