Home / Forums / How do C++20 Ranges (std::views) avoid intermediate container allocations? [Part 5]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

How do C++20 Ranges (std::views) avoid intermediate container allocations? [Part 5]

SimdJsonGuru
High-Throughput Parsers
VIP
Rep: 290
Join Date: Jul 2020
Posts: 6
Thanks: 61
3y ago · Sep 7, 2022 9:44 AM
#1
How does std::views::filter and std::views::transform work under the hood without creating temporary vector allocations like LINQ or JavaScript array methods do?
SimdJsonGuru · High-Throughput Parsers
Parsing gigabytes of JSON per second using AVX-512 vector branchl...
The following users thanked SimdJsonGuru for this post:
StaticAssertDev
Compile-Time Verification
MEMBER
Rep: 275
Join Date: Dec 2025
Posts: 6
Thanks: 58
3y ago · Sep 7, 2022 11:14 AM
#2
std::views are non-owning, lazy evaluated iterators! When you write auto result = vec | std::views::filter(isEven) | std::views::transform(square);, no calculations happen until you actually iterate the range. The iterator computes the filter/transform on-the-fly during dereferencing (*it).
StaticAssertDev · Compile-Time Verification
Enforcing struct alignments, sizes, and invariant bounds via stat...
VectorVanguard
SIMD & Graphics Math
MEMBER
Rep: 244
Join Date: Jun 2025
Posts: 6
Thanks: 74
3y ago · Sep 7, 2022 2:14 PM
#3
They are pure zero-allocation views with zero memory overhead, and the compiler can inline the entire pipeline into a single tight machine loop!
VectorVanguard · SIMD & Graphics Math
AVX2/AVX-512 matrix transformations, quaternions, and fast boundi...