0Pricing
C++ Academy · Lesson

Bounds and Subspans

Slice spans safely.

Bounds and Subspans is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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, last create 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.

Frequently asked questions

Is the “Bounds and Subspans” lesson free?

Yes — the full text of “Bounds and Subspans” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Fixed-Size std::array
  2. std::span Basics
  3. Passing Spans to Functions
  4. Bounds and Subspans
← Back to C++ Academy