[libstdc++] Two parts of C++20 p1032...
diff mbox series

Message ID 8074f1f285804d078727db1c89a6977c@alionscience.com
State New
Headers show
Series
  • [libstdc++] Two parts of C++20 p1032...
Related show

Commit Message

Smith-Rowland, Edward M Dec. 10, 2019, 5:41 p.m. UTC
These patches are modest reworkings of previous patches.

The patch for char_traits was pretty much approved already.  But in response to a comment from François I at least added __copy_backwards, and used ptrdiff_t instead of size_t parameters and I put all the mem* wrappers in a __detail namespace. I didn't change some names. I know Jonathan wanted a bikeshed discussion.

For the constexpr iterators I was asked to simplify the testsuite. Done.

These would finish p1032 except for std::string::copy which I think should wait behind the rest of constexpr string.

Another thing I need to do is respond to the SG-10 changes for the p1032 macros.  There's one thing that bothers me still in light of the policy that these macros are names __cpp_lib_constexpr_HEADER: We sill have __cpp_lib_constexpr_algorithmS in <algorithm>.

OK if these pass final retesting?

Patch
diff mbox series

Index: include/bits/stl_iterator.h
===================================================================
--- include/bits/stl_iterator.h	(revision 279174)
+++ include/bits/stl_iterator.h	(working copy)
@@ -502,8 +502,12 @@ 
       /// A nested typedef for the type of whatever container you used.
       typedef _Container          container_type;
 
+#if __cplusplus > 201703L
+      constexpr back_insert_iterator() noexcept = default;
+#endif
+
       /// The only way to create this %iterator is with a container.
-      explicit
+      explicit _GLIBCXX20_CONSTEXPR
       back_insert_iterator(_Container& __x)
       : container(std::__addressof(__x)) { }
 
@@ -526,7 +530,7 @@ 
 	return *this;
       }
 #else
-      back_insert_iterator&
+      _GLIBCXX20_CONSTEXPR back_insert_iterator&
       operator=(const typename _Container::value_type& __value)
       {
 	container->push_back(__value);
@@ -533,7 +537,7 @@ 
 	return *this;
       }
 
-      back_insert_iterator&
+      _GLIBCXX20_CONSTEXPR back_insert_iterator&
       operator=(typename _Container::value_type&& __value)
       {
 	container->push_back(std::move(__value));
@@ -542,17 +546,17 @@ 
 #endif
 
       /// Simply returns *this.
-      back_insert_iterator&
+      _GLIBCXX20_CONSTEXPR back_insert_iterator&
       operator*()
       { return *this; }
 
       /// Simply returns *this.  (This %iterator does not @a move.)
-      back_insert_iterator&
+      _GLIBCXX20_CONSTEXPR back_insert_iterator&
       operator++()
       { return *this; }
 
       /// Simply returns *this.  (This %iterator does not @a move.)
-      back_insert_iterator
+      _GLIBCXX20_CONSTEXPR back_insert_iterator
       operator++(int)
       { return *this; }
     };
@@ -569,7 +573,7 @@ 
    *  types for you.
   */
   template<typename _Container>
-    inline back_insert_iterator<_Container>
+    inline _GLIBCXX20_CONSTEXPR back_insert_iterator<_Container>
     back_inserter(_Container& __x)
     { return back_insert_iterator<_Container>(__x); }
 
@@ -594,8 +598,13 @@ 
       /// A nested typedef for the type of whatever container you used.
       typedef _Container          container_type;
 
+#if __cplusplus > 201703L
+      constexpr front_insert_iterator() noexcept = default;
+#endif
+
       /// The only way to create this %iterator is with a container.
-      explicit front_insert_iterator(_Container& __x)
+      explicit _GLIBCXX20_CONSTEXPR
+      front_insert_iterator(_Container& __x)
       : container(std::__addressof(__x)) { }
 
       /**
@@ -617,14 +626,13 @@ 
 	return *this;
       }
 #else
-      front_insert_iterator&
+      _GLIBCXX20_CONSTEXPR front_insert_iterator&
       operator=(const typename _Container::value_type& __value)
       {
 	container->push_front(__value);
 	return *this;
       }
-
-      front_insert_iterator&
+      _GLIBCXX20_CONSTEXPR front_insert_iterator&
       operator=(typename _Container::value_type&& __value)
       {
 	container->push_front(std::move(__value));
@@ -633,17 +641,17 @@ 
 #endif
 
       /// Simply returns *this.
-      front_insert_iterator&
+      _GLIBCXX20_CONSTEXPR front_insert_iterator&
       operator*()
       { return *this; }
 
       /// Simply returns *this.  (This %iterator does not @a move.)
-      front_insert_iterator&
+      _GLIBCXX20_CONSTEXPR front_insert_iterator&
       operator++()
       { return *this; }
 
       /// Simply returns *this.  (This %iterator does not @a move.)
-      front_insert_iterator
+      _GLIBCXX20_CONSTEXPR front_insert_iterator
       operator++(int)
       { return *this; }
     };
@@ -660,7 +668,7 @@ 
    *  types for you.
   */
   template<typename _Container>
-    inline front_insert_iterator<_Container>
+    inline _GLIBCXX20_CONSTEXPR front_insert_iterator<_Container>
     front_inserter(_Container& __x)
     { return front_insert_iterator<_Container>(__x); }
 
@@ -690,10 +698,15 @@ 
       /// A nested typedef for the type of whatever container you used.
       typedef _Container          container_type;
 
+#if __cplusplus > 201703L
+      constexpr insert_iterator() = default;
+#endif
+
       /**
        *  The only way to create this %iterator is with a container and an
        *  initial position (a normal %iterator into the container).
       */
+      _GLIBCXX20_CONSTEXPR
       insert_iterator(_Container& __x, typename _Container::iterator __i)
       : container(std::__addressof(__x)), iter(__i) {}
 
@@ -729,7 +742,7 @@ 
 	return *this;
       }
 #else
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator=(const typename _Container::value_type& __value)
       {
 	iter = container->insert(iter, __value);
@@ -737,7 +750,7 @@ 
 	return *this;
       }
 
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator=(typename _Container::value_type&& __value)
       {
 	iter = container->insert(iter, std::move(__value));
@@ -747,17 +760,17 @@ 
 #endif
 
       /// Simply returns *this.
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator*()
       { return *this; }
 
       /// Simply returns *this.  (This %iterator does not @a move.)
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator++()
       { return *this; }
 
       /// Simply returns *this.  (This %iterator does not @a move.)
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator++(int)
       { return *this; }
     };
@@ -775,7 +788,7 @@ 
    *  types for you.
   */
   template<typename _Container, typename _Iterator>
-    inline insert_iterator<_Container>
+    inline _GLIBCXX20_CONSTEXPR insert_iterator<_Container>
     inserter(_Container& __x, _Iterator __i)
     {
       return insert_iterator<_Container>(__x,
Index: testsuite/24_iterators/back_insert_iterator/requirements/constexpr.cc
===================================================================
--- testsuite/24_iterators/back_insert_iterator/requirements/constexpr.cc	(nonexistent)
+++ testsuite/24_iterators/back_insert_iterator/requirements/constexpr.cc	(working copy)
@@ -0,0 +1,67 @@ 
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+//
+// 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/>.
+
+#include <iterator>
+#include <array>
+
+template<typename Tp>
+  struct listoid
+  {
+    using value_type = Tp;
+    using iterator = size_t;
+
+    constexpr listoid() = default;
+
+    constexpr void push_back(const value_type& value) { return; }
+    constexpr void push_back(value_type&& value) { return; }
+    constexpr void push_front(const value_type& value) { return; }
+    constexpr void push_front(value_type&& value) { return; }
+    constexpr iterator insert(iterator pos, const value_type& value) { return pos; }
+    constexpr iterator begin() noexcept { return _M_begin; }
+    constexpr iterator end() noexcept { return _M_end; }
+
+  private:
+    size_t _M_begin = 0;
+    size_t _M_end = 0;
+  };
+
+constexpr bool
+test()
+{
+  listoid<int> l;
+
+  auto ok = true;
+  const int route = 66;
+
+  constexpr std::array<int, 2> b{89, 99};
+  const std::back_insert_iterator<listoid<int>> bi(l);
+  std::copy(b.begin(), b.end(), bi);
+  auto nbi = std::back_inserter(l);
+  nbi = route;
+  nbi = 77;
+
+  return ok;
+}
+
+int
+main()
+{
+  static_assert(test());
+}
Index: testsuite/24_iterators/insert_iterator/requirements/constexpr.cc
===================================================================
--- testsuite/24_iterators/insert_iterator/requirements/constexpr.cc	(nonexistent)
+++ testsuite/24_iterators/insert_iterator/requirements/constexpr.cc	(working copy)
@@ -0,0 +1,68 @@ 
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+//
+// 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/>.
+
+#include <iterator>
+#include <array>
+
+template<typename Tp>
+  struct listoid
+  {
+    using value_type = Tp;
+    using iterator = size_t;
+
+    constexpr listoid() = default;
+
+    constexpr void push_back(const value_type& value) { return; }
+    constexpr void push_back(value_type&& value) { return; }
+    constexpr void push_front(const value_type& value) { return; }
+    constexpr void push_front(value_type&& value) { return; }
+    constexpr iterator insert(iterator pos, const value_type& value) { return pos; }
+    constexpr iterator begin() noexcept { return _M_begin; }
+    constexpr iterator end() noexcept { return _M_end; }
+
+  private:
+    size_t _M_begin = 0;
+    size_t _M_end = 0;
+  };
+
+constexpr bool
+test()
+{
+  listoid<int> l;
+
+  auto ok = true;
+  const int route = 66;
+
+  constexpr std::array<int, 5> a{1, 2, 3, 4, 5};
+  const auto liter = l.begin() + 1;
+  const std::insert_iterator<listoid<int>> ii(l, liter);
+  std::copy(a.begin(), a.end(), ii);
+  auto nii = std::inserter(l, liter);
+  nii = route;
+  nii = 77;
+
+  return ok;
+}
+
+int
+main()
+{
+  static_assert(test());
+}
Index: testsuite/24_iterators/front_insert_iterator/requirements/constexpr.cc
===================================================================
--- testsuite/24_iterators/front_insert_iterator/requirements/constexpr.cc	(nonexistent)
+++ testsuite/24_iterators/front_insert_iterator/requirements/constexpr.cc	(working copy)
@@ -0,0 +1,67 @@ 
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+//
+// 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/>.
+
+#include <iterator>
+#include <array>
+
+template<typename Tp>
+  struct listoid
+  {
+    using value_type = Tp;
+    using iterator = size_t;
+
+    constexpr listoid() = default;
+
+    constexpr void push_back(const value_type& value) { return; }
+    constexpr void push_back(value_type&& value) { return; }
+    constexpr void push_front(const value_type& value) { return; }
+    constexpr void push_front(value_type&& value) { return; }
+    constexpr iterator insert(iterator pos, const value_type& value) { return pos; }
+    constexpr iterator begin() noexcept { return _M_begin; }
+    constexpr iterator end() noexcept { return _M_end; }
+
+  private:
+    size_t _M_begin = 0;
+    size_t _M_end = 0;
+  };
+
+constexpr bool
+test()
+{
+  listoid<int> l;
+
+  auto ok = true;
+  const int route = 66;
+
+  constexpr std::array<int, 2> f{-1, 0};
+  const std::front_insert_iterator<listoid<int>> fi(l);
+  std::copy(f.begin(), f.end(), fi);
+  auto nfi = std::front_inserter(l);
+  nfi = route;
+  nfi = 77;
+
+  return ok;
+}
+
+int
+main()
+{
+  static_assert(test());
+}
Index: testsuite/24_iterators/headers/iterator/synopsis_c++17.cc
===================================================================
--- testsuite/24_iterators/headers/iterator/synopsis_c++17.cc	(revision 279174)
+++ testsuite/24_iterators/headers/iterator/synopsis_c++17.cc	(working copy)
@@ -1,5 +1,5 @@ 
 // { dg-options "-std=gnu++17" }
-// { dg-do compile }
+// { dg-do compile { target c++17_only } }
 // { dg-require-normal-namespace "" }
 
 // Copyright (C) 2016-2019 Free Software Foundation, Inc.
@@ -88,7 +88,7 @@ 
 
   template <class Iterator>
   constexpr reverse_iterator<Iterator> make_reverse_iterator(const Iterator&);
-
+#if __cplusplus == 201703L
   template <class Container> class back_insert_iterator;
 
   template <class Container>
@@ -103,7 +103,7 @@ 
 
   template <class Container, class Iterator>
   insert_iterator<Container> inserter(Container& x, Iterator i);
-
+#endif
   template <class Iterator> class move_iterator;
 
   template <class Iterator1, class Iterator2>
Index: testsuite/24_iterators/headers/iterator/synopsis_c++20.cc
===================================================================
--- testsuite/24_iterators/headers/iterator/synopsis_c++20.cc	(revision 279174)
+++ testsuite/24_iterators/headers/iterator/synopsis_c++20.cc	(working copy)
@@ -76,6 +76,21 @@ 
     struct iterator_traits<counted_iterator<I>>;
 
   struct unreachable_sentinel_t;
+
+  template <class Container> class back_insert_iterator;
+
+  template <class Container>
+  constexpr back_insert_iterator<Container> back_inserter(Container& x);
+
+  template <class Container> class front_insert_iterator;
+
+  template <class Container>
+  constexpr front_insert_iterator<Container> front_inserter(Container& x);
+
+  template <class Container> class insert_iterator;
+
+  template <class Container, class Iterator>
+  constexpr insert_iterator<Container> inserter(Container& x, Iterator i);
 }
 
 struct I { };