dimanche 28 juin 2015

Deduct return and parameter types of function pointer passed as non-type template argument

I know that it is possible for the compiler to infer types for function pointers if said pointers are passed into my template function as arguments, but I want to pass mine as a non-type template argument and support an arbitrary number and types of arguments.

In spirit, what I want to do is this:

template <Ret (*Fun)(Args...), typename Ret, typename... Args>
Ret do_something_exciting(Args&&... args)
{
    // exciting things go here
    return Fun(std::forwards<Args>(args)...);
}

[...]

int some_arbitrary_function(double x, double y);

[...]

do_something_exciting<&some_arbitrary_function>(3.14, 9.82);

This obviously fails due to Fun being declared before Ret and Args, but if I were to declare Fun after Ret and Args, I could not use type deduction in the first place.

Am I attempting the impossible or can this be made to work?

Aucun commentaire:

Enregistrer un commentaire