Sitesh
Sitesh

Reputation: 1878

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..

Upvotes: 10

Views: 1708

Answers (1)

Drew Dormann
Drew Dormann

Reputation: 63745

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.)

Upvotes: 14

Related Questions