I'm trying to make a helper function that executes a lambda/std::function when called if the given weak_ptr is valid. Currently the following code works, but unfortunately, it requires me to define the template parameters. I'm looking for a way to do this with automatic template argument deduction.
template <typename DependentType, typename... ArgumentTypes>
auto make_dependent(std::weak_ptr<DependentType>& dependent, std::function < void(ArgumentTypes...)> functor) -> decltype(functor)
{
return [&dependent, functor] (ArgumentTypes... args)
{
if (!dependent.expired()) {
functor(args...);
}
};
};
Ideally, I would like to replace the std::function <void(ArgumentTypes...)>
with a generic template parameter FunctorType
, but then I'm not sure how I would extract arguments from FunctorType
. The above code works, the below code is theoretical:
template <typename DependentType, typename FunctorType>
auto make_dependent_ideal(std::weak_ptr<DependentType>& dependent, FunctorType functor) -> decltype(std::function<return_value(functor)(argument_list(functor))>)
{
return[&dependent, functor](argument_list(functor) args)
{
if (!dependent.expired()) {
functor(args...);
}
}
}
Is there any way to do something like this?