#include <array>
#include <cstddef>
#include <iostream>
#include <span>
#include <vector>
// *** before: ***
// a pointer and a length travelled as two separate arguments, and
// keeping them in step was left to the caller; a vector, an array and
// a plain buffer each needed their own overload
int sumOld(const int* data, std::size_t size) {
int total = 0;
for (std::size_t i = 0; i < size; ++i) total += data[i];
return total;
}
// *** in version C++20: ***
int sum(std::span<const int> data) {
int total = 0;
for (int n : data) total += n; // the length travels with the pointer
return total;
}
int main() {
int raw[3] = {1, 2, 3};
std::vector<int> v{1, 2, 3, 4};
std::array<int, 2> a{5, 6};
std::cout << sumOld(raw, 3) << "\n";
std::cout << sum(raw) << " " << sum(v) << " " << sum(a) << "\n";
std::cout << sum(std::span(v).subspan(1, 2)) << "\n"; // a window, no copy
std::cout << std::span(v).size() << "\n";
}