Skip to content

std::span — getting rid of pointer and size

C++ does not support passing around variably sized arrays by value. The closest is to pass std::vector<T>. But you don't always have your data in the exact std::vector<T> chunks to make that a viable interface. So instead, we pass a pointer to the start of the array and an integer to tell the function how many elements after the pointer are valid array accesses. Example: you have a vector<int> data and want to pass the elements from index n to n+16 to a function. std::span abstracts this use case and integrates more or less into the APIs of the C++ standard library.

Edited by Matthias Kretz