From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id D97DF385841D; Wed, 13 Oct 2021 20:54:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D97DF385841D 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-9148] libstdc++: Add noexcept-specifier to basic_string_view(It, End) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 8b333df9484c1697f3a80530a47aa90b1859e970 X-Git-Newrev: 3eac45a2a13b959166806f132aea756026c9b8cb Message-Id: <20211013205435.D97DF385841D@sourceware.org> Date: Wed, 13 Oct 2021 20:54:35 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 13 Oct 2021 20:54:36 -0000 https://gcc.gnu.org/g:3eac45a2a13b959166806f132aea756026c9b8cb commit r11-9148-g3eac45a2a13b959166806f132aea756026c9b8cb Author: Jonathan Wakely 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 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 // . -// { dg-options "-std=gnu++2a" } -// { dg-do run { target c++2a } } +// { dg-options "-std=gnu++20" } +// { dg-do run { target c++20 } } #include #include #include +#include constexpr char str[] = "abcdefg"; -constexpr std::basic_string_view s(std::begin(str), std::cend(str) - 1); +constexpr std::basic_string_view 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(str, str)) ); +using I = __gnu_test::contiguous_iterator_wrapper; +static_assert( ! noexcept(std::basic_string_view(I{}, I{})) ); void test01() { std::vector v{'a', 'b', 'c'}; - std::basic_string_view s(v.begin(), v.end()); + std::basic_string_view s(v.begin(), v.end()); VERIFY( s.data() == v.data() ); + std::basic_string_view ctad(v.begin(), v.end()); + VERIFY( ctad == s ); } int