range-v3 icon indicating copy to clipboard operation
range-v3 copied to clipboard

any_view & constant

Open sergegers opened this issue 5 years ago • 5 comments

Why the following code is failed:

#include #include <range/v3/all.hpp>

int main() { std::vector const v{ 1, 2, 3, 4 }; ranges::any_view<int, ranges::category::bidirectional | ranges::category::sized> const a{v}; ranges::begin(a); }

but if we remove the constant it's compiled

#include #include <range/v3/all.hpp>

int main() { std::vector const v{ 1, 2, 3, 4 }; ranges::any_view<int, ranges::category::bidirectional | ranges::category::sized> a{v}; ranges::begin(a); }

https://godbolt.org/z/sTKGev

It looks inconsistent with other ranges i.e.

#include #include <range/v3/all.hpp>

int main() { std::vector const v{ 1, 2, 3, 4 }; auto const b = v | ranges::views::transform([](auto i){ return i + 1; }); ranges::begin(b); }

https://godbolt.org/z/EnKafK

sergegers avatar Nov 26 '20 21:11 sergegers

Hi @sergegers,

there is no requirement that all ranges are "const-iterable", i.e. that that range offers a const version of the begin/end functions. For example the filters view, see https://eel.is/c++draft/range.filter#view. It is perfectly legal to write a view that returns an (iterator, sentinel) pair that changes the state of the view itself.

Any-view needs to handle the weakest guarantee any given range has. One could argue whether to add the property "const-iterable" to ranges::category (like ranges::category::sized does enable the sized-ness property) and support const views if the underlying view is "const-iterable", but I'm not too sure if that would be useful.

marehr avatar Dec 08 '20 20:12 marehr

@marehr So, the popular idioms - view like a couple of iterators or view introduces a new indirection level, it looks like a pointer - are wrong.

sergegers avatar Dec 08 '20 22:12 sergegers

I'm sorry, I don't know which popular idioms you are referring to, do you have any links to them? wikipedia? Maybe that helps to understand what you have in mind. The only thing that I wanted to point out is, that cbegin/cend is not a requirement for every written view.

marehr avatar Dec 08 '20 22:12 marehr

I'm not talking you are wrong and thanks for clarifying. I'm simply want to get a confirmation that is not a library design or implementation bug before to start adapting my code.

sergegers avatar Dec 08 '20 22:12 sergegers

Ah okay, it is expected that some ranges/views can't be const.

marehr avatar Dec 08 '20 22:12 marehr