Home > database >  How can I have a function pointer template as a template parameter?
How can I have a function pointer template as a template parameter?

Time:10-24

I am trying to create a class template that expects a type and a function pointer as template parameters. The function pointer is expected to be a member function of the type passed in. I want the user of the class template to be able to pass in a void member function of the type passed in. That member function will then be called on instances of the type passed in every time a certain function of the class template is called. It's a bit hard to explain but it's supposed to work sort of like this:

template<Type, Function> // For the purpose of explaining it
class Foo
{
public:
    template<Args... args>
    void callOnAll(Args... args)
    {
        for(Type* ptr : ptrs_)
        {
            ptr->Function(std::forward<Args>(args)...);
        }
    }
private:
    std::vector<Type*> ptrs_;
}

Assuming that something like this is possible (which I realize it might not be), the key would have to be getting the template parameters for the class right, and getting the update function right. This is what I've come up with but I still can't get it to work:

template<typename T, template<typename... Args> void(T::*func)(Args... args)>
class EngineSystem
{
public:
    template<typename... Args>
    void update(Args... args)
    {
        for (T* handler : handlers_)
        {
            ((*handler).*func)(std::forward<Args>(args)...);
        }
    }
private:
    std::vector<T*> handlers_;
};

The code above does not compile. It points me to the line where I declare the template parameters for the class, underlines void and says expected 'class' or 'typename'.

Is it clear what I'm trying to achieve, and is it possible?

CodePudding user response:

C doesn't allow non-type template template parameters. That means you can't have a parameter-pack for your member-function pointer parameter.

Assuming you're using C 17 or newer, you can use an auto template parameter instead:

template<typename T, auto func>
public:
    template<typename... Args>
    void update(Args... args)
    {
        for (T* handler : handlers_)
        {
            (handler->*func)(std::forward<Args>(args)...);
        }
    }
private:
    std::vector<T*> handlers_;
};

Live Demo

Technically that will accept any object for func, but assuming update is called, then (handler->*func)(std::forward<Args>(args)...) still has to be well-formed or compilation will fail.

CodePudding user response:

#include <iostream>
#include <vector>

template <typename T, typename... Args>
class EngineSystem
{
    public:
    EngineSystem(void(T::*fun)(Args... args)): fun(fun)
    {
    }
    void update(Args... args)
    {
        for (T* handler : handlers_)
        {
            (handler->*fun)(std::forward<Args>(args)...);
        }
    }
    void push(T* t){
        handlers_.push_back(t);
    }
private:
    void(T::*fun)(Args... args);
    std::vector<T*> handlers_;
};
struct A {
    int x = 3;
    void fn(int a, int b){
        std::cout << a << b << x;
    }
};

template <typename T, typename... Args>
auto makeEngine(void(T::*fun)(Args... args)){
    return EngineSystem<T, Args...>(fun);
}

int main() {
    EngineSystem<A, int, int> as(&A::fn);

    // or deduce types automatically
    auto es = makeEngine(&A::fn);
    A a;
    es.push(&a);
    es.update(1,2);
    
    return 0;
}

https://gcc.godbolt.org/z/Pcdf9K9nz

  • Related