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..
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.
Godbolt link: https://godbolt.org/z/d7TTfq9K5
The expected result is that
std::vector<std::string> jsis 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..