Passing a std::array of unknown size to a function
Is there a simple way to make this work, as one would with plain C-style arrays?
No. You really cannot do that unless you make your function a function template (or use another sort of container, like an std::vector
, as suggested in the comments to the question):
template<std::size_t SIZE>
void mulArray(std::array<int, SIZE>& arr, const int multiplier) {
for(auto& e : arr) {
e *= multiplier;
}
}
Here is a live example.
The size of the array
is part of the type, so you can't do quite what you want. There are a couple alternatives.
Preferred would be to take a pair of iterators:
template <typename Iter>
void mulArray(Iter first, Iter last, const int multiplier) {
for(; first != last; ++first) {
*first *= multiplier;
}
}
Alternately, use vector
instead of array, which allows you to store the size at runtime rather than as part of its type:
void mulArray(std::vector<int>& arr, const int multiplier) {
for(auto& e : arr) {
e *= multiplier;
}
}
EDIT
C++20 tentatively includes std::span
https://en.cppreference.com/w/cpp/container/span
Original Answer
What you want is something like gsl::span
, which is available in the Guideline Support Library described in the C++ Core Guidelines:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#SS-views
You can find an open-source header-only implementation of the GSL here:
https://github.com/Microsoft/GSL
With gsl::span
, you can do this:
// made up example
void mulArray(gsl::span<int>& arr, const int multiplier) {
for(auto& e : arr) {
e *= multiplier;
}
}
// lets imagine these being full of numbers
std::array<int, 17> arr1;
std::array<int, 6> arr2;
std::array<int, 95> arr3;
mulArray(arr1, 3);
mulArray(arr2, 5);
mulArray(arr3, 2);
The problem with std::array
is that its size is part of its type, so you'd have to use a template in order to implement a function that takes an std::array
of arbitrary size.
gsl::span
on the other hand stores its size as run-time information. This allows you to use one non-template function to accept an array of arbitrary size. It will also accept other contiguous containers:
std::vector<int> vec = {1, 2, 3, 4};
int carr[] = {5, 6, 7, 8};
mulArray(vec, 6);
mulArray(carr, 7);
Pretty cool, huh?