Skip to content

Commit 1247cb3

Browse files
committed
🐛 Fix compile-time capacity calculation for dynamic span
Problem: - `ct_capacity_v<span<int>>` returns its capacity as the maximum value of `std::size_t`, which is wrong. Solution: - Make `ct_capacity_v` a compile-time error when called on a dynamic `stdx::span`. (Just like on a dynamic `std::span`). These things don't have compile-time capacities; they are dynamic.
1 parent 4d468ed commit 1247cb3

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

include/stdx/span.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ span(R &&) -> span<std::remove_reference_t<detail::range_reference_t<R>>>;
281281

282282
template <typename T, std::size_t N>
283283
constexpr auto ct_capacity_v<span<T, N>> = N;
284+
285+
template <typename T>
286+
constexpr auto ct_capacity_v<span<T, dynamic_extent>> =
287+
detail::ct_capacity_fail<span<T, dynamic_extent>>{};
284288
} // namespace v1
285289
} // namespace stdx
286290

test/fail/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
3232
atomic_bool_dec
3333
call_by_need
3434
ct_format_mismatch
35-
dynamic_span_no_ct_capacity
3635
dynamic_container_no_ct_capacity
36+
dynamic_std_span_no_ct_capacity
37+
dynamic_stdx_span_no_ct_capacity
3738
tuple_index_out_of_bounds
3839
tuple_equality_mismatch
3940
tuple_equality_with_element
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdx/iterator.hpp>
2+
#include <stdx/span.hpp>
3+
4+
#include <array>
5+
#include <iterator>
6+
7+
// EXPECT: Type does not support compile-time capacity
8+
9+
auto main() -> int {
10+
auto a = std::array<int, 4>{};
11+
auto s = stdx::span{std::begin(a), std::end(a)};
12+
constexpr auto c = stdx::ct_capacity(s);
13+
}

0 commit comments

Comments
 (0)