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

morestl: add examples for std::optional, any, variant #79

Merged
merged 10 commits into from Jan 27, 2022
50 changes: 50 additions & 0 deletions talk/morelanguage/morestl.tex
Expand Up @@ -25,6 +25,56 @@ \subsection{More STL}
\end{block}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{std::optional}
\begin{itemize}
\item Indicates the absence of a value with \texttt{std::nullopt}
\item Has value semantics (Copy, Move, Compare, stack alloc.)
\item Useful in place of pointers where value semantics are intuitive
\end{itemize}
\begin{exampleblock}{Code example}
\begin{cppcode*}{}
std::optional<Phone> parse_phone(std::string in)
{
theanalyst marked this conversation as resolved.
Show resolved Hide resolved
if (is_valid_phone(in)) {
return std::optional<Phone>(in);
theanalyst marked this conversation as resolved.
Show resolved Hide resolved
}
return {}; // default constructs std::nullopt
}
...
auto v = parse_phone(get_input());
if (v) { // alternatively v.is_valid()
process_phone(v.value()); // *v is equivalent
}
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{std::any}
theanalyst marked this conversation as resolved.
Show resolved Hide resolved
\begin{itemize}
\item Indicates the absence of a value with \texttt{std::nullopt}
\item Has value semantics (Copy, Move, Compare, stack alloc.)
\item Useful in place of pointers where value semantics are intuitive
\end{itemize}
\begin{exampleblock}{Code example}
\begin{cppcode*}{}
std::optional<Phone> parse_phone(std::string in)
{
if (is_valid_phone(in)) {
return std::optional<Phone>(in);
}
return {}; // default constructs std::nullopt
}
...
auto v = parse_phone(get_input());
if (v) { // alternatively v.is_valid()
process_phone(v.value()); // *v is equivalent
}
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[11]{non-member begin and end}
\begin{alertblock}{The problem in \cpp98}
Expand Down