vendredi 29 septembre 2017

Variadic template as parameters to std::function

I want to build a structure that allow me to call member functions with an undefined number of parameters. For now I wrote something like this

template<typename Class, typename Return, typename ... Args>
struct Caller
{
private:
    std::function<Return(Args ...)> callerFunction;

    Caller() = delete;
    Caller(const Caller&) = delete;
    Caller(Caller&&) = delete;
    Caller& operator=(const Caller&) = delete;

public:
    ~Caller() = default;

    Caller(Class& instance, Return(Class::*function)(Args ...))
    {
        callerFunction = [&instance, function](Args... args)
        {
            return (instance.*function)(args ...);
        };
    }

    Return operator() (Args ... args)
    {
        return callerFunction(args ...);
    }
};

I am afraid that I cannot work around the fact that I cannot declare a std::function variable as std::function<Return<Args&& ...)> callerFunction

When I try to do this the compiler says that it cannot convert from int to int&& (if for example the parameters are ints), so I'm guessing that the function sees the Args&& ... as a parameter pack of rvalue references. Am I correct?

Is there a workaround?

Aucun commentaire:

Enregistrer un commentaire