Bounds and Subspans
Slice spans safely.
Bounds and Subspans is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Slicing with subspan
s.subspan(offset, count) returns a span over part of the original, no copy, just a new pointer and length.
#include <iostream>
#include <span>
int main() {
int buf[] = {0, 1, 2, 3, 4};
std::span<int> s(buf, 5);
auto mid = s.subspan(1, 3);
for (int x : mid) std::cout << x << " ";
std::cout << "\n"; // 1 2 3
}first and last
first(n) and last(n) give spans over the leading or trailing n elements.
#include <iostream>
#include <span>
int main() {
int buf[] = {10, 20, 30, 40};
std::span<int> s(buf, 4);
auto f = s.first(2);
auto l = s.last(2);
std::cout << f[0] << " " << l[1] << "\n"; // 10 40
}Subspans Share Memory
Modifying a subspan changes the original data, because they point at the same buffer.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 1, 1, 1};
std::span<int> s(buf, 4);
auto tail = s.last(2);
tail[0] = 99;
std::cout << buf[2] << "\n"; // 99
}size_bytes
size_bytes() returns the total byte size of the span's elements, useful for low-level I/O.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 2, 3};
std::span<int> s(buf, 3);
std::cout << s.size_bytes() << "\n"; // 3 * sizeof(int)
}Bounds Are Your Responsibility
operator[] on a span is not bounds-checked. Reading s[s.size()] is undefined behavior.
Check Before Slicing
Calling subspan with an offset or count larger than the span is undefined behavior, validate against size() first.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 2, 3};
std::span<int> s(buf, 3);
size_t off = 1, cnt = 2;
if (off + cnt <= s.size()) {
auto sub = s.subspan(off, cnt);
std::cout << sub.size() << "\n";
}
}Empty After Slicing
Slicing can yield an empty span (for example first(0)); handle it like any empty range.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 2};
std::span<int> s(buf, 2);
auto e = s.first(0);
std::cout << e.empty() << "\n"; // 1
}Chaining Slices
Subspans can be sliced further, since each is just another view into the original buffer.
#include <iostream>
#include <span>
int main() {
int buf[] = {0, 1, 2, 3, 4, 5};
std::span<int> s(buf, 6);
auto inner = s.subspan(1, 4).first(2);
std::cout << inner[0] << " " << inner[1] << "\n"; // 1 2
}Processing in Chunks
A common use of subspan is iterating over a large buffer in fixed-size windows for streaming or batching.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 2, 3, 4, 5, 6};
std::span<int> s(buf, 6);
for (size_t i = 0; i < s.size(); i += 2) {
auto chunk = s.subspan(i, 2);
std::cout << "[" << chunk[0] << "," << chunk[1] << "] ";
}
std::cout << "\n";
}Static Extent Checks
With a static-extent span the compiler can verify some slice sizes at compile time, catching mistakes earlier.
Lifetime Still Applies
Every subspan shares the original buffer's lifetime. If the source is destroyed or reallocated, all derived spans become invalid.
Quick Check
Check your understanding of subspans and bounds.
Recap
You learned bounds and subspans:
subspan,first,lastcreate allocation-free slices.- Subspans share memory, edits affect the original.
operator[]and slicing are not bounds-checked, validate first.- All subspans share the source's lifetime.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Bounds and Subspans” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Bounds and Subspans”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Bounds and Subspans”?
Slice spans safely. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Bounds and Subspans” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Fixed-Size std::array
- std::span Basics
- Passing Spans to Functions
- Bounds and Subspans