Method function pointer template without class type name

Consider this function templateby calling an object method class T.

template<class T, void (T::*Method)()>
void circuitousInvoke(T* callee) {
    (callee->*Method)();
}

Example:

struct A {
    void test() {};
}

circuitousInvoke<A, &A::test>(new A);

As you know, the type T is already known as circousInvoke from the parameter callee, is there a way to avoid entering this type?

circuitousInvoke<&A::test>(new A);

EDIT

This question applies only to template functions. Inheritance and other class-based solutions are not suitable in this case. (In my project, using a wrapper object will be worse than spelling an extra name.)

+6
source share
1 answer

In C ++ 17, this would be possible with auto

template<auto Method, typename T>
void circuitousInvoke(T* callee) {
    (callee->*Method)();
}

and then

A a;
circuitousInvoke<&A::test>(&a);
+8
source

All Articles