]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Make ranges::to closure objects SFINAE-friendly [PR112802]
authorPatrick Palka <ppalka@redhat.com>
Mon, 18 Dec 2023 23:05:36 +0000 (18:05 -0500)
committerPatrick Palka <ppalka@redhat.com>
Mon, 18 Dec 2023 23:05:36 +0000 (18:05 -0500)
This also happens to fix composition of these closure objects.

PR libstdc++/112802
PR libstdc++/113068

libstdc++-v3/ChangeLog:

* include/std/ranges (__detail::_To::operator()): Add constraints.
(__detail::_To2::operator()): Likewise.
* testsuite/std/ranges/conv/1.cc (test_sfinae): New test.
(test_composition): New test.

libstdc++-v3/include/std/ranges
libstdc++-v3/testsuite/std/ranges/conv/1.cc

index be8475c0cb1ba4f75131cb55bb587c1587b1ded3..752a04e01bd5692567ae9490a466c1eccd50a95f 100644 (file)
@@ -9389,6 +9389,8 @@ namespace __detail
     struct _To
     {
       template<typename _Range, typename... _Args>
+       requires requires { ranges::to<_Cont>(std::declval<_Range>(),
+                                             std::declval<_Args>()...); }
        constexpr auto
        operator()(_Range&& __r, _Args&&... __args) const
        {
@@ -9431,6 +9433,8 @@ namespace __detail
     struct _To2
     {
       template<typename _Range, typename... _Args>
+       requires requires { ranges::to<_Cont>(std::declval<_Range>(),
+                                             std::declval<_Args>()...); }
        constexpr auto
        operator()(_Range&& __r, _Args&&... __args) const
        {
index 6d6a708ab64c68cb37b5cda513b3f6b2d12ea9b3..09fd515edf1b585089cc2316b0c90cf46925266e 100644 (file)
@@ -448,6 +448,24 @@ test_constexpr()
   static_assert(x == 6);
 }
 
+void
+test_sfinae()
+{
+  // PR libstdc++/112802
+  [](auto x) {
+    static_assert(!requires { std::ranges::to<std::vector<int>>()(x); });
+    static_assert(!requires { std::ranges::to<std::vector>()(x); });
+  }(0);
+}
+
+void
+test_composition()
+{
+  // PR libstdc++/113068
+  auto adaptor = std::ranges::to<std::string>() | std::ranges::to<std::string>();
+  auto str = adaptor(" ");
+}
+
 int main()
 {
   test_p1206r7_examples();
@@ -460,4 +478,6 @@ int main()
   test_lwg3984();
   test_nodiscard();
   test_constexpr();
+  test_sfinae();
+  test_composition();
 }