Search code examples
c++c++14c++17variadic-templatesvariadic-functions

How to pass method and call it with variable number of arguments


I have class CallProtector that is supposed to call method with variable number of arguments which suppose to protect call via mutex, but I can't figure out how could I pass object's method with their arguments. Here what I have so far:

class CallProtector
{
public:

    template<typename F, typename ...Args>
    void callProtectedMethod(F& lambda, Args... args)
    {
        std::lock_guard<std::mutex> guard(m_guard);
        lambda(args);
    }

private:
    std::mutex m_guard;
};

I am trying to use it this way:

class Car
{
public:
    void updateEngine(int engineModelType) {}
};

int main()
{
    Car alfaRomeo;
    CallProtector callProtector;
    callProtector.callProtectedMethod(&Car::updateEngine, 10);

    return 0;
}

But I have compilation error saying

no instance of function template "CallProtector::callProtectedMethod" matches the argument list

Appreciate any help, thanks in advance.


Solution

  • Following might work for you:

    class CallProtector
    {
    public:
    
        template<typename F, typename ...Args>
        void callProtectedMethod(F&& func, Args&&... args)
        {
            std::lock_guard<std::mutex> guard(m_guard);
            func(std::forward<Args>(args)...);
        }
    
    private:
        std::mutex m_guard;
    };
    

    and then use it like:

    Car alfaRomeo;
    CallProtector callProtector;
    
    auto updateEngine = std::bind(&Car::updateEngine, &alfaRomeo, std::placeholders::_1); 
    callProtector.callProtectedMethod(updateEngine, 10);
    

    EDIT

    Or this would work too:

    template<typename F, typename ...Args>
    void callProtectedMethod(F&& func, Args&&... args)
    {
        std::lock_guard<std::mutex> guard(m_guard);
        std::invoke(std::forward<F>(func), std::forward<Args>(args)...);
    }
    

    and then

    callProtector.callProtectedMethod(&Car::updateEngine, alfaRomeo, 10);