6

Here is a great explanation of what a string_view object is.

Are there '_view' objects for any of the STL containers besides std::string?

Seems to me that it is an extremely useful thing to have. Imagine a std::vector_view class that simply stores a start iterator and a length field. Not actually owning the underlying data allows for awesome efficiency improvements.

Adam
  • 605
  • 4
  • 11
  • I think it's useless. Idea is to implement some memory management behind the scene, but usually if you make a copy of vector, you will change it. And thus anyway you need to copy it. And if you don't plan to change it, then you use const pointers or "&" without risk. Of you plan to do it from different threads, then copy usually is much better to avoid any race. So in the end the use case for such feature is quite rare, but it will add complexity and additional layer in the code. – Arkady Aug 08 '18 at 13:48
  • 1
    @Arkady I respectfully disagree. There is a well defined use case for _view objects. They don't work in every scenario, as you pointed out, but that does not change the fact that in certain situations they can provide great improvements. – Adam Aug 08 '18 at 13:59

1 Answers1

11

There is a proposal for span, which is a view over a contiguous range of objects: http://wg21.link/p0122. See also: What is a “span” and when should I use one?.

The GSL library also provides gsl::span.


This might be stretching it, but I also proposed function_ref, which is basically a view over a Callable: http://wg21.link/p0792

Vittorio Romeo
  • 82,972
  • 25
  • 221
  • 369