Search code examples
c++arraysvariadic-functions

c++ Pass an array instead of a variable length argument list


So I have a function that takes a variable length argument list, for example:

int avg(int count,...){
    //stuff
}

I can call it with avg(4,2,3,9,4); and it works fine. It needs to maintain this functionality.

Is there a way for me to also call it with an array instead of listing the variables? For example:

avg(4,myArray[5]) such that the function avg doesn't see any difference?


Solution

  • No there is no such way. You can however make two functions, one that takes a variable number of arguments, and one that takes an array (or better yet, an std::vector). The first function simply packs the arguments into the array (or vector) and calls the second function.