I have a template which takes a function of some varying signature as an argument, and produces a related function of a simple signature. The details aren't terribly important, it's some kind of delegate. It looks roughly like this.
struct foo;
using desired_signature_t = int(*)(foo*);
template <typename F, F f>
struct make_delegate;
template <typename... Args, int(*target_func)(foo*, Args...)>
struct make_delegate<int(*)(foo*, Args...), target_func> {
static int delegate(foo *) { ... }
};
To use it, what I currently do is:
#define MAKE_DELEGATE(f) &make_delegate<decltype(f), (f)>::delegate
int bar(foo *, int, float, double);
desired_signature_t delegated_bar = MAKE_DELEGATE(&bar);
I use a macro because, I don't like having to type the argument twice, once for decltype and once for the actual function pointer.
Is there a way to do this without typing it twice and also without using a macro? The issue is that the type of a non-type template parameter cannot be deduced, and I couldn't find a way to deduce those types first in an earlier template. I thought about trying to construct a literal object with a template constructor as a helper, but I'm not sure if that's helpful -- I still can't use the parameter to the ctor as a template parameter inside the ctor, and once I'm out of the ctor I don't know the function type anymore.