diff --git a/stl/inc/xstring b/stl/inc/xstring index 5cd221c2f72..8bb7e66e6f4 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -1240,6 +1240,15 @@ public: #endif // _CONTAINER_DEBUG_LEVEL > 0 } +#ifdef __cpp_lib_concepts + // clang-format off + template _Se> + requires (is_same_v, _Elem> && !is_convertible_v<_Se, size_type>) + constexpr basic_string_view(_It _First, _Se _Last) noexcept // strengthened + : _Mydata(_STD to_address(_First)), _Mysize(static_cast(_Last - _First)) {} + // clang-format on +#endif // __cpp_lib_concepts + _NODISCARD constexpr const_iterator begin() const noexcept { #if _ITERATOR_DEBUG_LEVEL >= 1 return const_iterator(_Mydata, _Mysize, 0); @@ -1628,6 +1637,9 @@ private: }; #ifdef __cpp_lib_concepts +template _Se> +basic_string_view(_It, _Se) -> basic_string_view>; + namespace ranges { template inline constexpr bool enable_view> = true; diff --git a/tests/std/tests/P0220R1_string_view/test.cpp b/tests/std/tests/P0220R1_string_view/test.cpp index 1d0fd5abba2..3e611eef4d1 100644 --- a/tests/std/tests/P0220R1_string_view/test.cpp +++ b/tests/std/tests/P0220R1_string_view/test.cpp @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include #include #include #include @@ -303,6 +304,24 @@ constexpr bool test_case_buffer_constructor() { return true; } +constexpr bool test_case_contiguous_constructor() { +#ifdef __cpp_lib_concepts + const array expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'}; + // Also tests the corresponding deduction guide: + same_as auto sv = basic_string_view(expectedData.begin(), expectedData.end()); + assert(sv.data() == expectedData.data()); + assert(sv.size() == 7); + assert(sv.length() == 7); + assert(!sv.empty()); + assert(sv[1] == 'o'); + assert(sv.at(1) == 'o'); + assert(sv.front() == 'n'); + assert(sv.back() == 'l'); +#endif // __cpp_lib_concepts + + return true; +} + template constexpr bool test_case_iterators() { using iterator = typename basic_string_view::iterator; @@ -1028,6 +1047,7 @@ static_assert(c_string_view{"abcd"} == "abcd"); static_assert(test_case_default_constructor()); static_assert(test_case_ntcts_constructor()); static_assert(test_case_buffer_constructor()); +static_assert(test_case_contiguous_constructor()); #if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-284079 "C1XX's C++14 constexpr emits bogus warnings C4146, // C4308, C4307 for basic_string_view::iterator" static_assert(test_case_iterators()); @@ -1072,6 +1092,7 @@ int main() { test_case_default_constructor(); test_case_ntcts_constructor(); test_case_buffer_constructor(); + test_case_contiguous_constructor(); test_case_iterators>(); test_case_iterators>(); test_case_prefix>();