Skip to content

Commit

Permalink
morestl: add example slide for std::optional
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Lekshmanan <abhishek.lekshmanan@cern.ch>
  • Loading branch information
theanalyst committed Dec 16, 2021
1 parent b9dd9e7 commit a407cdb
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions talk/morelanguage/morestl.tex
Expand Up @@ -25,6 +25,31 @@ \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 in areas 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); // can fwd arguments to Phone constr.
}
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

0 comments on commit a407cdb

Please sign in to comment.