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.
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 char
s. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With