Pass a List to a Function to act as Multiple Arguments in C++

116 Views Asked by At

In Python, we can unpack a list to pass the elements of the list as arguments to a function. I am looking for a similar feature in C++.

I have a list of unknown size and a function which has number of arguments == size of list. I want to unpack this list and pass the elements as arguments to this functions. Below is a sample code:

#include <cstdio>

void yourFunction(int a, int b, int c, int d, int e) {
    printf("%d\n", a);
    printf("%d\n", b);
    printf("%d\n", c);
    printf("%d\n", d);
    printf("%d\n", e);
}

int main() {
    int inputList[5] = {10, 20, 30, 40, 50}; // size of list can be unknown at compile time
    // Somehow pass this list to function as individual elements
}

I tried some methods in https://en.cppreference.com/w/cpp/language/parameter_pack but I can't figure the solution to the above problem.

0

There are 0 best solutions below