]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Make views::single/iota/istream SFINAE-friendly [PR108362]
authorPatrick Palka <ppalka@redhat.com>
Thu, 9 Mar 2023 18:35:04 +0000 (13:35 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 9 Mar 2023 18:35:04 +0000 (13:35 -0500)
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
libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
libstdc++-v3/testsuite/std/ranges/istream_view.cc
libstdc++-v3/testsuite/std/ranges/single_view.cc

index 0a65d74bb5bd1b4800044223cb1b9d43a0f5b802..867844e9e122007d8d6e9d59692a552401a87733 100644 (file)
@@ -675,30 +675,41 @@ namespace views
   template<typename _Tp>
     inline constexpr empty_view<_Tp> empty{};
 
-  struct _Single
+  namespace __detail
   {
     template<typename _Tp>
-      [[nodiscard]]
+      concept __can_single_view
+       = requires { single_view<decay_t<_Tp>>(std::declval<_Tp>()); };
+  } // namespace __detail
+
+  struct _Single
+  {
+    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,21 @@ 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);
+    };
+  } // namespace __detail
+
   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, remove_reference_t<decltype(__e)>>
        { return basic_istream_view<_Tp, _CharT, _Traits>(__e); }
     };
 
index 2dd17113536954ec4ddfd21e16f622f709141943..0d2eaf1d0c2f89f70c008a843862d3e9c5f8f62e 100644 (file)
@@ -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();
 }
index 26f109fdeaa3ea16387d69dc0045cc7d91591759..cc1c3e006b97507ae70f49713aa2e62b4c4afd7a 100644 (file)
@@ -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();
 }
index 063caeb87852d11a1b93d3ce442c3cac04385f7d..38a3946ca43c9a45acd66833bad25ec7a6ddac35 100644 (file)
@@ -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();
 }