Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added on slide on std::expected #494

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions talk/morelanguage/morestl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,35 @@
\end{exerciseWithShortcut}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[23]{std::expected \cpprefLink{https://en.cppreference.com/w/cpp/utility/expected}}
\begin{block}{Manages either of 2 values (expected or not)}
\begin{itemize}
\item templated by the 2 value types
\item Useful for the return value of a function that may fail
\begin{itemize}
\item and would then return another type (error type)
\end{itemize}
\end{itemize}
\end{block}
\begin{exampleblock}{}
\small
\begin{cppcode*}{}
enum class Error {...};
std::expected<int, Error> parse(std::string_view in) {
if (is_valid(in)) return convert_to_int(in);
return std::unexpected(Error::...);
}
auto v = parse(...);
if (v.has_value()) {
std::cout << *v; // (unchecked)
foo(v.value()); // may throw bad_expected_access
} else
log(v.error());
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{std::variant \cpprefLink{https://en.cppreference.com/w/cpp/utility/variant}}
\begin{block}{A type-safe union}
Expand Down