See this example code:
#include <ranges>
int main() {
for(auto i : std::ranges::iota_view(1) | std::views::reverse)
break;
}
It compiles on gcc (I cannot check on clang/msvc - since they do not support ranges). Of course -- it runs "forever" and does nothing.
I also checked that doing std::ranges::rbegin(inf)
or std::ranges::rend(inf)
on infinite range is not allowed (it does not compile).
I am not sure if this is correct c++ code? And I am curious about std::ranges::reverse implementation - looks like rbegin/rend is not used to implement this view -- so how this implementation works?
According to [iterator.requirements.general-10]:
A sentinel
s
is called reachable from an iteratori
if and only if there is a finite sequence of applications of the expression++i
that makesi == s
. Ifs
is reachable fromi
, [i
,s
) denotes a valid range.
And [iterator.requirements.general-12]:
The result of the application of library functions to invalid ranges is undefined.
Since ranges::iota_view(1)
is not a valid range, applying views::reverse
to it is undefined behavior.
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