public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-8666] libstdc++: Make std::string_view(Range&&) constructor explicit
@ 2022-08-05 12:32 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2022-08-05 12:32 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:61076545cb3c3cbc79036eff8bc46b0c2083730c

commit r12-8666-g61076545cb3c3cbc79036eff8bc46b0c2083730c
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 bccf4d1847f..30ff136b1cb 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -162,7 +162,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 bd50c3058e6..a5745fcb603 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 0b28220d862..af3c986e56f 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();
 }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-05 12:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-05 12:32 [gcc r12-8666] libstdc++: Make std::string_view(Range&&) constructor explicit Jonathan Wakely

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).