JohannesWilde
JohannesWilde

Reputation: 174

Why is a C++ 20 view not reevaluated after assignment?

I recently learned, that C++ 20 views can be assignable. Can anyone tell me, why the second v1.begin() below still points to the same element, instead of being reevaluated and thus skipped?

#include <iostream>
#include <ranges>
#include <vector>

int main()
{
    std::vector vec{1, 2, 3, 4, 5, 6};
    auto v1 = vec
             | std::views::filter(   [] (auto const i) { return i % 2 == 0; })
        ;

    std::cout << *v1.begin() << '\n'; // prints 2

    *v1.begin() = 13;

    std::cout << *v1.begin() << '\n'; // prints 13 <- why?
}

Upvotes: 2

Views: 112

Answers (0)

Related Questions