I'm trying to make a function template that takes a function template as a template argument and then returns the result of that function when invoked with the normal function parameters passed in. It would be used like this:
auto fooPtr = func<std::make_unique, Foo>(...);
The point of the function is to allow template type deduction even when letting another function perform construction of an instance. I already do this manually in a lot of places in my code like this:
auto fooPtr = std::make_unique<decltype(Foo{...})>(...);
I got the idea of a helper function from this answer to a question I posted. He suggested to make one for a specific type but I want a function that can be used for any type.
Here's what I've come up with so far:
template
<auto F, template<typename U> class T, typename... Args>
std::result_of_t<decltype(F)>
func(Args&&... args, std::enable_if_t<std::is_invocable_v<decltype(F), Args...>>* = nullptr)
{
return F<decltype(T{std::forward<Args>(args)...})>(std::forward<Args>(args)...);
}
But I can't get it to work.
Am I on the right track? Is what I'm trying to do even possible?
...
) – Burierstd::make_unique
is a template function, they simply cannot be passed around. The cleanest solution is to wrap the provided example in a macro. – Godheaddecltype(T{std::forward<Args>(args)...})
alwaysT
? (Could be useful in an expression SFINAE context, but you're not using it there.) – MelanochroiT
was supposed to be a template type. Edited the question – Untuckstd::unique_ptr<Foo<Some, Template, Args>> fooPtr
? – Melanochroiauto
– Untuckf([](auto&&… args){ return your_function_template(std::forward<decltype(args)>(args)…); });
. – Subsist