Skip to content

Instantly share code, notes, and snippets.

@ggthedev
Forked from goldshtn/cpp11.cpp
Last active August 29, 2015 14:27
Show Gist options
  • Save ggthedev/361302339a05866ca012 to your computer and use it in GitHub Desktop.
Save ggthedev/361302339a05866ca012 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…
#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