pmalek
11/12/2013 - 3:50 PM

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.Specificall

A single function that uses a bunch of C11/14 features and for "old-school" C developers will not even read like C++ anymore.Specifically, it uses:- lambda functions (C11) with generalized capture semantics (C14)- rvalue references (C11)- auto variables (C11)- decltype and trailing function return type syntax (C11)- std::move and std::forward (C11)- variadic templates (C11)- std::thread and std::future (C11)

#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;
	});
}