I'm trying to create a delayable call object. Something along the lines of (pseudo-code):
template <class FN>
struct delayable_call
{
return-type-of-FN call(); // <-- I'd like to use result_of here.
template<class ArgTypes...>
delayable_call(FN* pFn, ArgTypes... args);
FN* fn;
args-saving-struct;
};
I tried using result_of::type for the return type of call, but get errors during instantiation of the template because apparently the argument types need to be specified separately.
Instantiation:
int foo(bool, double); // function prototype.
delayable_call<int(bool, double)> delayable_foo(foo, false, 3.14); // instantiation
The error messages and documentation I've read about result_of seem to indicate that the argument types must also be specified. So instead of result_of<FN>::type
, I'd need to specify result_of<FN(bool, double)>::type
. This does actually fix the compilation problem I'm having, but breaks the generality of the template.
So, how can I use result_of with a template parameter when the template parameter represents the function signature?
replace your
delayable_call
with a specialization, and you will extrace bothR
andArgs...
. You needArgs...
anyhow to store the parameters.However, a library-strength delayable call will end up using type erasure. The easiest way is a simple
std::function<R()>
where you shove a lambda into it:and capture by value unless you really, really mean it to capture by reference.
You could deduce
R
via:which should deduce your
R
from the callable object and the arguments. This captures everything by copy -- capture by move requires either more boilerplate, or C++14.