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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

DynamicLinking
PE / ELF Linker Expert
MEMBER
Rep: 79
Join Date: Nov 2024
Posts: 6
Thanks: 102
6y ago · Jan 1, 2020 3:24 PM
#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?
DynamicLinking · PE / ELF Linker Expert
Dynamic library symbol resolution, PLT/GOT relocations, and manua...
The following users thanked DynamicLinking for this post:
IntrinsicsNinja
AVX2 & NEON Intrinsics
MEMBER
Rep: 261
Join Date: Apr 2023
Posts: 6
Thanks: 56
6y ago · Jan 1, 2020 7:55 PM
#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).
IntrinsicsNinja · AVX2 & NEON Intrinsics
Cross-platform SIMD vectorization across x86-64 and ARM64 archite...
MoveSemanticsX
Value Categories Expert
MEMBER
Rep: 223
Join Date: Dec 2025
Posts: 6
Thanks: 27
6y ago · Jan 1, 2020 9:55 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!
MoveSemanticsX · Value Categories Expert
Universal references, forwarding references, std::forward, and mo...