Considering
auto empty_line = [](auto& str){ return str.size() == 0; };
For ranges::getlines returns the ranges::getlines property, which owns the buffer for its frontal iterator.
So, we are supposed to make this range of values ββbefore moving on to algorithms.
auto line_range1 = ranges::getlines(std::cin); auto iter1 = ranges::find_if_not(line_range1,empty_line); auto input1 = std::stoi(*iter1);
And also there is a cool defensive machinism that prevents all dereferencing of iterators in time of already destroyed data and made these attempts by compiling time errors.
Therefore, when the owner of view_facade is passed to the algorithm as an rvalue, protection is captured when dereferencing.
This will not compile.
auto iter2 = ranges::find_if_not(ranges::getlines(std::cin),empty_line);
Mistake:
<source>:19:29: error: no match for 'operator*' (operand type is 'ranges::v3::dangling<ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor> >') auto input2 = std::stoi(*iter2); ^~~~~~
It will also not compile.
// Won't compile // auto input3 = std::stoi(*ranges::find_if_not(ranges::getlines(std::cin), // empty_line) // );
Mistake:
<source>:22:29: error: no match for 'operator*' (operand type is 'ranges::v3::safe_iterator_t<ranges::v3::getlines_range> {aka ranges::v3::dangling<ranges::v3::_basic_iterator_::basic_iterator<ranges::v3::getlines_range::cursor> >}') auto input3 = std::stoi(*ranges::find_if_not(ranges::getlines(std::cin),empty_line)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
godbolt.org/g/gF6nYx
My question is, besides the documentation, are there any traits or any range convention to check if the range type owns?
Maybe something like constexpr bool is_owning_v(Rng&&)
c ++ traits range range-v3
sandthorn
source share