diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2019-11-17 01:32:55 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2019-11-17 01:32:55 +0000 |
commit | 8857080c81761eb1344c3b5404b5e25e9cc949eb (patch) | |
tree | 540c435e1caf44d2761d188db56b20b96d913759 | |
parent | 37f33df706851a00b2f5c0d429f6b273e8ab0029 (diff) | |
download | gcc-8857080c81761eb1344c3b5404b5e25e9cc949eb.zip gcc-8857080c81761eb1344c3b5404b5e25e9cc949eb.tar.gz gcc-8857080c81761eb1344c3b5404b5e25e9cc949eb.tar.bz2 |
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
-rw-r--r-- | libstdc++-v3/ChangeLog | 4 | ||||
-rw-r--r-- | libstdc++-v3/include/std/string_view | 15 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc | 42 |
3 files changed, 61 insertions, 0 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index dad29e0..2dc2959 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,9 @@ 2019-11-17 Jonathan Wakely <jwakely@redhat.com> + * 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 ff1c0c3..9d2a8e8 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<contiguous_iterator _It, sized_sentinel_for<_It> _End> + requires same_as<iter_value_t<_It>, _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<contiguous_iterator _It, sized_sentinel_for<_It> _End> + basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>; +#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 0000000..d554b77 --- /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 +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include <string_view> +#include <vector> +#include <testsuite_hooks.h> + +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<char> v{'a', 'b', 'c'}; + std::basic_string_view s(v.begin(), v.end()); + VERIFY( s.data() == v.data() ); +} + +int +main() +{ + test01(); +} |