dimanche 22 septembre 2019

How to bind parameters recursively to a function?

I need to take an array of input arguments of size n and bind its values to another function that takes n arguments.

I tried using bind to pass the elements of the array one by one to the function but it didn't work (kind of like using bind_front inside a loop).

What I need is something like:

#include <iostream>
#include <functional>


using namespace std;

int addThreeNumbers(int a, int b, int c)
{
    return a+b+c;
}

int main()
{
    int parameters[] = {1, 2, 3};

    auto f = addThreeNumbers;

    // Bind each element from the list one by one
    for(int i=1; i<parametersLength; i++)
    {
         f = bind(f, parameters[i]);
    }

    f(); // call addThreeNumbers

    return 0;
}

The solution would need to work with parameters being an array of any size. Is this possible to do using c++ 11?

Aucun commentaire:

Enregistrer un commentaire