https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123182

            Bug ID: 123182
           Summary: repeated calls to std::views::filter may give wrong
                    results
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: code at simerda dot eu
  Target Milestone: ---

When you create a `std::views::filter` over a `std::vector`, it works perfect
the first time but if you change the first item of the vector from matching the
filter condition to not matching it, you may still have it in the result.

I would assume views would lazily take vector's `begin()` and `end()` and do
the filtering each time it is materialized. I created a minimal reproducer with
a `bool` vector using `std::ranges::to<std::vector>()` and `std::println()` for
debugging the view.

Here's the minimal code:

```
#include <print>
#include <vector>
#include <ranges>
#include <functional>

int main() {
    std::array flags = {true};
    auto true_flags = flags | std::views::filter(std::identity{});
    std::println("{}", true_flags | std::ranges::to<std::vector>());
    flags[0] = false;
    std::println("{}", true_flags | std::ranges::to<std::vector>());
}
```

Build instructions:

Just simple `g++` with either `-std=c++23` or `-std=c++26`. Reproduced on
Fedora 43 as well as on compiler explorer with both trunk and release.

https://compiler-explorer.com/z/1Mh11Mbxz

Expected result:

```
[true]
[]
```

Actual result:

```
[true]
[false]
```

Thank you for the great work you're doing. It is amazing to see gcc-c++ and
libstdc++ in such a good shape regarding the current C++ standards. Looking
forward to seeing more from C++23 and C++26.

Reply via email to