Is it possible to create a standalone template function which has a template parameter auto MEMFN
(a member function pointer), and has the same return and parameter types as MEMFN has?
So, if MEMFN's type is
RETURN (OBJECT::*)(PARAMETERS...)
then the desired function is this:
template <auto MEMFN>
RETURN foo(OBJECT &, PARAMETERS...);
My problem is how to extract PARAMETERS...
from MEMFN's type
(RETURN
and OBJECT
are easy to do).
So I can call this function like this:
Object o;
foo<&Object::func>(o, <parameters>...);
As for request from n.m., here is a stripped down example from an actual code:
#include <utility>
template <typename RETURN, typename OBJECT, typename ...PARAMETERS>
struct Wrapper {
template <RETURN (OBJECT::*MEMFN)(PARAMETERS...)>
RETURN foo(PARAMETERS... parameters) {
// do whatever with MEMFN, parameters, etc. here, not part of the problem
}
};
struct Object {
template <auto MEMFN, typename RETURN, typename OBJECT, typename ...PARAMETERS>
RETURN call(OBJECT &&object, PARAMETERS &&...parameters) {
// here, MEMFN parameters and PARAMETERS must be the same
// Wrapper actually not created here, it is accessed by other means
Wrapper<RETURN, typename std::decay<OBJECT>::type, PARAMETERS...> w;
return w.template foo<MEMFN>(std::forward<PARAMETERS>(parameters)...);
}
};
struct Foo {
void fn(int);
};
int main() {
Object o;
Foo f;
o.call<&Foo::fn, void, Foo &, int>(f, 42);
// this is wanted instead:
// o.call<&Foo::fn>(f, 42);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…