diff --git a/include/span.h b/include/span.h index 96a7901..0c67d22 100644 --- a/include/span.h +++ b/include/span.h @@ -1275,7 +1275,7 @@ public: && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > - constexpr span (Cont& cont) : span(static_cast(cont.data()), details::newBoundsHelper(cont.size())) + constexpr span (Cont& cont) : span(static_cast(cont.data()), details::newBoundsHelper(static_cast(cont.size()))) {} constexpr span(const span &) = default; @@ -1443,7 +1443,7 @@ public: return m_pdata; } - constexpr operator bool() const noexcept + constexpr explicit operator bool() const noexcept { return m_pdata != nullptr; } @@ -1577,6 +1577,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 +{ + Expects(str.size() < PTRDIFF_MAX); + return {&str[0], static_cast(str.size())}; +} + template class strided_span { @@ -1621,8 +1629,12 @@ public: {} // from array view - template > - constexpr strided_span(span av, bounds_type bounds) : strided_span(av.data(), av.bounds().total_size(), std::move(bounds)) + template ::value, + typename Dummy = std::enable_if_t + > + constexpr strided_span(span av, bounds_type bounds) : strided_span(av.data(), av.bounds().total_size(), std::move(bounds)) {} // convertible @@ -1688,7 +1700,7 @@ public: return m_pdata; } - constexpr operator bool() const noexcept + constexpr explicit operator bool() const noexcept { return m_pdata != nullptr; } diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 8a7c552..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); @@ -333,7 +342,7 @@ SUITE(span_tests) CHECK(sav_c[1] == 2); #if _MSC_VER > 1800 - strided_span sav_v{ {src}, {2, 1} }; + strided_span sav_v{ src, {2, 1} }; #else strided_span sav_v{ span{src}, strided_bounds<1>{2, 1} }; #endif @@ -342,7 +351,7 @@ SUITE(span_tests) CHECK(sav_v[1] == 2); #if _MSC_VER > 1800 - strided_span sav_cv{ {src}, {2, 1} }; + strided_span sav_cv{ src, {2, 1} }; #else strided_span sav_cv{ span{src}, strided_bounds<1>{2, 1} }; #endif @@ -361,7 +370,7 @@ SUITE(span_tests) CHECK(sav_c[1] == 2); #if _MSC_VER > 1800 - strided_span sav_cv{ {src}, {2, 1} }; + strided_span sav_cv{ src, {2, 1} }; #else strided_span sav_cv{ span{src}, strided_bounds<1>{2, 1} }; #endif @@ -381,7 +390,7 @@ SUITE(span_tests) CHECK(sav_v[1] == 2); #if _MSC_VER > 1800 - strided_span sav_cv{ {src}, {2, 1} }; + strided_span sav_cv{ src, {2, 1} }; #else strided_span sav_cv{ span{src}, strided_bounds<1>{2, 1} }; #endif