Difference between std::basic_string_view<T> and std::span<T>

1.6k Views Asked by At

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.

1

There are 1 best solutions below

6
On

string_view is intended for use with textual data. span is intended for use with arbitrary arrays of objects. While neither one is an exact fit for binary data, string_view is clearly inapplicable.