I have template class and some Policies to use it. It just execute lambdas with specific policy.
template<template<class>class Policy, typename ReturnType>
class execute :Policy<ReturnType>
{
public:
ReturnType res;
execute(ReturnType(*func)())
{res = Policy<ReturnType>().launch(func);}
};
template<typename ReturnType>
struct Policy1 {
ReturnType launch(ReturnType(*func)())
{/*some code*/
return func();}
};
template<typename ReturnType>
struct Policy2 {
ReturnType launch(ReturnType(*func)())
{/*some code*/
return func();}
};
int main()
{
auto lambda_int = [] {return int(1); };
auto lambda_float = [] {return float(1.0); };
execute<Policy1, decltype(lambda_int())> exec1(lambda_int);
execute<Policy2, decltype(lambda_float())> exec2(lambda_float);
}
Client code work exactly what i need. But i point to lambda twice and i wanna reduce class variable declaration in client code to this:
execute<Policy1> exec3(lambda_float);
As i understand i cant use something like tempalte<auto ReturnType>
because it works only with single argument.
I also cannot use default parameter because i wanna pass any type of lambda return.
template<template<class>class Policy, typename ReturnType=int>
class execute :Policy<ReturnType>
The question is how to pass function return type, which is constructor args, to class template arguments? Or may be there is another way?
UPDATE: Based on Secundi answer.
template<template<class>class Policy, typename FunctorType>
auto create_executor(FunctorType f)
{
return execute<Policy, decltype(f())>(f);
}
int main(){
auto ce5 = create_executor<Policy2>(lambda_int);
}