For std::ranges, why isn't a transform_view a borrowed_range? Here is a simple example: https://godbolt.org/z/14K8Y1xMe and below:
#include <ranges>
void foo() {
auto i = std::ranges::views::iota(10);
auto t = i | std::ranges::views::transform([](auto v) {return v;});
static_assert(std::ranges::borrowed_range<decltype(i)>);
static_assert(std::ranges::borrowed_range<decltype(t)>);
}
I checked that the base is a borrowed range. Is there an issue with the function? The documentation doesn't say: https://en.cppreference.com/w/cpp/ranges/transform_view.
transform_view internally saves the callable as a member, and its iterator saves a pointer to transform_view for accessing the callable. So when transform_view is destroyed, the callable is also destroyed, which causes its iterator to access a dangling pointer.
That's why it's not borrowed_range.
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