Engine::apply needs to read a sequence of events. If its parameter were const std::vector<Event>&, every caller would need a vector: the input adapter has one, but a test wants to write a braced list, and a replay tool might want to apply only the first ten events of a larger log. Each of those would have to copy events into a new vector just to satisfy the signature. The C answer, a pointer plus a length passed separately, avoids the copy but lets the two halves drift apart.
std::span<const Event>, from <span>, is the C++20 answer. You have already used it since Day 3's concepts lesson as "a read-only view"; here is what it really is. It is a small value holding a pointer and a length together, a view of a contiguous sequence it does not own. Any contiguous storage converts to it: a vector, a C array, a std::array, or a part of one. That makes it the natural parameter type for "read these elements during this call".
Predict and run
#include <iostream>
#include <span>
int main() {
int values[3] = {10,20,30};
std::span<const int> view(values);
std::cout << view.size() << " " << view.subspan(1)[0] << "\n";
return 0;
}Check your prediction
3 20view knows the array's length, 3, because it was built from the array itself, not from a decayed pointer. view.subspan(1) is a new span starting one element in, so its element 0 is 20. No element was copied. Both spans point into values, so both are valid only while values lives.
One parameter, many sources
#include <array>
#include <iostream>
#include <span>
#include <vector>
long sum(std::span<const int> values) {
long total = 0;
for (int value : values) { total += value; }
return total;
}
int main() {
std::vector<int> deltas{-3, 5, 2, -1};
std::array<int, 3> fixed{1, 2, 3};
int raw[] = {100, 200};
std::span<const int> all(deltas);
std::cout << sum(deltas) << " " << sum(fixed) << " " << sum(raw) << "\n";
std::cout << sum(all.first(2)) << " " << sum(all.last(2)) << " " << sum(all.subspan(1, 2)) << "\n";
return 0;
}3 6 300
2 1 7first(n), last(n) and subspan(offset, count) all return smaller views of the same storage. A span is cheap to copy, two words, so pass it by value like an int. The project's tests have one more source: an std::initializer_list<Event>, turned into a span with std::span<const Event>(events.begin(), events.size()). That is safe because the list's backing array lives until the end of the full expression that contains the call.
Which const?
std::span<const int> and const std::span<int> mean different things. In std::span<const int>, the elements are read-only through this view; that is the "borrow for reading" type, and the one apply uses. In const std::span<int>, the span object itself cannot be reseated, but the elements can still be modified through it, because a span's constness, like a visitor's reference member, is shallow. A function that only reads should take std::span<const T>; a std::span<T> parameter announces that the function may write into the caller's storage.
A span is a borrow
A span does not keep its storage alive. Two mistakes follow from that, and the compiler catches neither:
std::span<const int> recent() {
std::vector<int> values{1, 2, 3};
return values; // The vector dies here; the span dangles.
}
std::vector<int> log{1, 2, 3};
std::span<const int> view(log);
log.push_back(4); // May reallocate: view now points at freed memory.
std::cout << view[0]; // Undefined behavior if it did.The second is the subtle one. push_back on a full vector allocates a new buffer, moves the elements and frees the old one, and the span still points at the old one. Whether it happens depends on the vector's capacity, so the bug can hide in tests and appear in production. Clang can flag some returns of views to locals, but not all, and never the growth case. The safe rule is the one the engine follows: a span is a parameter, used synchronously during one call, and never stored in an object that outlives the call. Engine::apply reads each event, copies what it needs into its owned inventory, and keeps no pointer into the batch.
Bounds are your job
view[i] does no bounds check, and neither do first, last or subspan: asking for subspan(5) of a 3-element span is undefined behavior, not an empty span. When an offset comes from input, check it first:
#include <iostream>
#include <span>
std::span<const int> from(std::span<const int> values, std::size_t offset) {
if (offset > values.size()) { return {}; }
return values.subspan(offset);
}
int main() {
int values[] = {10, 20, 30};
std::cout << from(values, 1).size() << " " << from(values, 3).size() << " " << from(values, 7).size() << "\n";
return 0;
}2 0 0Offset 3 is allowed and gives an empty span, the "one past the end" position. Offset 7 is outside the sequence, and the helper returns an empty span instead of computing a pointer beyond the array.
Practice
Your turn
Write void window_sums(std::span<const int> values, std::size_t width) that prints the sum of each run of width consecutive values on one line, separated by spaces, using subspan and no index arithmetic on raw pointers. If width is 0 or larger than the input, print no windows. Run it on {4, -1, 3, 0, 2} with widths 2, 5, 6 and 0:
3 2 3 2
8
no windows
no windows#include <iostream>
#include <span>
int main() {
int values[] = {4, -1, 3, 0, 2};
// Write window_sums and call it with widths 2, 5, 6 and 0.
return 0;
}Hint 1: the idea
There are size - width + 1 windows, and window i is values.subspan(i, width). Check the width before computing that count, because it underflows when width > size.
Hint 2: a first step
Reuse a sum(std::span<const int>) helper, and loop for (std::size_t start = 0; start + width <= values.size(); ++start).
Show one solution
#include <iostream>
#include <span>
long sum(std::span<const int> values) {
long total = 0;
for (int value : values) { total += value; }
return total;
}
void window_sums(std::span<const int> values, std::size_t width) {
if (width == 0 || width > values.size()) {
std::cout << "no windows\n";
return;
}
for (std::size_t start = 0; start + width <= values.size(); ++start) {
std::cout << (start == 0 ? "" : " ") << sum(values.subspan(start, width));
}
std::cout << "\n";
}
int main() {
int values[] = {4, -1, 3, 0, 2};
window_sums(values, 2);
window_sums(values, 5);
window_sums(values, 6);
window_sums(values, 0);
return 0;
}3 2 3 2
8
no windows
no windowsThe loop condition start + width <= values.size() never subtracts, so it cannot wrap around like values.size() - width would for unsigned sizes; the early check makes the intent explicit anyway. Every subspan call stays within the original array, so there is no undefined behavior for any width. Next you apply the same borrowing discipline to text, with std::string_view, and see where a parser must switch from borrowing to owning.
Completion is saved in this browser only. It does not measure mastery.