Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Edit: Ok, I was declaring the function inside the Caller constructor in the wrong way.

Wrong way --> Caller(Class& instance, Return(Class::*function)(Args&& ...))

Right way --> Caller(Class& instance, Return(Class::*function)(Args ...))

The (I guess) right implementation is

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)(std::forward<Args>(args) ...);
        };
    }

    Return operator() (Args&& ... args)
    {
        return callerFunction(std::forward<Args>(args) ...);
    }
};

Now the question is: Why do I need to declare the function anyway without the double &?

like image 538
Astinog Avatar asked May 31 '26 01:05

Astinog


1 Answers

When you define your class template like this:

template <typename T>
struct A {
 A(T&& param)
}

And then create an instace:

A<int> myInstance(someIntVariable);

It won't compile. The reason is, the type of T is explicitly specified by you (as an int, in A<int>), and your class constructor parameter is no longer T&&, but int&&, so it's no longer universal reference (which accepts both lvalue and rvalue references), but regular rvalue reference.

Next, if you pass it some integer, there is a type missmatch error because you pass a regular variable when rvalue reference is expected.

In your example you explicitly defined function signatures, so the same applies - constructor expects a function taking rvalue references to Args..., but that's not true.

I think it's better explained in this question

like image 90
Outshined Avatar answered Jun 02 '26 15:06

Outshined