#include <iostream>
#include <functional>
#include <future>
template<class Fn, class... Args>
inline auto runTerminateOnException(Fn&& fn, Args&&... args) {
try {
return std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...)();
} catch (...) {
std::terminate();
}
}
template<class Fn, class... Args>
inline auto runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
return std::async(std::launch::async, runTerminateOnException<Fn, Args&&...>, std::forward<Fn>(fn), std::forward<Args>(args)...);
}
struct Foo {
void print() {
printf("Foo::print()\n");
}
};
int main() {
Foo foo;
std::future<void> future = runAsyncTerminateOnException(&Foo::print, &foo);
// your code goes here
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGZ1dHVyZT4KCnRlbXBsYXRlPGNsYXNzIEZuLCBjbGFzcy4uLiBBcmdzPgppbmxpbmUgYXV0byBydW5UZXJtaW5hdGVPbkV4Y2VwdGlvbihGbiYmIGZuLCBBcmdzJiYuLi4gYXJncykgewoJdHJ5IHsKCSAgICByZXR1cm4gc3RkOjpiaW5kKHN0ZDo6Zm9yd2FyZDxGbj4oZm4pLCBzdGQ6OmZvcndhcmQ8QXJncz4oYXJncykuLi4pKCk7Cgl9IGNhdGNoICguLi4pIHsKCQlzdGQ6OnRlcm1pbmF0ZSgpOwoJfQp9Cgp0ZW1wbGF0ZTxjbGFzcyBGbiwgY2xhc3MuLi4gQXJncz4KaW5saW5lIGF1dG8gcnVuQXN5bmNUZXJtaW5hdGVPbkV4Y2VwdGlvbihGbiYmIGZuLCBBcmdzJiYuLi4gYXJncykgewoJcmV0dXJuIHN0ZDo6YXN5bmMoc3RkOjpsYXVuY2g6OmFzeW5jLCBydW5UZXJtaW5hdGVPbkV4Y2VwdGlvbjxGbiwgQXJncyYmLi4uPiwgc3RkOjpmb3J3YXJkPEZuPihmbiksIHN0ZDo6Zm9yd2FyZDxBcmdzPihhcmdzKS4uLik7Cn0KCnN0cnVjdCBGb28gewoJdm9pZCBwcmludCgpIHsKCQlwcmludGYoIkZvbzo6cHJpbnQoKVxuIik7Cgl9Cn07CgppbnQgbWFpbigpIHsKCUZvbyBmb287CglzdGQ6OmZ1dHVyZTx2b2lkPiBmdXR1cmUgPSBydW5Bc3luY1Rlcm1pbmF0ZU9uRXhjZXB0aW9uKCZGb286OnByaW50LCAmZm9vKTsKCS8vIHlvdXIgY29kZSBnb2VzIGhlcmUKCXJldHVybiAwOwp9
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);
^