diff --git a/libcxx/include/string_view b/libcxx/include/string_view --- a/libcxx/include/string_view +++ b/libcxx/include/string_view @@ -323,10 +323,7 @@ !is_convertible_v<_Range, const _CharT*> && (!requires(remove_cvref_t<_Range>& __d) { __d.operator _VSTD::basic_string_view<_CharT, _Traits>(); - }) && - (!requires { - typename remove_reference_t<_Range>::traits_type; - } || is_same_v::traits_type, _Traits>) + }) ) constexpr explicit _LIBCPP_HIDE_FROM_ABI basic_string_view(_Range&& __r) : __data_(ranges::data(__r)), __size_(ranges::size(__r)) {} diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_range.pass.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_range.pass.cpp --- a/libcxx/test/std/strings/string.view/string.view.cons/from_range.pass.cpp +++ b/libcxx/test/std/strings/string.view/string.view.cons/from_range.pass.cpp @@ -12,6 +12,7 @@ // template // constexpr basic_string_view(Range&& range); +#include #include #include #include @@ -25,11 +26,11 @@ #include "test_iterators.h" #include "test_range.h" -template +template constexpr void test() { auto data = MAKE_STRING_VIEW(CharT, "test"); std::array arr; - for(int i = 0; i < 4; ++i) { + for (int i = 0; i < 4; ++i) { arr[i] = data[i]; } auto sv = std::basic_string_view(arr); @@ -107,6 +108,15 @@ assert(sv == "test"); } + // Different trait types + { + struct OtherTraits : std::char_traits {}; + std::basic_string_view sv1{"hello"}; + std::basic_string_view sv2(sv1); + assert(sv1.size() == sv2.size()); + assert(sv1.data() == sv2.data()); + } + return true; } @@ -120,7 +130,10 @@ static_assert(std::ranges::sized_range); static_assert(!std::is_constructible_v); -using ContiguousButNotSizedRange = std::ranges::subrange, sentinel_wrapper>, std::ranges::subrange_kind::unsized>; +using ContiguousButNotSizedRange = + std::ranges::subrange, + sentinel_wrapper>, + std::ranges::subrange_kind::unsized>; static_assert(std::ranges::contiguous_range); static_assert(!std::ranges::sized_range); static_assert(!std::is_constructible_v); @@ -133,9 +146,9 @@ operator std::string_view() const { return {}; } }; -static_assert(std::is_constructible_v); // lvalue +static_assert(std::is_constructible_v); // lvalue static_assert(std::is_constructible_v); // const lvalue -static_assert(std::is_constructible_v); // rvalue +static_assert(std::is_constructible_v); // rvalue template struct WithTraitsType { @@ -150,9 +163,10 @@ static_assert(std::is_constructible_v>>); #endif static_assert(std::is_constructible_v, WithTraitsType>); -static_assert(!std::is_constructible_v>); // wrong traits type +static_assert(std::is_constructible_v>); // wrong traits type #ifndef TEST_HAS_NO_WIDE_CHARACTERS -static_assert(!std::is_constructible_v>>); // wrong traits type +static_assert( + std::is_constructible_v>>); // wrong traits type #endif #ifndef TEST_HAS_NO_EXCEPTIONS @@ -160,11 +174,14 @@ struct ThrowingData { char* begin() const { return nullptr; } char* end() const { return nullptr; } - char* data() const { throw 42; return nullptr; } + char* data() const { + throw 42; + return nullptr; + } }; try { ThrowingData x; - (void) std::string_view(x); + (void)std::string_view(x); assert(false); } catch (int i) { assert(i == 42); @@ -173,11 +190,14 @@ struct ThrowingSize { char* begin() const { return nullptr; } char* end() const { return nullptr; } - size_t size() const { throw 42; return 0; } + size_t size() const { + throw 42; + return 0; + } }; try { ThrowingSize x; - (void) std::string_view(x); + (void)std::string_view(x); assert(false); } catch (int i) { assert(i == 42); @@ -196,4 +216,3 @@ return 0; } - diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_string1.compile.fail.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_string1.compile.fail.cpp deleted file mode 100644 --- a/libcxx/test/std/strings/string.view/string.view.cons/from_string1.compile.fail.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -// UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14) - -// - -// template -// basic_string_view(const basic_string<_CharT, _Traits, Allocator>& _str) noexcept - -#include -#include -#include - -struct dummy_char_traits : public std::char_traits {}; - -int main(int, char**) { - using string_view = std::basic_string_view; - using string = std:: basic_string ; - - { - string s{"QBCDE"}; - string_view sv1 ( s ); - assert ( sv1.size() == s.size()); - assert ( sv1.data() == s.data()); - } - - return 0; -} diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_string2.compile.fail.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_string2.compile.fail.cpp deleted file mode 100644 --- a/libcxx/test/std/strings/string.view/string.view.cons/from_string2.compile.fail.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -// UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14) - -// - -// template -// basic_string_view(const basic_string<_CharT, _Traits, Allocator>& _str) noexcept - -#include -#include -#include - -struct dummy_char_traits : public std::char_traits {}; - -int main(int, char**) { - using string_view = std::basic_string_view; - using string = std:: basic_string ; - - { - string s{"QBCDE"}; - string_view sv1 ( s ); - assert ( sv1.size() == s.size()); - assert ( sv1.data() == s.data()); - } - - return 0; -}