diff --git a/include/span.h b/include/span.h index 74a5e11..2bf9d20 100644 --- a/include/span.h +++ b/include/span.h @@ -1568,6 +1568,14 @@ template constexpr auto as_span(Cont &&arr) -> std::enable_if_t>::value, span, dynamic_range>> = delete; +// from basic_string which doesn't have nonconst .data() member like other contiguous containers +template +constexpr auto as_span(std::basic_string &str) -> span +{ + fail_fast_assert(str.size() < PTRDIFF_MAX); + return {&str[0], static_cast(str.size())}; +} + template class strided_span { diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index a78fe6e..e5078af 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -203,6 +203,15 @@ SUITE(span_tests) overloaded_func(av3.as_span(dim<>(1), dim<3>(), dim<5>()), 't'); } + { + string str; + span strspan = as_span(str); + (void)strspan; + const string cstr; + span cstrspan = as_span(cstr); + (void)cstrspan; + } + { int a[3][4][5]; auto av = as_span(a);