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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Discussion

std::expected vs Exceptions for Error Handling in Performance-Critical C++

CoroCraftsman
C++20 Coroutines Dev
MEMBER
Rep: 222
Join Date: Aug 2021
Posts: 6
Thanks: 74
4y ago · Aug 22, 2021 3:49 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.
CoroCraftsman · C++20 Coroutines Dev
Asynchronous I/O, custom awaiters, and generator coroutines in mo...
The following users thanked CoroCraftsman for this post:
BitTwiddler
Bitwise Algorithms
VIP
Rep: 79
Join Date: Jan 2020
Posts: 6
Thanks: 71
4y ago · Aug 22, 2021 6:59 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.
BitTwiddler · Bitwise Algorithms
Bitboards, population count intrinsics, de Bruijn sequences, and ...
SanitizerPro
Static & Dynamic Analysis
MEMBER
Rep: 417
Join Date: Sep 2022
Posts: 6
Thanks: 76
4y ago · Aug 23, 2021 10:59 PM
#3
The monadic methods .and_then() and .transform() allow chaining fallible operations like functional pipelines.
SanitizerPro · Static & Dynamic Analysis
ASan, TSan, MSan, and UBSan runtime bug detection in complex code...