libstdc++: Implement P2432R1 changes for views::istream

Message ID 20211021143728.2107631-1-ppalka@redhat.com
State Committed
Headers
Series libstdc++: Implement P2432R1 changes for views::istream |

Commit Message

Patrick Palka Oct. 21, 2021, 2:37 p.m. UTC
  Tested on x86_64-pc-linux-gnu, does this look OK for trunk/11/10?

libstdc++-v3/ChangeLog:

	* include/std/ranges (istream_view): Replace this function
	template with an alias template as per P2432R1.
	(wistream_view): Define as per P2432R1.
	(views::_Istream, views::istream): Likewise.
	* testsuite/std/ranges/istream_view.cc (test07): New test.
---
 libstdc++-v3/include/std/ranges               | 25 ++++++++++++++++---
 .../testsuite/std/ranges/istream_view.cc      | 13 ++++++++++
 2 files changed, 34 insertions(+), 4 deletions(-)
  

Comments

Jonathan Wakely Oct. 21, 2021, 3:18 p.m. UTC | #1
On Thu, 21 Oct 2021 at 15:41, Patrick Palka via Libstdc++ <
libstdc++@gcc.gnu.org> wrote:

> Tested on x86_64-pc-linux-gnu, does this look OK for trunk/11/10?
>

Yes for trunk.

I'm undecided whether we want to make this breaking change on the branches.



> libstdc++-v3/ChangeLog:
>
>         * include/std/ranges (istream_view): Replace this function
>         template with an alias template as per P2432R1.
>         (wistream_view): Define as per P2432R1.
>         (views::_Istream, views::istream): Likewise.
>         * testsuite/std/ranges/istream_view.cc (test07): New test.
> ---
>  libstdc++-v3/include/std/ranges               | 25 ++++++++++++++++---
>  .../testsuite/std/ranges/istream_view.cc      | 13 ++++++++++
>  2 files changed, 34 insertions(+), 4 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/ranges
> b/libstdc++-v3/include/std/ranges
> index 30ba0606869..7aa0f9dfc67 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -781,10 +781,27 @@ namespace views
>        friend _Iterator;
>      };
>
> -  template<typename _Val, typename _CharT, typename _Traits>
> -    basic_istream_view<_Val, _CharT, _Traits>
> -    istream_view(basic_istream<_CharT, _Traits>& __s)
> -    { return basic_istream_view<_Val, _CharT, _Traits>{__s}; }
> +  template<typename _Val>
> +    using istream_view = basic_istream_view<_Val, char>;
> +
> +  template<typename _Val>
> +    using wistream_view = basic_istream_view<_Val, wchar_t>;
> +
> +namespace views
> +{
> +  template<typename _Tp>
> +    struct _Istream
> +    {
> +      template<typename _CharT, typename _TraitsT>
> +       [[nodiscard]]
> +       constexpr auto
> +       operator()(basic_istream<_CharT, _TraitsT>& __e) const
> +       { return basic_istream_view<_Tp, _CharT, _TraitsT>(__e); }
> +    };
> +
> +  template<typename _Tp>
> +    inline constexpr _Istream<_Tp> istream;
> +}
>
>    // C++20 24.7 [range.adaptors] Range adaptors
>
> diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> index f5c0c2a6bb0..ea7c5a35ef7 100644
> --- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> @@ -103,6 +103,18 @@ test06()
>    static_assert( std::is_same_v<V, W> );
>  }
>
> +void
> +test07()
> +{
> +  // P2432R1, views::istream
> +  auto nums = std::istringstream("0 1 2 3 4");
> +  ranges::istream_view<int> v(nums);
> +  int sum = 0;
> +  for (int val : views::istream<int>(nums))
> +    sum += val;
> +  VERIFY( sum == 10 );
> +}
> +
>  int
>  main()
>  {
> @@ -112,4 +124,5 @@ main()
>    test04();
>    test05();
>    test06();
> +  test07();
>  }
> --
> 2.33.1.711.g9d530dc002
>
>
  

Patch

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 30ba0606869..7aa0f9dfc67 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -781,10 +781,27 @@  namespace views
       friend _Iterator;
     };
 
-  template<typename _Val, typename _CharT, typename _Traits>
-    basic_istream_view<_Val, _CharT, _Traits>
-    istream_view(basic_istream<_CharT, _Traits>& __s)
-    { return basic_istream_view<_Val, _CharT, _Traits>{__s}; }
+  template<typename _Val>
+    using istream_view = basic_istream_view<_Val, char>;
+
+  template<typename _Val>
+    using wistream_view = basic_istream_view<_Val, wchar_t>;
+
+namespace views
+{
+  template<typename _Tp>
+    struct _Istream
+    {
+      template<typename _CharT, typename _TraitsT>
+	[[nodiscard]]
+	constexpr auto
+	operator()(basic_istream<_CharT, _TraitsT>& __e) const
+	{ return basic_istream_view<_Tp, _CharT, _TraitsT>(__e); }
+    };
+
+  template<typename _Tp>
+    inline constexpr _Istream<_Tp> istream;
+}
 
   // C++20 24.7 [range.adaptors] Range adaptors
 
diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
index f5c0c2a6bb0..ea7c5a35ef7 100644
--- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
@@ -103,6 +103,18 @@  test06()
   static_assert( std::is_same_v<V, W> );
 }
 
+void
+test07()
+{
+  // P2432R1, views::istream
+  auto nums = std::istringstream("0 1 2 3 4");
+  ranges::istream_view<int> v(nums);
+  int sum = 0;
+  for (int val : views::istream<int>(nums))
+    sum += val;
+  VERIFY( sum == 10 );
+}
+
 int
 main()
 {
@@ -112,4 +124,5 @@  main()
   test04();
   test05();
   test06();
+  test07();
 }