libstdc++: add range constructor for std::string_view (P1391R4)
authorJonathan Wakely <jwakely@redhat.com>
Sun, 17 Nov 2019 01:32:55 +0000 (01:32 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Sun, 17 Nov 2019 01:32:55 +0000 (01:32 +0000)
* 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

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/string_view
libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range.cc [new file with mode: 0644]

index dad29e03f81761c1c50fb2fe49e39e12549db63c..2dc29596959221522a080394b026c2e201b230e3 100644 (file)
@@ -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):
index ff1c0c3f36f85a0d64efbf5dd942c09878fd92a4..9d2a8e8e0c2bb10c61437faf3bb43636d17eec4b 100644 (file)
@@ -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 (file)
index 0000000..d554b77
--- /dev/null
@@ -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();
+}