In C++20, how does std::span<float, 4> differ from std::span<float>, and when does converting between them compile?
answer
- the second template argument
- size in the type or in the object
- dropping the size is free
- first<N>() as the bridge
basics
~20 sstd::span<float, 4> has a static extent: the size 4 is part of the type. std::span<float> has dynamic extent, storing its size at run time. Static converts implicitly to dynamic; dynamic to static needs an explicit conversion whose size must match.
solid answer
~40 s`std::span` has a second template parameter, the **extent**, defaulting to `std::dynamic_extent`. `std::span<float>` stores its size and learns it at run time; `std::span<float, 4>` carries the size in its type, so a parameter of that type accepts a `float[4]` or `std::array<float, 4>` and the compiler rejects a `float[3]`. Converting static to dynamic is implicit because nothing can go wrong. Dynamic to static is `explicit`, with the precondition `size() == 4`, because the compiler cannot check it: write `std::span<float, 4>(p, 4)`, or better, take `all.first<4>()`, which returns a static-extent span. With libc++, a static span is just a pointer, 8 bytes on a 64-bit target against 16 for the dynamic one, but that layout is an implementation choice.
code
cpp · 21 lines#include <cstdio>
#include <span>
#include <vector>
void mix_block(std::span<float, 4> out) { // exactly 4 samples, checked at compile time
for (float& s : out) s *= 0.5f;
}
int main() {
float frame[4] = {1, 2, 3, 4};
mix_block(frame); // OK: float[4] matches the static extent
// float small[3] = {}; mix_block(small); // error: no float[3] -> span<float, 4>
std::vector<float> buffer(8, 1.0f);
std::span<float> all(buffer); // dynamic extent: size known at run time
mix_block(all.first<4>()); // first<4>() returns span<float, 4>
mix_block(std::span<float, 4>(all.data() + 4, 4)); // explicit: caller vouches for size
std::printf("%zu %zu\n", sizeof(std::span<float, 4>), sizeof(std::span<float>));
std::printf("%g %g\n", frame[3], buffer[7]);
}go deeper
Recall that span's second template argument is the extent and that std::span<float> means dynamic extent, with the size known only at run time.
Explain implicit static-to-dynamic and explicit dynamic-to-static conversion, why the asymmetry exists, and how first<N>(), last<N>() and subspan<Offset, Count>() produce static spans.
Use static extents where a length is a true invariant, such as fixed DSP blocks or SIMD lanes, and avoid them where callers would scatter unchecked explicit conversions.
Decide whether fixed-size kernels in a library expose static-extent spans in their public API, trading compile-time size checking against friction for every caller holding dynamic data.
## Two kinds of span in one template `std::span` is declared as `template<class ElementType, size_t Extent = dynamic_extent> class span;`. The second parameter, the **extent**, is the number of elements: - **Dynamic extent**, `std::span<float>`, which is `std::span<float, std::dynamic_extent>`: the count is a run-time value stored in the object and returned by `size()`. - **Static extent**, `std::span<float, 4>`: the count is part of the type. `size()` always returns 4, and `extent` is a compile-time constant usable in `static_assert` or as an array bound. `std::dynamic_extent` is a constant of type `size_t` equal to `std::numeric_limits<size_t>::max()`, used as the marker value for "not known at compile time". ## What the static extent buys 1. **Compile-time size checks.** A function `void mix_block(std::span<float, 4> out)` accepts a `float[4]` or a `std::array<float, 4>` directly, and the compiler rejects a `float[3]`: Apple clang reports `no matching function for call to 'mix_block'`. 2. **A smaller object, in practice.** Since the size is in the type, an implementation can store only the pointer. With libc++ on a 64-bit target, `sizeof(std::span<float, 4>)` is 8 and `sizeof(std::span<float>)` is 16. The standard does not mandate a layout; this is libc++'s choice. 3. **Fixed-size kernels.** DSP blocks, SIMD lanes, matrix rows of known width and cryptographic blocks of fixed size all document their contract in the type. ## Conversions between the two | from | to | rule | |---|---|---| | `float[4]`, `std::array<float, 4>` | `std::span<float, 4>` | implicit | | `std::span<float, 4>` | `std::span<float>` | implicit; nothing can go wrong | | `std::span<float>` | `std::span<float, 4>` | `explicit`; precondition `size() == 4` | | `std::vector<float>` | `std::span<float, 4>` | `explicit`; the vector's size is not known at compile time | | pointer and count | either | `explicit` for a static extent | The asymmetry is deliberate. Losing the static size is always safe, so it is implicit. Gaining a static size from a run-time value is a claim the compiler cannot check, so you must write it out: `std::span<float, 4>(all.data() + 4, 4)`. Get it wrong and the precondition is violated, which is undefined behaviour in C++23; the C++26 draft makes it a **hardened precondition**, which a hardened implementation checks. ## Getting a static span from a dynamic one The subview functions come in two forms: - `first(n)`, `last(n)` and `subspan(offset, count)` take run-time arguments and return a **dynamic** span. - `first<N>()`, `last<N>()` and `subspan<Offset, Count>()` take template arguments and return a span with a **static** extent where it can be computed. So `all.first<4>()` turns a `std::span<float>` of eight samples into a `std::span<float, 4>`, ready for `mix_block`. Its precondition is `4 <= size()`. On a span that already has a static extent, the template forms check the counts at compile time; the draft spells this as a mandate that `Count <= Extent`. ## Deduction guides Class template argument deduction picks the extent from the argument: - `std::span(raw)` with `float raw[4]` deduces `std::span<float, 4>`. - `std::span(arr)` with `std::array<float, 4>` deduces `std::span<float, 4>`. - `std::span(vec)` with a `std::vector<float>` deduces `std::span<float>`, dynamic. ## When to choose which - **Default to dynamic extent** for general-purpose APIs. It accepts every contiguous source and every length. - **Use a static extent** when the length is a true invariant of the algorithm, such as a 4-sample SIMD lane, a 64-byte block or a 3-component vector, so a wrong-sized buffer is a compile error instead of a run-time bug. - Avoid static extents where callers would have to write explicit conversions everywhere; that moves the size check into casts nobody reviews. ## Common mistakes - Expecting `subspan(0, 4)` or `first(4)` to produce `std::span<float, 4>`; only the template forms carry a static size. - Writing `std::span<float, 4>(v.data(), v.size())` to silence a compile error without checking that `v.size()` is 4. That turns a compile-time check into undefined behaviour. - Assuming a static span is always pointer-sized because one library made it so. - Using `std::span<float, 0>` as if it meant "unknown size"; zero is a real static extent, and `std::dynamic_extent` is the marker for unknown. ## What interviewers listen for Name `std::dynamic_extent` as the default, explain that a static extent puts the size in the type, give the implicit-one-way, explicit-the-other conversion rule and why, and mention `first<N>()` as the idiomatic bridge. Noting that the pointer-only layout is libc++'s choice rather than a guarantee shows care about what the standard actually promises.
- What does std::span(arr) deduce for float arr[4], and for a std::vector<float>?Class template argument deduction uses the source's compile-time size when there is one. A `float[4]` gives `std::span<float, 4>`, and so does a `std::array<float, 4>`, while a `const std::array<float, 4>` gives `std::span<const float, 4>`. A `std::vector<float>` has only a run-time size, so it deduces `std::span<float>` with dynamic extent.
- What happens if you write std::span<float, 4>(v.data(), 3)?It compiles, because the pointer-and-count constructor is merely `explicit` for a static extent, but the precondition that the count equals the extent is violated. In C++20 and C++23 that is undefined behaviour; in the C++26 draft it is a hardened precondition, which a hardened implementation checks and terminates on.
- Says std::span<float, 4> holds a copy of four floats
- Thinks std::span<float> converts implicitly to std::span<float, 4>
- Claims the standard guarantees a static span is pointer-sized
- Believes subspan(0, 4) returns a static-extent span
- Says std::dynamic_extent is zero
Assumes C++20 std::span; the C++26 draft turns the size-match precondition of the dynamic-to-static conversion into a hardened precondition.