public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362]
@ 2023-03-08 14:35 Patrick Palka
  2023-03-09 17:52 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-03-08 14:35 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, Patrick Palka

Tested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 12?

	PR libstdc++/108362

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::__can_single_view): New concept.
	(_Single::operator()): Constrain it.  Move [[nodiscard]] to the
	end of the function declarator.
	(__detail::__can_iota_view): New concept.
	(_Iota::operator()): Constrain it.  Move [[nodiscard]] to the
	end of the function declarator.
	(__detail::__can_istream_view): New concept.
	(_Istream::operator()): Constrain it.  Move [[nodiscard]] to the
	end of the function declarator.
	* testsuite/std/ranges/iota/iota_view.cc (test07): New test.
	* testsuite/std/ranges/istream_view.cc (test08): New test.
	* testsuite/std/ranges/single_view.cc (test07): New test.
---
 libstdc++-v3/include/std/ranges               | 40 ++++++++++++++-----
 .../testsuite/std/ranges/iota/iota_view.cc    | 10 +++++
 .../testsuite/std/ranges/istream_view.cc      | 12 ++++++
 .../testsuite/std/ranges/single_view.cc       | 13 ++++++
 4 files changed, 65 insertions(+), 10 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 0a65d74bb5b..67566c6ebcf 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -675,30 +675,41 @@ namespace views
   template<typename _Tp>
     inline constexpr empty_view<_Tp> empty{};
 
+  namespace __detail
+  {
+    template<typename _Tp>
+      concept __can_single_view
+	= requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
+  } // namespace __detail
+
   struct _Single
   {
-    template<typename _Tp>
-      [[nodiscard]]
+    template<__detail::__can_single_view _Tp>
       constexpr auto
-      operator()(_Tp&& __e) const
+      operator() [[nodiscard]] (_Tp&& __e) const
       noexcept(noexcept(single_view<decay_t<_Tp>>(std::forward<_Tp>(__e))))
       { return single_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
   };
 
   inline constexpr _Single single{};
 
+  namespace __detail
+  {
+    template<typename... _Args>
+      concept __can_iota_view = requires { iota_view(std::declval<_Args>()...); };
+  } // namespace __detail
+
   struct _Iota
   {
-    template<typename _Tp>
-      [[nodiscard]]
+    template<__detail::__can_iota_view _Tp>
       constexpr auto
-      operator()(_Tp&& __e) const
+      operator() [[nodiscard]] (_Tp&& __e) const
       { return iota_view(std::forward<_Tp>(__e)); }
 
     template<typename _Tp, typename _Up>
-      [[nodiscard]]
+      requires __detail::__can_iota_view<_Tp, _Up>
       constexpr auto
-      operator()(_Tp&& __e, _Up&& __f) const
+      operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
       { return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); }
   };
 
@@ -796,13 +807,22 @@ namespace views
 
 namespace views
 {
+  namespace __detail
+  {
+    template<typename _Tp, typename _Up>
+    concept __can_istream_view
+      = requires (_Up __e) {
+	basic_istream_view<_Tp, typename _Up::char_type, typename _Up::traits_type>(__e);
+      };
+  };
+
   template<typename _Tp>
     struct _Istream
     {
       template<typename _CharT, typename _Traits>
-	[[nodiscard]]
 	constexpr auto
-	operator()(basic_istream<_CharT, _Traits>& __e) const
+	operator() [[nodiscard]] (basic_istream<_CharT, _Traits>& __e) const
+	requires __detail::__can_istream_view<_Tp, std::remove_reference_t<decltype(__e)>>
 	{ return basic_istream_view<_Tp, _CharT, _Traits>(__e); }
     };
 
diff --git a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
index 2dd17113536..0d2eaf1d0c2 100644
--- a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
@@ -110,6 +110,15 @@ test06()
   VERIFY( std::ranges::equal(v3, w3) );
 }
 
+template<auto iota = std::views::iota>
+void
+test07()
+{
+  // Verify SFINAE behavior.
+  static_assert(!requires { iota(nullptr); });
+  static_assert(!requires { iota(nullptr, nullptr); });
+}
+
 int
 main()
 {
@@ -119,4 +128,5 @@ main()
   test04();
   test05();
   test06();
+  test07();
 }
diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
index 26f109fdeaa..cc1c3e006b9 100644
--- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
@@ -115,6 +115,17 @@ test07()
   VERIFY( sum == 10 );
 }
 
+template<class T, class U>
+concept can_istream_view = requires (U u) { views::istream<T>(u); };
+
+void
+test08()
+{
+  // Verify SFINAE behavior.
+  struct S { };
+  static_assert(!can_istream_view<S, std::istringstream>);
+}
+
 int
 main()
 {
@@ -125,4 +136,5 @@ main()
   test05();
   test06();
   test07();
+  test08();
 }
diff --git a/libstdc++-v3/testsuite/std/ranges/single_view.cc b/libstdc++-v3/testsuite/std/ranges/single_view.cc
index 063caeb8785..38a3946ca43 100644
--- a/libstdc++-v3/testsuite/std/ranges/single_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/single_view.cc
@@ -111,6 +111,18 @@ test06()
   auto y = std::views::single(std::move(obj));
 }
 
+template<auto single = std::views::single>
+void
+test07()
+{
+  // Verify SFINAE behavior.
+  struct uncopyable {
+    uncopyable();
+    uncopyable(const uncopyable&) = delete;
+  };
+  static_assert(!requires { single(uncopyable{}); });
+}
+
 int main()
 {
   test01();
@@ -119,4 +131,5 @@ int main()
   test04();
   test05();
   test06();
+  test07();
 }
-- 
2.40.0.rc0.57.g454dfcbddf


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362]
  2023-03-08 14:35 [PATCH] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362] Patrick Palka
@ 2023-03-09 17:52 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-03-09 17:52 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches, libstdc++

On Wed, 8 Mar 2023 at 14:36, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 12?

Yes, OK for trunk and 12.

>
>         PR libstdc++/108362
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/ranges (__detail::__can_single_view): New concept.
>         (_Single::operator()): Constrain it.  Move [[nodiscard]] to the
>         end of the function declarator.
>         (__detail::__can_iota_view): New concept.
>         (_Iota::operator()): Constrain it.  Move [[nodiscard]] to the
>         end of the function declarator.
>         (__detail::__can_istream_view): New concept.
>         (_Istream::operator()): Constrain it.  Move [[nodiscard]] to the
>         end of the function declarator.
>         * testsuite/std/ranges/iota/iota_view.cc (test07): New test.
>         * testsuite/std/ranges/istream_view.cc (test08): New test.
>         * testsuite/std/ranges/single_view.cc (test07): New test.
> ---
>  libstdc++-v3/include/std/ranges               | 40 ++++++++++++++-----
>  .../testsuite/std/ranges/iota/iota_view.cc    | 10 +++++
>  .../testsuite/std/ranges/istream_view.cc      | 12 ++++++
>  .../testsuite/std/ranges/single_view.cc       | 13 ++++++
>  4 files changed, 65 insertions(+), 10 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index 0a65d74bb5b..67566c6ebcf 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -675,30 +675,41 @@ namespace views
>    template<typename _Tp>
>      inline constexpr empty_view<_Tp> empty{};
>
> +  namespace __detail
> +  {
> +    template<typename _Tp>
> +      concept __can_single_view
> +       = requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
> +  } // namespace __detail
> +
>    struct _Single
>    {
> -    template<typename _Tp>
> -      [[nodiscard]]
> +    template<__detail::__can_single_view _Tp>
>        constexpr auto
> -      operator()(_Tp&& __e) const
> +      operator() [[nodiscard]] (_Tp&& __e) const
>        noexcept(noexcept(single_view<decay_t<_Tp>>(std::forward<_Tp>(__e))))
>        { return single_view<decay_t<_Tp>>(std::forward<_Tp>(__e)); }
>    };
>
>    inline constexpr _Single single{};
>
> +  namespace __detail
> +  {
> +    template<typename... _Args>
> +      concept __can_iota_view = requires { iota_view(std::declval<_Args>()...); };
> +  } // namespace __detail
> +
>    struct _Iota
>    {
> -    template<typename _Tp>
> -      [[nodiscard]]
> +    template<__detail::__can_iota_view _Tp>
>        constexpr auto
> -      operator()(_Tp&& __e) const
> +      operator() [[nodiscard]] (_Tp&& __e) const
>        { return iota_view(std::forward<_Tp>(__e)); }
>
>      template<typename _Tp, typename _Up>
> -      [[nodiscard]]
> +      requires __detail::__can_iota_view<_Tp, _Up>
>        constexpr auto
> -      operator()(_Tp&& __e, _Up&& __f) const
> +      operator() [[nodiscard]] (_Tp&& __e, _Up&& __f) const
>        { return iota_view(std::forward<_Tp>(__e), std::forward<_Up>(__f)); }
>    };
>
> @@ -796,13 +807,22 @@ namespace views
>
>  namespace views
>  {
> +  namespace __detail
> +  {
> +    template<typename _Tp, typename _Up>
> +    concept __can_istream_view
> +      = requires (_Up __e) {
> +       basic_istream_view<_Tp, typename _Up::char_type, typename _Up::traits_type>(__e);
> +      };
> +  };
> +
>    template<typename _Tp>
>      struct _Istream
>      {
>        template<typename _CharT, typename _Traits>
> -       [[nodiscard]]
>         constexpr auto
> -       operator()(basic_istream<_CharT, _Traits>& __e) const
> +       operator() [[nodiscard]] (basic_istream<_CharT, _Traits>& __e) const
> +       requires __detail::__can_istream_view<_Tp, std::remove_reference_t<decltype(__e)>>
>         { return basic_istream_view<_Tp, _CharT, _Traits>(__e); }
>      };
>
> diff --git a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
> index 2dd17113536..0d2eaf1d0c2 100644
> --- a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
> @@ -110,6 +110,15 @@ test06()
>    VERIFY( std::ranges::equal(v3, w3) );
>  }
>
> +template<auto iota = std::views::iota>
> +void
> +test07()
> +{
> +  // Verify SFINAE behavior.
> +  static_assert(!requires { iota(nullptr); });
> +  static_assert(!requires { iota(nullptr, nullptr); });
> +}
> +
>  int
>  main()
>  {
> @@ -119,4 +128,5 @@ main()
>    test04();
>    test05();
>    test06();
> +  test07();
>  }
> diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> index 26f109fdeaa..cc1c3e006b9 100644
> --- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> @@ -115,6 +115,17 @@ test07()
>    VERIFY( sum == 10 );
>  }
>
> +template<class T, class U>
> +concept can_istream_view = requires (U u) { views::istream<T>(u); };
> +
> +void
> +test08()
> +{
> +  // Verify SFINAE behavior.
> +  struct S { };
> +  static_assert(!can_istream_view<S, std::istringstream>);
> +}
> +
>  int
>  main()
>  {
> @@ -125,4 +136,5 @@ main()
>    test05();
>    test06();
>    test07();
> +  test08();
>  }
> diff --git a/libstdc++-v3/testsuite/std/ranges/single_view.cc b/libstdc++-v3/testsuite/std/ranges/single_view.cc
> index 063caeb8785..38a3946ca43 100644
> --- a/libstdc++-v3/testsuite/std/ranges/single_view.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/single_view.cc
> @@ -111,6 +111,18 @@ test06()
>    auto y = std::views::single(std::move(obj));
>  }
>
> +template<auto single = std::views::single>
> +void
> +test07()
> +{
> +  // Verify SFINAE behavior.
> +  struct uncopyable {
> +    uncopyable();
> +    uncopyable(const uncopyable&) = delete;
> +  };
> +  static_assert(!requires { single(uncopyable{}); });
> +}
> +
>  int main()
>  {
>    test01();
> @@ -119,4 +131,5 @@ int main()
>    test04();
>    test05();
>    test06();
> +  test07();
>  }
> --
> 2.40.0.rc0.57.g454dfcbddf
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-09 17:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 14:35 [PATCH] libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362] Patrick Palka
2023-03-09 17:52 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).