jeudi 4 juin 2015

Any number of pointer to method using variadic template

I'm trying to have a variadic template class that should have any number of method pointers. 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;
};

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? Or at least get rid of the decltype()? Do I need to use macro or something? If I can solve this without macro, I would be glad.

Thanks.

Aucun commentaire:

Enregistrer un commentaire