I'm working with a non-modifiable interface that looks something like this:
struct Foo {
static Foo* get() { static Foo foo; return &foo; }
int (*func_ptr)(float, double);
int func_impl(float x, double y) { return 0; }
};
I'd like to bind func_ptr
so that it invokes func_impl
on the static get()
instance. Currently, I'm doing the following:
Foo::get()->func_ptr = [](float x, double y) -> int {
return Foo::get()->func_impl(x, y);
};
But I'd like this to be more concise. In particular, I'm trying to avoid duplicating the parameter types and names. After reviewing several related questions, I'm convinced that there's no way to generically assign the member function to the function pointer, but I think it still should be possible to assign it a specific implementation that happens to use Foo::get
using templates. Is this accurate? If so, how can I define such a binder function? I've tried things like the following, but I've hit a wall in defining the binder, and I'm not fluent in clang-template-error.
template<typename TFuncPtr, typename TFuncImpl, typename... TArgs>
void BindImplToPtr(TFuncPtr ptr, TFuncImpl impl) {
Foo::get()->(*ptr) = [](TArgs... args) -> int {
return Foo::get()->(*impl)(args...);
};
}
The goal would be to be able to bind the pointer and implementation using a single call:
BindImplToPtr(&Foo::func_ptr, &Foo::func_impl);