std::span Basics
Non-owning views over contiguous data.
std::span Basics is a free C++ Academy lesson on CoddyKit — lesson 2 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.
A Non-Owning View
std::span<T> (C++20) is a lightweight view over a contiguous sequence, like string_view but for any element type. Include <span>.
#include <iostream>
#include <span>
#include <array>
int main() {
std::array<int, 3> a = {1, 2, 3};
std::span<int> s = a;
std::cout << s.size() << "\n";
}Spans Do Not Own
A span stores a pointer and a length; it observes data owned elsewhere and never copies the elements.
#include <iostream>
#include <vector>
#include <span>
int main() {
std::vector<int> v = {5, 6, 7};
std::span<int> s = v;
s[0] = 50; // modifies v
std::cout << v[0] << "\n"; // 50
}From a std::array
An array converts to a span automatically, the span sees all its elements.
#include <iostream>
#include <span>
#include <array>
int main() {
std::array<int, 4> a = {1, 2, 3, 4};
std::span<int> s{a};
for (int x : s) std::cout << x << " ";
std::cout << "\n";
}From a vector
A std::vector also converts to a span, giving a uniform view regardless of the underlying container.
#include <iostream>
#include <vector>
#include <span>
int main() {
std::vector<int> v = {9, 8, 7};
std::span<int> s = v;
std::cout << s.front() << " " << s.back() << "\n";
}From Pointer and Size
You can build a span from a raw pointer and a count, useful for C-style buffers.
#include <iostream>
#include <span>
int main() {
int buf[] = {10, 20, 30};
std::span<int> s(buf, 3);
std::cout << s[1] << "\n"; // 20
}Iterating a Span
Spans support range-based for and the usual container interface (size, front, back, data).
#include <iostream>
#include <span>
int main() {
int buf[] = {2, 4, 6, 8};
std::span<int> s(buf, 4);
int sum = 0;
for (int x : s) sum += x;
std::cout << sum << "\n"; // 20
}Read-Only Spans
A std::span<const int> grants read-only access; use it when a function should not modify the data.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 2, 3};
std::span<const int> s(buf, 3);
std::cout << s[2] << "\n"; // 3
}Mutable Through a Span
A non-const span can change the underlying elements, since it points at the real data.
#include <iostream>
#include <span>
int main() {
int buf[] = {1, 1, 1};
std::span<int> s(buf, 3);
for (int& x : s) x *= 10;
std::cout << buf[2] << "\n"; // 10
}Dynamic vs Static Extent
By default a span has a dynamic size, but you can fix it at compile time with std::span<int, 3> for extra checking.
No Allocation, Cheap to Pass
Like all views, a span is cheap to copy and pass by value, it carries only a pointer and a length.
Mind the Lifetime
A span is valid only while its source lives and is not reallocated. Do not keep a span to a vector that later grows.
Quick Check
Check your understanding of std::span.
Recap
You learned std::span basics:
- Non-owning view over contiguous data (array, vector, raw buffer).
- Holds a pointer + length; no copy or allocation.
constspans are read-only; mutable spans edit the source.- Valid only while the source lives.
Frequently asked questions
Is the “std::span Basics” lesson free?
Yes — the full text of “std::span Basics” 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 “std::span Basics”?
Non-owning views over contiguous data. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “std::span Basics” 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.