If you know the array size in compile-time, as is implied by your usage of std::array
, then you have templates to the rescue. To restrictively support std::array
s of int
of different sizes you can do this:
#include <array>
template <size_t N>
void ArrayFunc(std::array<int, N> arr)
{
// Do something with `arr`
}
int main()
{
std::array<int, 10> arr10;
std::array<int, 20> arr20;
ArrayFunc(arr10);
ArrayFunc(arr20);
return 0;
}
Note the passing by value of the arr
parameter of ArrayFunc()
, a thing you should consider whether it is desired or not -- I was just following your example (in pseudo) in your question. If you don't really want to copy the entire std::array
for each function call then you can do either:
template <size_t N>
void ArrayFunc(std::array<int, N>& arr)
or
template <size_t N>
void ArrayFunc(const std::array<int, N>& arr)
Depending on whether you will be modifying arr
in the function or not.