I'm trying to have a variadic template class that should have any number of method pointers aka a compile-time list of method pointer. My goal was to achieve something like this:
struct SomeType : Invoke<&SomeType::foo, &SomeType::bar> {
int foo(int i);
double bar(double a, double b);
};
However, to accept any number of method with different parameter and return type, I came up with this:
template<typename T, T m>
struct Method;
template<typename R, typename T, typename... Args, R(T::f*)(Args...)>
struct Method<R(T::*)(Args...), f> {
constexpr static auto method = f;
};
It work, but it forces me to use this syntax:
struct SomeType : Invoke<Method<decltype(&SomeType::foo), &SomeType::foo>, Method<decltype(&SomeType::bar), &SomeType::bar>> {
int foo(int i);
double bar(double a, double b);
};
With Invoke as this class:
template<typename... T>
struct Invoke;
template<typename First, typename... Others>
struct Invoke<First, Others...> : Invoke<Others...> {
constexpr static auto method = First = First::method;
using Next = Invoke<Others...>
};
template<>
struct Invoke<> {};
To get rid of the decltype()
, the only way I found is not valid C++, but it would be:
template<T f, typename T>
struct Method;
So the compiler would guess the type of f
.
How can I get closer to the syntax in the first code snippet? What would be an example of macro that is used like INVOKE(&SomeType::foo)
and would expand to the code Invoke<Method<decltype(&SomeType::foo), &SomeType::foo>>
?