This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Use std::decay in return type of std::async (LWGW 2021)


http://cplusplus.github.io/LWG/lwg-defects.html#2021 corrects the
return type of std::async(), which is necessary to ensure that the
result_of expression matches the actual result of the callable object
passed to async.

We already do the right thing for std::bind(), so only std::async()
needs changes.

Tested powerpc64le-linux, committed to trunk.

commit 93ea8027998a76eb93e248c509e6a25a59ba3a79
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu May 28 23:22:23 2015 +0100

    	* include/std/future (__async_result_of): New alias template.
    	(async): Use __async_result_of to pass decayed types to result_of.
    	* testsuite/30_threads/async/lwg2021.cc: New.
    	* doc/xml/manual/intro.xml: Document LWG 2021 status.

diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml
index b521089..f0c66a2 100644
--- a/libstdc++-v3/doc/xml/manual/intro.xml
+++ b/libstdc++-v3/doc/xml/manual/intro.xml
@@ -814,6 +814,12 @@ requirements of the license of GCC.
     <listitem><para>Return the end of the filled range.
     </para></listitem></varlistentry>
 
+    <varlistentry><term><link xmlns:xlink="http://www.w3.org/1999/xlink"; xlink:href="../ext/lwg-defects.html#2021">2021</link>:
+       <emphasis>Further incorrect uses of <code>result_of</code></emphasis>
+    </term>
+    <listitem><para>Correctly decay types in signature of <code>std::async</code>.
+    </para></listitem></varlistentry>
+
   </variablelist>
 
  </section>
diff --git a/libstdc++-v3/include/std/future b/libstdc++-v3/include/std/future
index c4baf90..216a921 100644
--- a/libstdc++-v3/include/std/future
+++ b/libstdc++-v3/include/std/future
@@ -168,12 +168,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     deferred
   };
 
+  // _GLIBCXX_RESOLVE_LIB_DEFECTS
+  // 2021. Further incorrect usages of result_of
   template<typename _Fn, typename... _Args>
-    future<typename result_of<_Fn(_Args...)>::type>
+    using __async_result_of = typename result_of<
+      typename decay<_Fn>::type(typename decay<_Args>::type...)>::type;
+
+  template<typename _Fn, typename... _Args>
+    future<__async_result_of<_Fn, _Args...>>
     async(launch __policy, _Fn&& __fn, _Args&&... __args);
 
   template<typename _Fn, typename... _Args>
-    future<typename result_of<_Fn(_Args...)>::type>
+    future<__async_result_of<_Fn, _Args...>>
     async(_Fn&& __fn, _Args&&... __args);
 
 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) \
@@ -727,7 +733,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       friend class promise<_Res>;
       template<typename> friend class packaged_task;
       template<typename _Fn, typename... _Args>
-        friend future<typename result_of<_Fn(_Args...)>::type>
+        friend future<__async_result_of<_Fn, _Args...>>
         async(launch, _Fn&&, _Args&&...);
 
       typedef __basic_future<_Res> _Base_type;
@@ -770,7 +776,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       friend class promise<_Res&>;
       template<typename> friend class packaged_task;
       template<typename _Fn, typename... _Args>
-        friend future<typename result_of<_Fn(_Args...)>::type>
+        friend future<__async_result_of<_Fn, _Args...>>
         async(launch, _Fn&&, _Args&&...);
 
       typedef __basic_future<_Res&> _Base_type;
@@ -813,7 +819,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       friend class promise<void>;
       template<typename> friend class packaged_task;
       template<typename _Fn, typename... _Args>
-        friend future<typename result_of<_Fn(_Args...)>::type>
+        friend future<__async_result_of<_Fn, _Args...>>
         async(launch, _Fn&&, _Args&&...);
 
       typedef __basic_future<void> _Base_type;
@@ -1699,10 +1705,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   /// async
   template<typename _Fn, typename... _Args>
-    future<typename result_of<_Fn(_Args...)>::type>
+    future<__async_result_of<_Fn, _Args...>>
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     {
-      typedef typename result_of<_Fn(_Args...)>::type result_type;
       std::shared_ptr<__future_base::_State_base> __state;
       if ((__policy & launch::async) == launch::async)
 	{
@@ -1714,16 +1719,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 	  __state = __future_base::_S_make_deferred_state(std::__bind_simple(
               std::forward<_Fn>(__fn), std::forward<_Args>(__args)...));
 	}
-      return future<result_type>(__state);
+      return future<__async_result_of<_Fn, _Args...>>(__state);
     }
 
   /// async, potential overload
   template<typename _Fn, typename... _Args>
-    inline future<typename result_of<_Fn(_Args...)>::type>
+    inline future<__async_result_of<_Fn, _Args...>>
     async(_Fn&& __fn, _Args&&... __args)
     {
-      return async(launch::async|launch::deferred, std::forward<_Fn>(__fn),
-		   std::forward<_Args>(__args)...);
+      return std::async(launch::async|launch::deferred,
+			std::forward<_Fn>(__fn),
+			std::forward<_Args>(__args)...);
     }
 
 #endif // _GLIBCXX_ASYNC_ABI_COMPAT
diff --git a/libstdc++-v3/testsuite/30_threads/async/lwg2021.cc b/libstdc++-v3/testsuite/30_threads/async/lwg2021.cc
new file mode 100644
index 0000000..0c0ac3b
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/async/lwg2021.cc
@@ -0,0 +1,42 @@
+// Copyright (C) 2015 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } }
+// { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } }
+// { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } }
+// { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-darwin* } }
+// { dg-require-cstdint "" }
+// { dg-require-gthreads "" }
+// { dg-require-atomic-builtins "" }
+
+// LWG 2021. Further incorrect usages of result_of
+// Arguments to result_of should use decay.
+
+#include <future>
+
+struct A
+{
+  int operator()(int&&)&& { return 0; }
+  void operator()(int&)& { }
+};
+
+int main()
+{
+  A a;
+  int i = 0;
+  std::future<int> f = std::async(a, i);
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]