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.
This commit is contained in:
Jonathan Wakely 2021-07-14 11:03:17 +01:00
parent a967a3efd3
commit f9c2ce1dae
2 changed files with 15 additions and 4 deletions

View File

@ -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))
{ }

View File

@ -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