fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <future>
  4.  
  5. template<class Fn, class... Args>
  6. inline auto runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
  7. auto make_call = std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...);
  8.  
  9. return std::async(std::launch::async, [=]() -> decltype(make_call()) {
  10. try {
  11. return make_call();
  12. } catch (...) {
  13. std::cout << "Terminate Called!" << std::endl;
  14. std::terminate();
  15. }
  16. });
  17. }
  18.  
  19. struct Foo {
  20. template<class... Args>
  21. void print(Args&&... args) {
  22. printf("Foo::print(%d)\n", std::forward<Args>(args)...);
  23. }
  24. };
  25.  
  26. int main() {
  27. Foo foo;
  28. std::future<void> future = std::async(std::launch::async, &Foo::print<int>, &foo, 2);
  29. // your code goes here
  30. return 0;
  31. }
Success #stdin #stdout 0s 5528KB
stdin
Standard input is empty
stdout
Foo::print(2)