From e4d8d35af51aab9b072242aef0f57d8ce70625ab Mon Sep 17 00:00:00 2001 From: "Elron A. Yellin" Date: Fri, 20 Nov 2015 17:50:02 -0500 Subject: [PATCH] add as_span overload for basic_string which doesn't have nonconst .data() like other contiguous containers --- include/span.h | 8 ++++++++ tests/span_tests.cpp | 9 +++++++++ 2 files changed, 17 insertions(+) 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);