Developer knowledge network · moderated exchange

Супольнасць UnreliableCode

Супольнасць распрацоўшчыкаў даследаванняў, зваротнага праектавання і кадавання

Knowledge indexжыць
4Categories
919Threads
2.8KПаведамленні
Guide

How to use std::span in C++20 for safe bounds-checked buffer views without copying [StackOverflow Architecture Guide]

memory_model_mook
Low-Level C Veteran
MEMBER
прадстаўнік: 163
Дата далучэння: Jan 2019
Паведамленні: 11
Дзякуй: 33
1 месяцаў таму · Jul 4, 2026 10:05 AM
#1

std::span<T> provides a lightweight, non-owning view over contiguous memory (C arrays, std::vector, std::array, memory-mapped files):

CPP
void ProcessSamples(std::span<const float> samples) {
    for (float val : samples) {
        // Process samples safely
    }
}

Instead of taking const float* pData, size_t count, std::span encapsulates both the pointer and length in a single object with .subspan(), .data(), and .size(), preventing buffer overruns.

raii_clean_coder
Modern C++ Advocate
MEMBER
прадстаўнік: 190
Дата далучэння: Aug 2020
Паведамленні: 20
Дзякуй: 22
1 месяцаў таму · Jul 4, 2026 1:43 PM
#2

std::span with static extent (std::span<int, 4>) is only 8 bytes (pointer only), while dynamic extent (std::span<int>) is 16 bytes (pointer + size).

modern_cpp_artisan
C++ Template Wizard
MEMBER
прадстаўнік: 124
Дата далучэння: Jun 2019
Паведамленні: 29
Дзякуй: 72
1 месяцаў таму · Jul 4, 2026 9:51 PM
#3

Modern C++ API design guideline: use std::span whenever accepting contiguous data buffers in functions.