fork(3) download
  1. #include <iostream>
  2. #include <functional>
  3. #include <future>
  4.  
  5. template<class Fn, class... Args>
  6. inline auto runTerminateOnException(Fn&& fn, Args&&... args) {
  7. try {
  8. return std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...)();
  9. } catch (...) {
  10. std::terminate();
  11. }
  12. }
  13.  
  14. template<class Fn, class... Args>
  15. inline auto runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
  16. return std::async(std::launch::async, runTerminateOnException<Fn, Args&&...>, std::forward<Fn>(fn), std::forward<Args>(args)...);
  17. }
  18.  
  19. struct Foo {
  20. void print() {
  21. printf("Foo::print()\n");
  22. }
  23. };
  24.  
  25. int main() {
  26. Foo foo;
  27. std::future<void> future = runAsyncTerminateOnException(&Foo::print, &foo);
  28. // your code goes here
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'auto runAsyncTerminateOnException(Fn&&, Args&& ...) [with Fn = void (Foo::*)(); Args = {Foo*}]':
prog.cpp:27:75:   required from here
prog.cpp:16:19: error: no matching function for call to 'async(std::launch, <unresolved overloaded function type>, void (Foo::*)(), Foo*)'
  return std::async(std::launch::async, runTerminateOnException<Fn, Args&&...>, std::forward<Fn>(fn), std::forward<Args>(args)...);
                   ^
In file included from prog.cpp:3:0:
/usr/include/c++/5/future:1703:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     ^
/usr/include/c++/5/future:1703:5: note:   template argument deduction/substitution failed:
prog.cpp:16:19: note:   couldn't deduce template parameter '_Fn'
  return std::async(std::launch::async, runTerminateOnException<Fn, Args&&...>, std::forward<Fn>(fn), std::forward<Args>(args)...);
                   ^
In file included from prog.cpp:3:0:
/usr/include/c++/5/future:1723:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(_Fn&&, _Args&& ...)
     async(_Fn&& __fn, _Args&&... __args)
     ^
/usr/include/c++/5/future:1723:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/future: In substitution of 'template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {}]':
prog.cpp:16:19:   required from 'auto runAsyncTerminateOnException(Fn&&, Args&& ...) [with Fn = void (Foo::*)(); Args = {Foo*}]'
prog.cpp:27:75:   required from here
/usr/include/c++/5/future:1723:5: error: no type named 'type' in 'class std::result_of<std::launch()>'
prog.cpp: In function 'int main()':
prog.cpp:27:57: error: conversion from 'void' to non-scalar type 'std::future<void>' requested
  std::future<void> future = runAsyncTerminateOnException(&Foo::print, &foo);
                                                         ^
stdout
Standard output is empty