Home / Forums / Why you should prefer std::string_view over const std::string& for read-only parameters [Part 3]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Why you should prefer std::string_view over const std::string& for read-only parameters [Part 3]

CacheOptimizer
Data-Oriented Architect
VIP
Rep: 111
Join Date: Jul 2020
Posts: 6
Thanks: 34
3y ago · Sep 7, 2022 12:32 PM
#1
Passing const std::string& forces string literals or char arrays to allocate a temporary heap std::string before calling the function. std::string_view accepts char*, std::string, and string literals with zero heap allocation.
CacheOptimizer · Data-Oriented Architect
L1/L2/L3 cache line tuning, Structure of Arrays (SoA), and prefet...
The following users thanked CacheOptimizer for this post:
GpuComputeDev
CUDA & Compute Shaders
MEMBER
Rep: 202
Join Date: Dec 2025
Posts: 6
Thanks: 48
3y ago · Sep 7, 2022 2:45 PM
#2
std::string_view holds only a pointer and a length (16 bytes in x64), fitting entirely within CPU registers during function calls.
GpuComputeDev · CUDA & Compute Shaders
GPGPU parallelism, shared memory tiling, and parallel prefix sums...
MathEngineX
Game Physics & Math
MEMBER
Rep: 232
Join Date: Aug 2021
Posts: 6
Thanks: 107
3y ago · Sep 7, 2022 5:45 PM
#3
Just be careful not to store string_views that outlive the original string buffer!
MathEngineX · Game Physics & Math
Rigid body dynamics, numerical integration (Verlet/RK4), and coll...