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
112 changes: 112 additions & 0 deletions talk/morelanguage/morestl.tex
Expand Up @@ -25,6 +25,118 @@ \subsection{More STL}
\end{block}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{Some new STL types \texttt{std::string\_view}}
\begin{itemize}
\item Non owning view of a continuous char sequence
\item Doesn't allocate memory, similar interface to std::string
\item Easy to copy, faster for some calls eg. substr (O(1) vs O(n))
\item Please ensure that the string pointed to outlives the string\_view!
\end{itemize}
\begin{exampleblock}{Some example uses}
\begin{cppcode*}{}
std::string_view sv {"Some example"};
auto first = sv.substr(0,sv.find_first_of(" "));
constexpr std::string_view location {"Geneva,CH"};
std::string some_string {"foo bar"};
std::string_view sv_str(some_string);
char foo[3] = {'f','o','o'};
std::string_view carr(foo, std::size(foo));
\end{cppcode*}
\end{exampleblock}
\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(string_view in) {
if (is_valid_phone(in)) {
return in;// equiv. to 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[17]{std::variant}
\begin{itemize}
\item Expresses intent that variable holds one of the types
\item Makes it easy to implement visitor pattern
\end{itemize}

\begin{exampleblock}{Code example}
\begin{cppcode*}{}
std::variant<int,float,string> opt{100}; // Now holding int
int ival = std::get<int>(opt); //also std::get<0>(opt)
try {
float val = std::get<float>(opt) // will throw...
} catch (const std::bad_variant_access& ex) {...}

// Or check the type before accessing it
if (std::holds_alternative<float>(opt))
std::cout<<"option type is fp32, value="<< std::get<float>(opt);
sponce marked this conversation as resolved.
Show resolved Hide resolved
\end{cppcode*}
\end{exampleblock}

\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{Visiting with std::variant}
sponce marked this conversation as resolved.
Show resolved Hide resolved
\begin{cppcode*}{}
using option_t = std::variant<int,float,string>;
struct Visitor {
void operator() (int i) { std::cout<< "i32:"<< i;}
void operator() (float f) { std::cout<< "f32:"<< f;}
void operator() (string s) { std::cout<< "s:"<< s;}
};
void print_opt(option_t opt) {
std::visit(Visitor{}, opt);
sponce marked this conversation as resolved.
Show resolved Hide resolved
}
...
print_opt(100); print_opt(3.14f); print_opt("example");
\end{cppcode*}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{std::any}
\begin{itemize}
\item Type safe alternative to void*
\item Useful for passing arbitrary values across interfaces that may not know
all the types
\item \texttt{any\_cast} will only match concrete types, inheritance hierarchy
is irrelevant
sponce marked this conversation as resolved.
Show resolved Hide resolved
\end{itemize}

\begin{exampleblock}{Code example}
\begin{cppcode*}{}
class Orc: public Monster {...};

void Player::attack(const std::any& e) {
if (Monster *m = std::any_cast<Monster>(&e)) {
m->take_hit(this->damage);
} else if (Orc *o = std::any_cast<Orc>(&e)) {
o->take_hit(this->damage*0.5);
} else if (Cat *c = std::any_cast<Cat>(&e)) {//No!
c->pet();
} ....}
\end{cppcode*}
\end{exampleblock}

\end{frame}

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