Passing Spans to Functions
Write flexible APIs.
Passing Spans to Functions is a free C++ Academy lesson on CoddyKit — lesson 3 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.
One Function, Many Containers
A function taking std::span accepts arrays, vectors, and raw buffers alike, no templates or overloads needed.
#include <iostream>
#include <span>
#include <vector>
#include <array>
int sum(std::span<const int> s) {
int t = 0; for (int x : s) t += x; return t;
}
int main() {
std::vector<int> v = {1, 2, 3};
std::array<int, 2> a = {4, 5};
std::cout << sum(v) << " " << sum(a) << "\n";
}No Size Parameter Needed
Old C-style APIs pass a pointer plus a separate size. A span bundles both, eliminating mismatched-length bugs.
#include <iostream>
#include <span>
void printAll(std::span<const int> s) {
for (int x : s) std::cout << x << " ";
std::cout << "\n";
}
int main() {
int buf[] = {7, 8, 9};
printAll(buf); // size inferred
}const span for Read-Only
Use std::span<const T> to promise the function will not modify the caller's data.
#include <iostream>
#include <span>
int maxOf(std::span<const int> s) {
int m = s[0];
for (int x : s) if (x > m) m = x;
return m;
}
int main() {
int data[] = {3, 9, 4};
std::cout << maxOf(data) << "\n"; // 9
}Mutable span to Edit In Place
A non-const span lets a function modify the caller's elements directly.
#include <iostream>
#include <span>
#include <vector>
void doubleAll(std::span<int> s) {
for (int& x : s) x *= 2;
}
int main() {
std::vector<int> v = {1, 2, 3};
doubleAll(v);
std::cout << v[2] << "\n"; // 6
}Avoids Template Bloat
Without span you might template on container type, generating many instantiations. A span gives one concrete signature that just works.
Avoids Copies
Passing a span never copies the elements, only a pointer and length travel into the function.
#include <iostream>
#include <span>
#include <vector>
size_t count(std::span<const int> s) { return s.size(); }
int main() {
std::vector<int> big(1000, 0);
std::cout << count(big) << "\n"; // no copy of 1000 ints
}Empty Spans Are Fine
A function should handle an empty span gracefully; size() is zero and the range-based loop simply runs zero times.
#include <iostream>
#include <span>
int sum(std::span<const int> s) { int t = 0; for (int x : s) t += x; return t; }
int main() {
std::span<const int> empty;
std::cout << sum(empty) << "\n"; // 0
}Pass by Value
Spans are tiny, so pass them by value, not by reference. Copying a pointer and length is essentially free.
Document Const Intent
Choosing span<const T> versus span<T> tells callers at a glance whether their data may be modified.
Caller Keeps Ownership
The caller still owns the container; the span only borrows it for the duration of the call. The function must not store the span beyond the call.
A Modern API Style
Accepting a span is the idiomatic modern way to write functions over contiguous data, flexible, safe, and copy-free.
Quick Check
Check your understanding of passing spans.
Recap
You learned to pass spans to functions:
- One signature accepts arrays, vectors, and buffers.
span<const T>for read-only;span<T>to edit in place.- Pass by value, copies nothing.
- The caller keeps ownership; do not store the span past the call.
Frequently asked questions
Is the “Passing Spans to Functions” lesson free?
Yes — the full text of “Passing Spans to Functions” 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 “Passing Spans to Functions”?
Write flexible APIs. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Passing Spans to Functions” 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