10

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

Sitesh
  • 1,816
  • 1
  • 18
  • 25
  • 1
    I forgot why it was not included but GSL to the rescue: https://stackoverflow.com/questions/45723819/what-is-a-span-and-when-should-i-use-one – NathanOliver Apr 12 '18 at 14:23
  • 1
    Possible duplicate of [Why string\_view instead of generalized container\_view?](https://stackoverflow.com/questions/39034413/why-string-view-instead-of-generalized-container-viewt) – underscore_d Apr 12 '18 at 14:35
  • How would `string_view` work or `string_view>`? – Thomas Matthews Apr 12 '18 at 16:35

1 Answers1

14

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

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • 3
    Sweet, it made it in. – NathanOliver Apr 12 '18 at 14:25
  • Just wondering what will be the future of `std::string_view` ? Because one can easily use std::span for std::string. – Sitesh Apr 12 '18 at 14:28
  • 2
    `span` is not a "view"; views are non-modifiable; `span` is modifiable. – Nicol Bolas Apr 12 '18 at 14:28
  • 3
    @Sitesh: "*Because one can easily use std::span for std::string.*" Unless you want to use the string-specific member functions of `string_view`. – Nicol Bolas Apr 12 '18 at 14:28
  • 3
    @NicolBolas No, `span` is definitely a View. View does not imply non-modifiable. – Barry Apr 12 '18 at 14:31
  • Also, potentially mutable... you can have `span`, for instance... – Barry Apr 12 '18 at 14:32
  • 3
    @Sitesh We'll have `span` and `string_view` in the same way that today we have `vector` and `string`. – Barry Apr 12 '18 at 14:32
  • Is it not good idea to create a view of any type T generically with immutability (just as string_view) ? – Sitesh Apr 12 '18 at 14:33
  • What I think we are saying is that std::span *is* a generic view for a linear container (c array std::array, std::vector, std::string). The "main/only" benefit that string_view gives you is the additional text-specific member functions such as find/rfind/substr, which make no sense on other containers. – Gem Taylor Apr 12 '18 at 14:42
  • 1
    @Sitesh That type is spelled `span`. – Barry Apr 12 '18 at 14:56