Is there a trait or some convention to check if there is a range or `view_facade` that owns things? (e.g. getlines)

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); // at this point the `owning` range destroyed, // so as the buffer we (should've) hold (before!). // So this won't compile // auto input2 = std::stoi(*iter2); 

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&&)

+7
c ++ traits range range-v3
source share

No one has answered this question yet.

See related questions:

116
PHP traits - examples of real world examples / best practices?
7
PHP Traits naming convention?
6
Using Any with Traits in Rust
4
How does range-v3 `partial_sum` not contradict non-referenced semantics?
4
Scala: Appointment. Like a feature suffix, for example. IndexSequenceLike
2
Custom Attributes
one
It is impossible to realize a trait that I do not own for all types that implement a trait that I myself
one
Scala targeting naming convention when a tag matches a class
0
Call your own method from the property
-one
A trait is not implemented (a thing that implements a trait)

All Articles