From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 6FF5F3858D32; Mon, 5 Sep 2022 09:57:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6FF5F3858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662371825; bh=BT3xLLOr3dOZqC2sO4I+uLOjf5qUOJGdzv26uwvv58w=; h=From:To:Subject:Date:From; b=AXY2NHqSnT44bEKh6baZU2jlS7quWIXd+9j2eleIb3a6rrsXA0WnTDNhASiZlTcSP CFdKmAdwJTXBGub+/ehjmh/LoGivaVVwje6RJqS0kLQGhAbdUxj01/YgYjkL1vOBao tj1BAPYH3fG0tPpB8lcN880rbTdRVeHoDE4OK1yE= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-10236] libstdc++: Make std::string_view(Range&&) constructor explicit X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 9f3599704aa55acc2b4da42f990fa3d3d20a941e X-Git-Newrev: 9c6141bfec48c2bb92c6846d78589996ff62e4b2 Message-Id: <20220905095705.6FF5F3858D32@sourceware.org> Date: Mon, 5 Sep 2022 09:57:05 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9c6141bfec48c2bb92c6846d78589996ff62e4b2 commit r11-10236-g9c6141bfec48c2bb92c6846d78589996ff62e4b2 Author: Jonathan Wakely Date: Thu Aug 4 13:08:00 2022 +0100 libstdc++: Make std::string_view(Range&&) constructor explicit The P2499R0 paper was recently approved for C++23. libstdc++-v3/ChangeLog: * include/std/string_view (basic_string_view(Range&&)): Add explicit as per P2499R0. * testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc: Adjust implicit conversions. Check implicit conversions fail. * testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc: Likewise. (cherry picked from commit 2678386df2cc3505da85e95643327aa928e66a8e) Diff: --- libstdc++-v3/include/std/string_view | 2 +- .../basic_string_view/cons/char/range_c++20.cc | 28 +++++++++++++++++--- .../basic_string_view/cons/wchar_t/range_c++20.cc | 30 ++++++++++++++++++---- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index d8cbee9bee0..68df70e99f1 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -160,7 +160,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION }) && (!requires { typename _DRange::traits_type; } || is_same_v) - constexpr + constexpr explicit basic_string_view(_Range&& __r) noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r))) : _M_len(ranges::size(__r)), _M_str(ranges::data(__r)) diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc index fa85f1994c9..c95239c613b 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc @@ -36,7 +36,7 @@ test01() }; R r; - std::string_view s = r; + std::string_view s{r}; VERIFY( s == r.str ); VERIFY( s.data() == std::ranges::data(r) ); VERIFY( s.size() == std::ranges::size(r) ); @@ -50,10 +50,15 @@ test01() static_assert( std::ranges::contiguous_range ); static_assert( std::ranges::sized_range ); R2 r2; - std::string_view s2 = r2; // uses conversion to string_view + std::string_view s2(r2); // uses conversion to string_view VERIFY( s2 == "Out of range" ); VERIFY( std::string_view(const_cast(r2)) == s2 ); + // And again using copy-initialization instead of direct-initialization. + std::string_view s2_implicit = r2; // uses conversion to string_view + VERIFY( s2_implicit == "Out of range" ); + VERIFY( std::string_view(const_cast(r2)) == s2_implicit ); + struct R3 : R { using R::begin; @@ -91,7 +96,7 @@ test01() static_assert( std::ranges::contiguous_range ); static_assert( std::ranges::sized_range ); R5 r5; - std::string_view s5 = r5; // Uses range constructor + std::string_view s5(r5); // Uses range constructor VERIFY( s5 == r5.str ); s5 = std::string_view(std::move(r5)); // In C++20 this used conversion op. VERIFY( s5 == r5.str ); // In C++23 it uses range constructor. @@ -156,15 +161,30 @@ test04() }; R r; - std::basic_string_view s = r; // Use deduction guide. + std::basic_string_view s(r); // Use deduction guide. static_assert( std::is_same_v ); } +void +test05() +{ + struct R + { + const char* begin() const { return nullptr; } + const char* end() const { return nullptr; } + }; + + // P2499R0 string_view range constructor should be explicit + // P2516R0 string_view is implicitly convertible from what? + static_assert( ! std::is_convertible_v ); +} + int main() { test01(); test02(); test03(); test04(); + test05(); } diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc index cf73ae36a60..2144ca49e77 100644 --- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc @@ -36,7 +36,7 @@ test01() }; R r; - std::wstring_view s = r; + std::wstring_view s{r}; VERIFY( s == r.str ); VERIFY( s.data() == std::ranges::data(r) ); VERIFY( s.size() == std::ranges::size(r) ); @@ -50,10 +50,15 @@ test01() static_assert( std::ranges::contiguous_range ); static_assert( std::ranges::sized_range ); R2 r2; - std::wstring_view s2 = r2; // uses conversion to wstring_view + std::wstring_view s2(r2); // uses conversion to wstring_view VERIFY( s2 == L"Out of range" ); VERIFY( std::wstring_view(const_cast(r2)) == s2 ); + // And again using copy-initialization instead of direct-initialization. + std::wstring_view s2_implicit = r2; // uses conversion to wstring_view + VERIFY( s2_implicit == L"Out of range" ); + VERIFY( std::wstring_view(const_cast(r2)) == s2_implicit ); + struct R3 : R { using R::begin; @@ -91,10 +96,10 @@ test01() static_assert( std::ranges::contiguous_range ); static_assert( std::ranges::sized_range ); R5 r5; - std::wstring_view s5 = r5; // Uses range constructor + std::wstring_view s5(r5); // Uses range constructor VERIFY( s5 == r5.str ); s5 = std::wstring_view(std::move(r5)); // In C++20 this used conversion op. - VERIFY( s5 == r5.str ); // In C++23 it uses range constructor. + VERIFY( s5 == r5.str ); // In C++23 it uses range constructor. wchar_t arr[] = L"arrangement\0with\0nulls"; std::wstring_view sa = arr; // Does not use range constructor @@ -156,15 +161,30 @@ test04() }; R r; - std::basic_string_view s = r; // Use deduction guide. + std::basic_string_view s(r); // Use deduction guide. static_assert( std::is_same_v ); } +void +test05() +{ + struct R + { + const wchar_t* begin() const { return nullptr; } + const wchar_t* end() const { return nullptr; } + }; + + // P2499R0 string_view range constructor should be explicit + // P2516R0 string_view is implicitly convertible from what? + static_assert( ! std::is_convertible_v ); +} + int main() { test01(); test02(); test03(); test04(); + test05(); }