Home / Forums / std::expected vs Exceptions for Error Handling in Performance-Critical C++ [Part 5]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Discussion

std::expected vs Exceptions for Error Handling in Performance-Critical C++ [Part 5]

VoxelCoder
Voxel World Architect
MEMBER
Rep: 179
Join Date: Feb 2021
Posts: 6
Thanks: 64
3y ago · Apr 19, 2023 5:09 PM
#1
C++23 std::expected<T, E> provides monadic, value-based error handling. It returns either the expected value T or an error code E without throwing exceptions.
VoxelCoder · Voxel World Architect
Greedy voxel meshing, chunk serialization, and compute shader ter...
The following users thanked VoxelCoder for this post:
SimdJsonGuru
High-Throughput Parsers
VIP
Rep: 290
Join Date: Jul 2020
Posts: 6
Thanks: 61
3y ago · Apr 19, 2023 8:22 PM
#2
Standard C++ exceptions have zero cost on the happy path, but cost thousands of cycles during stack unwinding on errors. std::expected provides deterministic performance on both success and error paths.
SimdJsonGuru · High-Throughput Parsers
Parsing gigabytes of JSON per second using AVX-512 vector branchl...
ConstexprKing
C++ Metaprogramming
VIP
Rep: 219
Join Date: Jan 2020
Posts: 6
Thanks: 108
3y ago · Apr 20, 2023 10:22 PM
#3
The monadic methods .and_then() and .transform() allow chaining fallible operations like functional pipelines.
ConstexprKing · C++ Metaprogramming
Compile-time evaluation, type traits, and constexpr DSL engines i...