From: Jonathan Wakely Date: Sun, 17 Nov 2019 01:32:55 +0000 (+0000) Subject: libstdc++: add range constructor for std::string_view (P1391R4) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=8857080c81761eb1344c3b5404b5e25e9cc949eb;p=gcc.git libstdc++: add range constructor for std::string_view (P1391R4) * include/std/string_view (basic_string_view(It, End)): Add range constructor and deduction guide from P1391R4. * testsuite/21_strings/basic_string_view/cons/char/range.cc: New test. From-SVN: r278371 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index dad29e03f81..2dc29596959 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2019-11-17 Jonathan Wakely + * include/std/string_view (basic_string_view(It, End)): Add range + constructor and deduction guide from P1391R4. + * testsuite/21_strings/basic_string_view/cons/char/range.cc: New test. + * include/bits/regex.h (match_results): Specialize __enable_view_impl. * include/bits/stl_set.h (set): Likewise. * include/bits/unordered_set.h (unordered_set, unordered_multiset): diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view index ff1c0c3f36f..9d2a8e8e0c2 100644 --- a/libstdc++-v3/include/std/string_view +++ b/libstdc++-v3/include/std/string_view @@ -130,6 +130,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION : _M_len{__len}, _M_str{__str} { } +#if __cplusplus > 201703L && __cpp_lib_concepts + template _End> + requires same_as, _CharT> + && (!convertible_to<_End, size_type>) + constexpr + basic_string_view(_It __first, _End __last) + : _M_len(__last - __first), _M_str(std::to_address(__first)) + { } +#endif + constexpr basic_string_view& operator=(const basic_string_view&) noexcept = default; @@ -457,6 +467,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION const _CharT* _M_str; }; +#if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides + template _End> + basic_string_view(_It, _End) -> basic_string_view>; +#endif + // [string.view.comparison], non-member basic_string_view comparison function // Several of these functions use type_identity_t to create a non-deduced 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 new file mode 100644 index 00000000000..d554b77c874 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +constexpr char str[] = "abcdefg"; +constexpr std::basic_string_view s(std::begin(str), std::cend(str) - 1); +static_assert( s == str ); +static_assert( s.data() == str ); + +void +test01() +{ + std::vector v{'a', 'b', 'c'}; + std::basic_string_view s(v.begin(), v.end()); + VERIFY( s.data() == v.data() ); +} + +int +main() +{ + test01(); +}