Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a8b46f

Browse files
author
Sebastien Ponce
committedOct 11, 2023
Added on slide on std::expected
1 parent 63ad561 commit 7a8b46f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
 

‎talk/morelanguage/morestl.tex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,35 @@
147147
\end{exerciseWithShortcut}
148148
\end{frame}
149149

150+
\begin{frame}[fragile]
151+
\frametitlecpp[23]{std::expected \cpprefLink{https://en.cppreference.com/w/cpp/utility/expected}}
152+
\begin{block}{Manages either of 2 values (expected or not)}
153+
\begin{itemize}
154+
\item templated by the 2 value types
155+
\item Useful for the return value of a function that may fail
156+
\begin{itemize}
157+
\item and would then return another type (error type)
158+
\end{itemize}
159+
\end{itemize}
160+
\end{block}
161+
\begin{exampleblock}{}
162+
\small
163+
\begin{cppcode*}{}
164+
enum class Error {...};
165+
std::expected<int, Error> parse(std::string_view in) {
166+
if (is_valid(in)) return convert_to_int(in);
167+
return std::unexpected(Error::...);
168+
}
169+
auto v = parse(...);
170+
if (v.has_value()) {
171+
std::cout << *v; // (unchecked)
172+
foo(v.value()); // may throw bad_expected_access
173+
} else
174+
log(v.error());
175+
\end{cppcode*}
176+
\end{exampleblock}
177+
\end{frame}
178+
150179
\begin{frame}[fragile]
151180
\frametitlecpp[17]{std::variant \cpprefLink{https://en.cppreference.com/w/cpp/utility/variant}}
152181
\begin{block}{A type-safe union}

0 commit comments

Comments
 (0)
Please sign in to comment.