public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r11-10236] libstdc++: Make std::string_view(Range&&) constructor explicit
Date: Mon,  5 Sep 2022 09:57:05 +0000 (GMT)	[thread overview]
Message-ID: <20220905095705.6FF5F3858D32@sourceware.org> (raw)

https://gcc.gnu.org/g:9c6141bfec48c2bb92c6846d78589996ff62e4b2

commit r11-10236-g9c6141bfec48c2bb92c6846d78589996ff62e4b2
Author: Jonathan Wakely <jwakely@redhat.com>
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<typename _DRange::traits_type, _Traits>)
-	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<R2> );
   static_assert( std::ranges::sized_range<R2> );
   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<const R2&>(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<const R2&>(r2)) == s2_implicit );
+
   struct R3 : R
   {
     using R::begin;
@@ -91,7 +96,7 @@ test01()
   static_assert( std::ranges::contiguous_range<R5> );
   static_assert( std::ranges::sized_range<R5> );
   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<decltype(s), std::string_view> );
 }
 
+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<R, std::string_view> );
+}
+
 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<R2> );
   static_assert( std::ranges::sized_range<R2> );
   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<const R2&>(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<const R2&>(r2)) == s2_implicit );
+
   struct R3 : R
   {
     using R::begin;
@@ -91,10 +96,10 @@ test01()
   static_assert( std::ranges::contiguous_range<R5> );
   static_assert( std::ranges::sized_range<R5> );
   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<decltype(s), std::wstring_view> );
 }
 
+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<R, std::wstring_view> );
+}
+
 int main()
 {
   test01();
   test02();
   test03();
   test04();
+  test05();
 }

                 reply	other threads:[~2022-09-05  9:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220905095705.6FF5F3858D32@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).