Skip to content

Commit 711278b

Browse files
committed
Add footnote
1 parent f63a4e1 commit 711278b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

_posts/2025-10-06-cpp-std-expected.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ description: "Practical introduction to std::expected: intent-revealing, structu
66
image: /images/cpp_logo.png
77
hero_image: /images/cpp_logo.png
88
hero_darken: true
9-
tags: cpp, ai, cmake
9+
tags: cpp ai cmake
1010
---
1111

12-
**How to handle errors in C++ has been a constant point of debate.** Do you use exceptions, error code, out-parameters or return nullptrs on failure? And how do you convey information on the nature of the failure? With C++17 we got `std::optional` for "value or nothing" semantics, but it lacks error context. [C++23 - finally - introduces `std::expected`](https://en.cppreference.com/w/cpp/utility/expected.html), a type that encapsulates either a value or an error, making error handling explicit and composable. Let's explore how `std::expected` can improve your C++ code.
12+
**How to handle errors in C++ has been a constant point of debate.** Do you use exceptions, error code, out-parameters or return nullptrs on failure? And how do you convey information on the nature of the failure? With C++17 we got `std::optional` for "value or nothing" semantics, but it lacks error context. [C++23 - finally - introduces `std::expected`](https://en.cppreference.com/w/cpp/utility/expected.html)[^1], a type that encapsulates either a value or an error, making error handling explicit and composable. Let's explore how `std::expected` can improve your C++ code.
1313

1414
## `std::expected` in a nutshell
1515

16-
Semantically, `std::expected<T, E>` is a returnable type that can either hold a value of type `T` (indicating success) or an error of type `E` (indicating failure). This makes it clear to the caller that a function can fail and provides a structured way to handle that failure.
16+
At the core, `std::expected<T, E>` is a returnable type that can either hold a value of type `T` (indicating success) or an error of type `E` (indicating failure). This makes it clear to the caller that a function can fail and provides a structured way to handle that failure.
1717

1818
Let's look at a simple example of a function that computes the square root of a number, returning an error if the input is negative:
1919

@@ -192,4 +192,6 @@ With these practices in mind, `std::expected` and its combinators can greatly en
192192
193193
## final thoughts
194194
195-
With the arrival of `std::expected` in C++23 there is another powerful tool in C++ to allow more expressive code in a functional programming style. This can make applications that do a lot of data processing and have many recoverable failure paths much cleaner and easier to maintain. While it does not replace exceptions for unrecoverable errors, it nicely complements them by providing a structured way to handle expected errors. And the beauty of it is, that it still works seamlessly with existing C++ code and libraries - So no need to go all in and change it everywhare. So give it a try in your next C++ project and see how it can improve your error handling!
195+
With the arrival of `std::expected` in C++23 there is another powerful tool in C++ to allow more expressive code in a functional programming style. This can make applications that do a lot of data processing and have many recoverable failure paths much cleaner and easier to maintain. While it does not replace exceptions for unrecoverable errors, it nicely complements them by providing a structured way to handle expected errors. And the beauty of it is, that it still works seamlessly with existing C++ code and libraries - So no need to go all in and change it everywhere. So give it a try in your next C++ project and see how it can improve your error handling!
196+
---
197+
[^1]: While `std::expected` became part of the C++ standard with C++23, it has been around for quite some time as an [open-source library](https://github.com/TartanLlama/expected) by Sy Brand before that.

0 commit comments

Comments
 (0)