From a02f9078effaa9c5a03d6150f3863703f50de794 Mon Sep 17 00:00:00 2001 From: Abhishek Lekshmanan Date: Fri, 17 Dec 2021 14:51:45 +0530 Subject: [PATCH] more cosmetic fixes to fit code in the slides Signed-off-by: Abhishek Lekshmanan --- talk/morelanguage/morestl.tex | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/talk/morelanguage/morestl.tex b/talk/morelanguage/morestl.tex index 95c12122..c1174bf7 100644 --- a/talk/morelanguage/morestl.tex +++ b/talk/morelanguage/morestl.tex @@ -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 @@ -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] @@ -60,9 +55,9 @@ \subsection{More STL} \end{itemize} \begin{exampleblock}{Code example} \begin{cppcode*}{} - std::optional parse_phone(std::string_view in) { + std::optional parse_phone(string_view in) { if (is_valid_phone(in)) { - return in; // automatic conv. to std::optional(in); + return in;//automatic conv. to optional(in); } return {}; // default constructs std::nullopt } @@ -86,16 +81,16 @@ \subsection{More STL} \begin{cppcode*}{} using option_t = std::variant; option_t opt; // default constructs first type - std::variant myvariant; + std::variant myvariant; opt = 100; - int ival = std::get(opt); // also std::get<0>(opt) + int ival = std::get(opt);//also std::get<0>(opt) try { - float val = std::get(opt) // will throw as opt is int + float val = std::get(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(&opt)) - std::cout << "option type is f64, value=" << val; + std::cout<<"option type is f64, value="<< val; \end{cppcode*} \end{exampleblock} @@ -137,7 +132,7 @@ \subsection{More STL} m->take_hit(this->damage); } else if (Orc *o = std::any_cast(&e)) { o->take_hit(this->damage*0.5); - } else if (Cat* c = std::any_cast(&e)) {// No! + } else if (Cat* c = std::any_cast(&e)) {//No! c->pet(); } ....} \end{cppcode*}