Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no view<T> similar to std::string_view

I am aware that std::string_view is a non-owning reference to a string and the major differences between std::string_view and std::string are

enter image description here

Now, Why std::string_view is not applicable to other types ? or why this implementation is specific ONLY to std::string ?

For Example : if we have similar <T>generic_view where T can be of any type including custom types.

With this, instead of using const T& as function argument, <T>generic_view can be used. And also other advantages of std::string_view will be useful like Allocation, Copying etc..

like image 984
Sitesh Avatar asked Apr 12 '18 14:04

Sitesh


1 Answers

There is a non-owning type for contiguous collections of arbitrary objects in C++20 called std::span.

Versions of C++ prior to C++20 can use the standalone implementation gsl::span.

std::span behaves similarly to C++17's std::string_view, but the interface provides general container-like access instead of string-like access, and the underlying data can be non-const. (From the table in the question, Element Mutability is Allowed.)

like image 132
Drew Dormann Avatar answered Oct 23 '22 06:10

Drew Dormann