Daily bit(e) of C++ | handling C arrays with std::span
Daily bit(e) of C++ ♻️53, Safely wrapping C-style arrays using C++20 std::span.
Passing data around as C-style arrays might be unavoidable when interacting with legacy code or C APIs. This can lead to errors when keeping track of the array size.
C++20 introduced std::span, a view that can wrap a contiguous memory block in a range interface. This allows us to safely use algorithms and views to process data without copying it into a C++ container.
std::span can also be converted to a std::span of std::byte, giving access to underlying object representation.