Skip to content

VS 2022 17.4 reproduces different result with std::ranges compared to older version #3199

Description

@MennoK

Copying a view with filter and transform in VS 2022 17.4 (exact compiler version is MSVC 19.34.31933.0) to a vector behaves differently compared to older versions.

Below is a small code snippet is shown. The code should filter unique integer values and transform them to a string, and should be copied to a new vector.

#include <iostream>
#include <ranges>
#include <string>
#include <unordered_set>
#include <vector>

int main() {
    const std::vector<int> v = {1, 2, 2};
    std::unordered_set<int> set{};
    auto view =
        v | std::views::filter([&set](const int i) {
            std::cout << "checking filter for " << i << '\n';
            auto it = set.find(i);
            if (it != set.end()) return false;
            set.insert(i);
            return true;
        }) |
        std::views::transform([](const int i) { return std::to_string(i); });
    const auto js = std::vector<std::string>(view.begin(), view.end());
    std::cout << "js size is " << js.size() << '\n';
}

Godbolt link: https://godbolt.org/z/d7TTfq9K5

The expected result is that std::vector<std::string> js is size 2 and only includes "1" and "2", but with the newer VS compiler the vector is size 1 and contains only "1",

I'm not 100% sure whether this code sample has always been wrong, and the older compilers were printing the correct output by accident. Or a bug has been introduced with VS 2022 17.4..

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis issue is incorrect or by designrangesC++20/23 ranges

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions