[committed] libstdc++: Add default_sentinel support to stream iterators

Message ID 20200224134011.GA2111056@redhat.com
State New
Headers show
Series
  • [committed] libstdc++: Add default_sentinel support to stream iterators
Related show

Commit Message

Jonathan Wakely Feb. 24, 2020, 1:40 p.m.
Missing pieces of P0896R4 "The One Ranges Proposal" for C++20.

	* include/bits/stream_iterator.h (istream_iterator(default_sentinel_t)):
	Add constructor.
	(operator==(istream_iterator, default_sentinel_t)): Add operator.
	(ostream_iterator::difference_type): Define to ptrdiff_t for C++20.
	* include/bits/streambuf_iterator.h
	(istreambuf_iterator(default_sentinel_t)): Add constructor.
	(operator==(istreambuf_iterator, default_sentinel_t)): Add operator.
	* testsuite/24_iterators/istream_iterator/cons/sentinel.cc:
	New test.
	* testsuite/24_iterators/istream_iterator/sentinel.cc: New test.
	* testsuite/24_iterators/istreambuf_iterator/cons/sentinel.cc:
	New test.
	* testsuite/24_iterators/istreambuf_iterator/sentinel.cc: New test.

Tested powerpc64le-linux, committed to master.
commit 120e873484f20d9a0b8400e2e464ac5b2088a747
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Feb 24 13:11:31 2020 +0000

    libstdc++: Add default_sentinel support to stream iterators
    
    Missing pieces of P0896R4 "The One Ranges Proposal" for C++20.
    
            * include/bits/stream_iterator.h (istream_iterator(default_sentinel_t)):
            Add constructor.
            (operator==(istream_iterator, default_sentinel_t)): Add operator.
            (ostream_iterator::difference_type): Define to ptrdiff_t for C++20.
            * include/bits/streambuf_iterator.h
            (istreambuf_iterator(default_sentinel_t)): Add constructor.
            (operator==(istreambuf_iterator, default_sentinel_t)): Add operator.
            * testsuite/24_iterators/istream_iterator/cons/sentinel.cc:
            New test.
            * testsuite/24_iterators/istream_iterator/sentinel.cc: New test.
            * testsuite/24_iterators/istreambuf_iterator/cons/sentinel.cc:
            New test.
            * testsuite/24_iterators/istreambuf_iterator/sentinel.cc: New test.

Comments

Jonathan Wakely Feb. 24, 2020, 2:24 p.m. | #1
On 24/02/20 13:40 +0000, Jonathan Wakely wrote:
>Missing pieces of P0896R4 "The One Ranges Proposal" for C++20.

>

>	* include/bits/stream_iterator.h (istream_iterator(default_sentinel_t)):

>	Add constructor.

>	(operator==(istream_iterator, default_sentinel_t)): Add operator.

>	(ostream_iterator::difference_type): Define to ptrdiff_t for C++20.

>	* include/bits/streambuf_iterator.h

>	(istreambuf_iterator(default_sentinel_t)): Add constructor.

>	(operator==(istreambuf_iterator, default_sentinel_t)): Add operator.

>	* testsuite/24_iterators/istream_iterator/cons/sentinel.cc:

>	New test.

>	* testsuite/24_iterators/istream_iterator/sentinel.cc: New test.

>	* testsuite/24_iterators/istreambuf_iterator/cons/sentinel.cc:

>	New test.

>	* testsuite/24_iterators/istreambuf_iterator/sentinel.cc: New test.


This fixes a dumb mistake in that commit.

Tested powerpc64le-linux, committed to master.
commit 8566286eaeb9a977339df88212826150767f1203
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Feb 24 14:22:21 2020 +0000

    libstdc++: Fix noexcept-specifier for istream_iterator
    
    Somehow I missed that the _M_value member can throw on construction.
    
            * include/bits/stream_iterator.h (istream_iterator(default_sentinel_t)):
            Make noexcept-specifier conditional.
            * testsuite/24_iterators/istream_iterator/cons/sentinel.cc: Check
            noexcept-specifier.

diff --git a/libstdc++-v3/include/bits/stream_iterator.h b/libstdc++-v3/include/bits/stream_iterator.h
index 1ddf647f729..9d8ead092b8 100644
--- a/libstdc++-v3/include/bits/stream_iterator.h
+++ b/libstdc++-v3/include/bits/stream_iterator.h
@@ -79,7 +79,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #if __cplusplus > 201703L
       constexpr
-      istream_iterator(default_sentinel_t) noexcept
+      istream_iterator(default_sentinel_t)
+      noexcept(is_nothrow_default_constructible_v<_Tp>)
       : istream_iterator() { }
 #endif
 
diff --git a/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc b/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc
index 77a19498817..b890f04f8e9 100644
--- a/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc
+++ b/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc
@@ -19,9 +19,18 @@
 // <http://www.gnu.org/licenses/>.
 
 #include <iterator>
+#include <istream>
 
 // C++20 doesn't require this to be non-throwing.
 static_assert( std::is_nothrow_constructible_v<std::istream_iterator<int>,
 					       std::default_sentinel_t> );
 
 constexpr std::istream_iterator<int> i = std::default_sentinel;
+
+struct X { X() noexcept(false); };
+std::istream& operator<<(std::istream&, X&);
+
+static_assert( std::is_constructible_v<std::istream_iterator<X>,
+				       std::default_sentinel_t> );
+static_assert( ! std::is_nothrow_constructible_v<std::istream_iterator<X>,
+						 std::default_sentinel_t> );

Patch

diff --git a/libstdc++-v3/include/bits/stream_iterator.h b/libstdc++-v3/include/bits/stream_iterator.h
index 345a4d84e25..1ddf647f729 100644
--- a/libstdc++-v3/include/bits/stream_iterator.h
+++ b/libstdc++-v3/include/bits/stream_iterator.h
@@ -77,6 +77,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
         _M_ok(__obj._M_ok)
       { }
 
+#if __cplusplus > 201703L
+      constexpr
+      istream_iterator(default_sentinel_t) noexcept
+      : istream_iterator() { }
+#endif
+
 #if __cplusplus >= 201103L
       istream_iterator& operator=(const istream_iterator&) = default;
       ~istream_iterator() = default;
@@ -145,6 +151,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       friend bool
       operator!=(const istream_iterator& __x, const istream_iterator& __y)
       { return !__x._M_equal(__y); }
+
+#if __cplusplus > 201703L
+      friend bool
+      operator==(const istream_iterator& __i, default_sentinel_t)
+      { return !__i._M_stream; }
+#endif
     };
 
   /**
@@ -166,6 +178,9 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
     public:
       //@{
       /// Public typedef
+#if __cplusplus > 201703L
+      using difference_type = ptrdiff_t;
+#endif
       typedef _CharT                         char_type;
       typedef _Traits                        traits_type;
       typedef basic_ostream<_CharT, _Traits> ostream_type;
diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h b/libstdc++-v3/include/bits/streambuf_iterator.h
index fe612f39fe9..fc06c50040c 100644
--- a/libstdc++-v3/include/bits/streambuf_iterator.h
+++ b/libstdc++-v3/include/bits/streambuf_iterator.h
@@ -115,6 +115,11 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
       : _M_sbuf(0), _M_c(traits_type::eof()) { }
 
+#if __cplusplus > 201703L
+      constexpr istreambuf_iterator(default_sentinel_t) noexcept
+      : istreambuf_iterator() { }
+#endif
+
 #if __cplusplus >= 201103L
       istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
 
@@ -209,6 +214,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	const int_type __eof = traits_type::eof();
 	return traits_type::eq_int_type(__c, __eof);
       }
+
+#if __cplusplus > 201703L
+      friend bool
+      operator==(const istreambuf_iterator& __i, default_sentinel_t __s)
+      { return __i._M_at_eof(); }
+#endif
     };
 
   template<typename _CharT, typename _Traits>
diff --git a/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc b/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc
new file mode 100644
index 00000000000..77a19498817
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/istream_iterator/cons/sentinel.cc
@@ -0,0 +1,27 @@ 
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2020 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>
+
+// C++20 doesn't require this to be non-throwing.
+static_assert( std::is_nothrow_constructible_v<std::istream_iterator<int>,
+					       std::default_sentinel_t> );
+
+constexpr std::istream_iterator<int> i = std::default_sentinel;
diff --git a/libstdc++-v3/testsuite/24_iterators/istream_iterator/sentinel.cc b/libstdc++-v3/testsuite/24_iterators/istream_iterator/sentinel.cc
new file mode 100644
index 00000000000..ec0c7db56fd
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/istream_iterator/sentinel.cc
@@ -0,0 +1,57 @@ 
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+// Copyright (C) 2020 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 <sstream>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  static_assert( std::sentinel_for<std::default_sentinel_t,
+				   std::istream_iterator<std::string>> );
+  static_assert( std::sentinel_for<std::default_sentinel_t,
+				   std::istream_iterator<int>> );
+
+  std::istream_iterator<int> i = std::default_sentinel;
+  VERIFY( i == std::default_sentinel );
+  VERIFY( std::default_sentinel == i );
+}
+
+void
+test02()
+{
+  std::istringstream in("1 2 3");
+  std::istream_iterator<int> iter(in);
+  VERIFY( iter != std::default_sentinel );
+  VERIFY( std::default_sentinel != iter );
+
+  *iter++;
+  *iter++;
+  *iter++;
+  VERIFY( iter == std::default_sentinel );
+  VERIFY( std::default_sentinel == iter );
+}
+
+int main()
+{
+  test01();
+  test02();
+}
diff --git a/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/cons/sentinel.cc b/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/cons/sentinel.cc
new file mode 100644
index 00000000000..0b05fb532e5
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/istreambuf_iterator/cons/sentinel.cc
@@ -0,0 +1,26 @@ 
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2020 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>
+
+static_assert( std::is_nothrow_constructible_v<std::istreambuf_iterator<char>,
+					       std::default_sentinel_t> );
+
+constexpr std::istreambuf_iterator<char> i = std::default_sentinel;