Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C++ views can influence the original sequence

Tags:

c++

Why is there no unified approach in C++ to views in terms of the ability to change elements of the original sequence by changing the corresponding elements of its view?

string_view does not allow changing the elements of the string it represents, but, for example, span, views::drop, views::take do.

And in general, why some types of views have an ability to change the elements of the original sequence? It would seem more logical if the view could not influence the original sequence.

like image 658
Pavel Avatar asked Sep 01 '25 20:09

Pavel


1 Answers

std::string_view is somewhat of a special case, because it really wants to interoperate with string literals, which are const char[]s. Because of this, it only provides const access to the underlying chars. In addition to that, mutations of std::string will often result in a different number of elements, which isn't possible for a view to do.

Having said that, it's very similar with other views. You can't point a std::span<int> to the contents of a const std::vector<int> etc, but you can point a std::span<const int> to them.

like image 128
Caleth Avatar answered Sep 08 '25 02:09

Caleth