From: Jonathan Wakely <jwak...@redhat.com>
Sent: Friday, November 15, 2019 2:33 PM
To: Smith-Rowland, Edward M
Cc: libstd...@gcc.gnu.org; gcc-patches@gcc.gnu.org
Subject: Re: [External]_Re: Implement the <iterator> part of C++20 p1032 Misc 
constexpr bits.   

Oh I see the problem, it's because I made synopsis_c++20.cc include
synopsis_c++17.cc because I was lazy.

How about leaving those declarations in synopsis_c++17.cc but
guarding them with #if __cplusplus == 201703L and then adding the
constexpr versions of them in synopsis_c++20.cc ?

I think we should also add { target c++17_only } to synopsis_c++17.cc
(which should have had a target selector anyway).


Ok,
Testing this...
2019-11-15  Edward Smith-Rowland  <3dw...@verizon.net>

	Implement the <iterator> part of C++20 p1032 Misc constexpr bits.
	* include/bits/stl_iterator.h (back_insert_iterator, back_inserter)
	(front_insert_iterator, front_inserter, insert_iterator, inserter):
	Make constexpr.
	* testsuite/24_iterators/insert_iterator/requirements/constexpr.cc: New.
	* testsuite/24_iterators/back_insert_iterator/requirements/
	constexpr.cc: New.
	* testsuite/24_iterators/front_insert_iterator/requirements/
	constexpr.cc: New.
	* testsuite/24_iterators/headers/iterator/synopsis_c++17.cc: Make test
	C++17 only and block insert_iterator tests for C++17 (because of include
	of synopsis_c++20.cc.
	* testsuite/24_iterators/headers/iterator/synopsis_c++20.cc: Add
	constexpr inserter tests.

Index: include/bits/stl_iterator.h
===================================================================
--- include/bits/stl_iterator.h	(revision 278302)
+++ include/bits/stl_iterator.h	(working copy)
@@ -497,8 +497,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)) { }
 
@@ -521,7 +525,7 @@
 	return *this;
       }
 #else
-      back_insert_iterator&
+      _GLIBCXX20_CONSTEXPR back_insert_iterator&
       operator=(const typename _Container::value_type& __value)
       {
 	container->push_back(__value);
@@ -528,7 +532,7 @@
 	return *this;
       }
 
-      back_insert_iterator&
+      _GLIBCXX20_CONSTEXPR back_insert_iterator&
       operator=(typename _Container::value_type&& __value)
       {
 	container->push_back(std::move(__value));
@@ -537,17 +541,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; }
     };
@@ -564,7 +568,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); }
 
@@ -589,8 +593,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)) { }
 
       /**
@@ -612,14 +621,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));
@@ -628,17 +636,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; }
     };
@@ -655,7 +663,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); }
 
@@ -685,10 +693,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) {}
 
@@ -724,7 +737,7 @@
 	return *this;
       }
 #else
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator=(const typename _Container::value_type& __value)
       {
 	iter = container->insert(iter, __value);
@@ -732,7 +745,7 @@
 	return *this;
       }
 
-      insert_iterator&
+      _GLIBCXX20_CONSTEXPR insert_iterator&
       operator=(typename _Container::value_type&& __value)
       {
 	iter = container->insert(iter, std::move(__value));
@@ -742,17 +755,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; }
     };
@@ -770,7 +783,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/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 278302)
+++ 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/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());
+}

Reply via email to