public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Add noexcept-specifier to basic_string_view(It, End)
@ 2021-07-14 11:24 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-07-14 11:24 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 496 bytes --]

This adds a conditional noexcept to the C++20 constructor. The
std::to_address call cannot throw, so only taking the difference of the
two iterators can throw.

Signed-off-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	* include/std/string_view (basic_string_view(It, End)): Add
	noexcept-specifier.
	* testsuite/21_strings/basic_string_view/cons/char/range.cc:
	Check noexcept-specifier. Also check construction without CTAD.

Tested powerpc64le-linux. Committed to trunk.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2837 bytes --]

commit f9c2ce1dae270d8d5dc261a57a21f96a1da5ea2d
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jul 14 11:03:17 2021

    libstdc++: Add noexcept-specifier to basic_string_view(It, End)
    
    This adds a conditional noexcept to the C++20 constructor. The
    std::to_address call cannot throw, so only taking the difference of the
    two iterators can throw.
    
    Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
    
    libstdc++-v3/ChangeLog:
    
            * include/std/string_view (basic_string_view(It, End)): Add
            noexcept-specifier.
            * testsuite/21_strings/basic_string_view/cons/char/range.cc:
            Check noexcept-specifier. Also check construction without CTAD.

diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index 4ea72c6cef2..d8cbee9bee0 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -144,6 +144,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  && (!convertible_to<_End, size_type>)
 	constexpr
 	basic_string_view(_It __first, _End __last)
+	noexcept(noexcept(__last - __first))
 	: _M_len(__last - __first), _M_str(std::to_address(__first))
 	{ }
 
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc
index 39fcaf52925..7376e2fa3f4 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc
@@ -15,24 +15,34 @@
 // with this library; see the file COPYING3.  If not see
 // <http://www.gnu.org/licenses/>.
 
-// { dg-options "-std=gnu++2a" }
-// { dg-do run { target c++2a } }
+// { dg-options "-std=gnu++20" }
+// { dg-do run { target c++20 } }
 
 #include <string_view>
 #include <vector>
 #include <testsuite_hooks.h>
+#include <testsuite_iterators.h>
 
 constexpr char str[] = "abcdefg";
-constexpr std::basic_string_view s(std::begin(str), std::cend(str) - 1);
+constexpr std::basic_string_view<char> s(std::begin(str), std::cend(str) - 1);
 static_assert( s == str );
 static_assert( s.data() == str );
+constexpr std::basic_string_view ctad(std::begin(str), std::cend(str) - 1);
+static_assert( ctad == s );
+
+// The standard does not require this constructor to have a noexcept-specifier.
+static_assert( noexcept(std::basic_string_view<char>(str, str)) );
+using I = __gnu_test::contiguous_iterator_wrapper<char>;
+static_assert( ! noexcept(std::basic_string_view<char>(I{}, I{})) );
 
 void
 test01()
 {
   std::vector<char> v{'a', 'b', 'c'};
-  std::basic_string_view s(v.begin(), v.end());
+  std::basic_string_view<char> s(v.begin(), v.end());
   VERIFY( s.data() == v.data() );
+  std::basic_string_view ctad(v.begin(), v.end());
+  VERIFY( ctad == s );
 }
 
 int

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

only message in thread, other threads:[~2021-07-14 11:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-14 11:24 [committed] libstdc++: Add noexcept-specifier to basic_string_view(It, End) 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).