I'm parsing a binary network data and i would like to make the process as allocation-less as possible. But now i realized there are 2 very similar concepts that are probably both good enough for my case, and those are std::basic_string_view<T>
and std::span<T>
.
So I was wondering. What are the differences between these 2 and are there any advantages using one over the other? One obvious difference is the availability, std::basic_string_view
is already in C++17 while std::span
is C++20 (but you can use the one in 'Guidelines Support Library' for older standards). But is there anything else? It should, because otherwise they wouldn't both make it into the standard.
string_view
isspan<char const>
and has lots of string "convenience functions" – Artemisstring_view
's API has operations that are similar to whatstd::string
provides i.e. substr, copy, find etc. – Threadyvector
andbasic_string
exist – Cracowbasic_string
at least guarantees null-termination. That's not true forstring_view
(is there a null-terminated string_view planned?) – Artemis