Skip to content

Commit

Permalink
more cosmetic fixes to fit code in the slides
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 17, 2021
1 parent c608d8d commit a02f907
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions talk/morelanguage/morestl.tex
Expand Up @@ -26,8 +26,7 @@ \subsection{More STL}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[17]{Some new STL types}
\begin{block}{\texttt{std::string\_view}}
\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
Expand All @@ -38,17 +37,13 @@ \subsection{More STL}
\begin{cppcode*}{}
std::string_view sv {"Some example"};
auto first_word = 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{block}
\end{frame}

\begin{frame}[fragile]
Expand All @@ -60,9 +55,9 @@ \subsection{More STL}
\end{itemize}
\begin{exampleblock}{Code example}
\begin{cppcode*}{}
std::optional<Phone> parse_phone(std::string_view in) {
std::optional<Phone> parse_phone(string_view in) {
if (is_valid_phone(in)) {
return in; // automatic conv. to std::optional<Phone>(in);
return in;//automatic conv. to optional<Phone>(in);
}
return {}; // default constructs std::nullopt
}
Expand All @@ -86,16 +81,16 @@ \subsection{More STL}
\begin{cppcode*}{}
using option_t = std::variant<int,float,string>;
option_t opt; // default constructs first type
std::variant<std::monostate, NonDefault, int, float> myvariant;
std::variant<std::monostate, NonDefault, int> myvariant;
opt = 100;
int ival = std::get<int>(opt); // also std::get<0>(opt)
int ival = std::get<int>(opt);//also std::get<0>(opt)
try {
float val = std::get<float>(opt) // will throw as opt is int
float val = std::get<float>(opt) // will throw...
} catch (const std::bad_variant_access& ex) {...}

// Much easier with get_if returns a ptr or null if not conv.
// Much easier with get_if returns a ptr or null
if (const float* val = std::get_if<float>(&opt))
std::cout << "option type is f64, value=" << val;
std::cout<<"option type is f64, value="<< val;
\end{cppcode*}
\end{exampleblock}
Expand Down Expand Up @@ -137,7 +132,7 @@ \subsection{More STL}
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!
} else if (Cat* c = std::any_cast<Cat>(&e)) {//No!
c->pet();
} ....}
\end{cppcode*}
Expand Down

0 comments on commit a02f907

Please sign in to comment.