-
-
Save jdiego/da7ab9caf6d38e440102 to your computer and use it in GitHub Desktop.
A single function that uses a bunch of C++11/14 features and for "old-school" C++ developers will not even read like C++ anymore.Specifically, it uses:- lambda functions (C++11) with generalized capture semantics (C++14)- rvalue references (C++11)- auto variables (C++11)- decltype and trailing function return type syntax (C++11)- std::move and s…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <future> | |
using namespace std; | |
template <typename Fn, typename... Args> | |
auto do_async_with_log(ostream& os, Fn&& fn, Args&&... args) -> | |
future<decltype(fn(args...))> | |
{ | |
os << "[TID=" << this_thread::get_id() | |
<< "] Starting to invoke function..." << endl; | |
auto bound = bind(fn, forward<Args&&...>(args...)); | |
return async([b=move(bound),&os]() mutable { | |
auto result = b(); | |
os << "[TID=" << this_thread::get_id() | |
<< "] ...invocation done, returning " << result << endl; | |
return result; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment