diff --git a/talk/morelanguage/templates.tex b/talk/morelanguage/templates.tex index df69828f..9c7f177b 100644 --- a/talk/morelanguage/templates.tex +++ b/talk/morelanguage/templates.tex @@ -202,6 +202,66 @@ \end{cppcode*} \end{frame} +\begin{advanced} + +\begin{frame}[fragile] + \frametitlecpp[17]{Non-type template parameter - auto} + \small + \begin{cppcode} + template + struct Shape { + static constexpr auto area() { return Area; } + }; + // we use a type here to hold values + using Area51 = Shape<51>; + + template + struct Prism { + double height; + auto volume() const { return height * Base::area(); } + }; + Prism prism1{2.}; + Prism> prism2{3.}; // C++20 + \end{cppcode} + \begin{block}{} + \begin{itemize} + \item Using a type to hold a value is a frequently trick + \begin{itemize} + \item See e.g.\ \mintinline{cpp}{std::integral_constant} + \end{itemize} + \end{itemize} + \end{block} +\end{frame} + +\begin{frame}[fragile] + \frametitlecpp[20]{Non-type template parameter - literal types} + \footnotesize + \begin{cppcode*}{} + template struct Shape { + T area_; + constexpr auto area() const { return area_; } + }; + template struct RightPrism { + T height; + T volume() const { return height * Base.area(); } + constexpr T area() const { ... } + }; + RightPrism prism1{3.}; + + template struct Polygon { + float radius; + constexpr float area() const { ... } + }; + RightPrism{2.f}, double> prism2{3.}; + RightPrism, int>{3}, float> hyperprism{3.f}; + \end{cppcode*} + \begin{block}{} + Enormous potential! We have yet to see what people will do with it! + \end{block} +\end{frame} + +\end{advanced} + \begin{frame}[fragile] \frametitlecpp[98]{Template specialization} \begin{block}{Specialization}