public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9148] libstdc++: Add noexcept-specifier to basic_string_view(It, End)
@ 2021-10-13 20:54 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-10-13 20:54 UTC (permalink / raw)
  To: gcc-cvs, libstdc++-cvs

https://gcc.gnu.org/g:3eac45a2a13b959166806f132aea756026c9b8cb

commit r11-9148-g3eac45a2a13b959166806f132aea756026c9b8cb
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jul 14 11:03:17 2021 +0100

    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.
    
    (cherry picked from commit f9c2ce1dae270d8d5dc261a57a21f96a1da5ea2d)

Diff:
---
 libstdc++-v3/include/std/string_view                   |  1 +
 .../21_strings/basic_string_view/cons/char/range.cc    | 18 ++++++++++++++----
 2 files changed, 15 insertions(+), 4 deletions(-)

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-10-13 20:54 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13 20:54 [gcc r11-9148] 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).