]> gcc.gnu.org Git - gcc.git/commitdiff
libstdc++: Adjust deduction guides for static operator() [PR106651]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 27 Sep 2022 10:25:51 +0000 (11:25 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 27 Sep 2022 10:59:53 +0000 (11:59 +0100)
Adjust the deduction guides for std::function and std::packaged_task to
work with static call operators. This finishes the implementation of
P1169R4 for C++23.

libstdc++-v3/ChangeLog:

PR c++/106651
* include/bits/std_function.h (__function_guide_t): New alias
template.
[__cpp_static_call_operator] (__function_guide_static_helper):
New class template.
(function): Use __function_guide_t in deduction guide.
* include/std/future (packaged_task): Use __function_guide_t in
deduction guide.
* testsuite/20_util/function/cons/deduction_c++23.cc: New test.
* testsuite/30_threads/packaged_task/cons/deduction_c++23.cc:
New test.

libstdc++-v3/include/bits/std_function.h
libstdc++-v3/include/std/future
libstdc++-v3/testsuite/20_util/function/cons/deduction_c++23.cc [new file with mode: 0644]
libstdc++-v3/testsuite/30_threads/packaged_task/cons/deduction_c++23.cc [new file with mode: 0644]

index 96918a04a35d4ae3afd93efad466b526f5c8061b..f5423a3a5c73f0a8fe9af13639ca92db4d710998 100644 (file)
@@ -697,12 +697,31 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     >
     { using type = _Res(_Args...); };
 
+#if __cpp_static_call_operator >= 202207L && __cpp_concepts >= 202002L
+  template<typename _StaticCallOp>
+    struct __function_guide_static_helper
+    { };
+
+  template<typename _Res, bool _Nx, typename... _Args>
+    struct __function_guide_static_helper<_Res (*) (_Args...) noexcept(_Nx)>
+    { using type = _Res(_Args...); };
+
+  template<typename _Fn, typename _Op>
+    using __function_guide_t = typename __conditional_t<
+      requires (_Fn& __f) { (void) __f.operator(); },
+      __function_guide_static_helper<_Op>,
+      __function_guide_helper<_Op>>::type;
+#else
+  template<typename _Fn, typename _Op>
+    using __function_guide_t = typename __function_guide_helper<_Op>::type;
+#endif
+
   template<typename _Res, typename... _ArgTypes>
     function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>;
 
-  template<typename _Functor, typename _Signature = typename
-          __function_guide_helper<decltype(&_Functor::operator())>::type>
-    function(_Functor) -> function<_Signature>;
+  template<typename _Fn, typename _Signature
+            = __function_guide_t<_Fn, decltype(&_Fn::operator())>>
+    function(_Fn) -> function<_Signature>;
 #endif
 
   // [20.7.15.2.6] null pointer comparisons
index a1b2d7f1d3afb0c7a372736e7f381277074ef927..cf08c155a24bfab203ab5a0940cadd7992b6bdcf 100644 (file)
@@ -1649,8 +1649,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Res, typename... _ArgTypes>
     packaged_task(_Res(*)(_ArgTypes...)) -> packaged_task<_Res(_ArgTypes...)>;
 
-  template<typename _Fun, typename _Signature = typename
-          __function_guide_helper<decltype(&_Fun::operator())>::type>
+  template<typename _Fun, typename _Signature
+            = __function_guide_t<_Fun, decltype(&_Fun::operator())>>
     packaged_task(_Fun) -> packaged_task<_Signature>;
 #endif
 
diff --git a/libstdc++-v3/testsuite/20_util/function/cons/deduction_c++23.cc b/libstdc++-v3/testsuite/20_util/function/cons/deduction_c++23.cc
new file mode 100644 (file)
index 0000000..17454ea
--- /dev/null
@@ -0,0 +1,23 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <functional>
+
+template<typename T, typename U> struct require_same;
+template<typename T> struct require_same<T, T> { using type = void; };
+
+template<typename T, typename U>
+  typename require_same<T, U>::type
+  check_type(U&) { }
+
+void
+test_static_call_operator()
+{
+  struct F1 { static long operator()() { return 0; } };
+  std::function func1 = F1{};
+  check_type<std::function<long()>>(func1);
+
+  struct F2 { static float operator()(char, void*) noexcept { return 0; } };
+  std::function func2 = F2{};
+  check_type<std::function<float(char, void*)>>(func2);
+}
diff --git a/libstdc++-v3/testsuite/30_threads/packaged_task/cons/deduction_c++23.cc b/libstdc++-v3/testsuite/30_threads/packaged_task/cons/deduction_c++23.cc
new file mode 100644 (file)
index 0000000..e36edfa
--- /dev/null
@@ -0,0 +1,23 @@
+// { dg-options "-std=gnu++23" }
+// { dg-do compile { target c++23 } }
+
+#include <future>
+
+template<typename T, typename U> struct require_same;
+template<typename T> struct require_same<T, T> { using type = void; };
+
+template<typename T, typename U>
+  typename require_same<T, U>::type
+  check_type(U&) { }
+
+void
+test_static_call_operator()
+{
+  struct F1 { static long operator()() { return 0; } };
+  std::packaged_task task1{ F1{} };
+  check_type<std::packaged_task<long()>>(task1);
+
+  struct F2 { static float operator()(char, void*) noexcept { return 0; } };
+  std::packaged_task task2{ F2{} };
+  check_type<std::packaged_task<float(char, void*)>>(task2);
+}
This page took 0.064682 seconds and 5 git commands to generate.