New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auto const view not const #618
Comments
Views have pointer-like semantics: applying If you want a pointer that cannot be used to modify the elements it denotes, you have to apply #include <string>
#include <iostream>
#include <range/v3/all.hpp>
int main()
{
std::string foo{"abcdefgeherefwweed"};
auto bar = foo | ranges::view::drop(3) | ranges::view::take(4) | ranges::view::const_;
bar[0] = 'X'; // Error
std::cout << foo << '\n'; // "abcXefgeherefwweed"
}
This sounds like a bug.
Yes: the reference type of An |
Thanks for the long reply! I'll investigate this further tomorrow! |
Not a bug. |
It seems to me that a const view doesn't actually prevent one from modifying the underlying container. I have read through #385 but I am not sure if it's the same issue.
Take this example:
I would expect that changing
auto bar = ...
toauto const bar = ...
would prevent bar from being written to, but this is not the case. OTOH when usingranges::any_random_access_view<char &>
instead ofauto
it works, i.e.const
does prevent the element assignment.From my perspective the explicit cast gives the correct behaviour, maybe something is wrong with the automatic type deduction?
On a related note, do any of the following behave differently:
?
Thanks for your help!
The text was updated successfully, but these errors were encountered: