|
202 | 202 | \end{cppcode*}
|
203 | 203 | \end{frame}
|
204 | 204 |
|
| 205 | +\begin{advanced} |
| 206 | + |
| 207 | +\begin{frame}[fragile] |
| 208 | + \frametitlecpp[17]{Non-type template parameter - auto} |
| 209 | + \small |
| 210 | + \begin{cppcode} |
| 211 | + template <auto Area> |
| 212 | + struct Shape { |
| 213 | + static constexpr auto area() { return Area; } |
| 214 | + }; |
| 215 | + // we use a type here to hold values |
| 216 | + using Area51 = Shape<51>; |
| 217 | + |
| 218 | + template<typename Base> |
| 219 | + struct Prism { |
| 220 | + double height; |
| 221 | + auto volume() const { return height * Base::area(); } |
| 222 | + }; |
| 223 | + Prism<Area51> prism1{2.}; |
| 224 | + Prism<Shape<3.14>> prism2{3.}; // C++20 |
| 225 | + \end{cppcode} |
| 226 | + \begin{block}{} |
| 227 | + \begin{itemize} |
| 228 | + \item Using a type to hold a value is a frequently trick |
| 229 | + \begin{itemize} |
| 230 | + \item See e.g.\ \mintinline{cpp}{std::integral_constant} |
| 231 | + \end{itemize} |
| 232 | + \end{itemize} |
| 233 | + \end{block} |
| 234 | +\end{frame} |
| 235 | + |
| 236 | +\begin{frame}[fragile] |
| 237 | + \frametitlecpp[20]{Non-type template parameter - literal types} |
| 238 | + \footnotesize |
| 239 | + \begin{cppcode*}{} |
| 240 | + template <typename T> struct Shape { |
| 241 | + T area_; |
| 242 | + constexpr auto area() const { return area_; } |
| 243 | + }; |
| 244 | + template<auto Base, typename T> struct RightPrism { |
| 245 | + T height; |
| 246 | + T volume() const { return height * Base.area(); } |
| 247 | + constexpr T area() const { ... } |
| 248 | + }; |
| 249 | + RightPrism<Shape{60.}, double> prism1{3.}; |
| 250 | + |
| 251 | + template<unsigned int N> struct Polygon { |
| 252 | + float radius; |
| 253 | + constexpr float area() const { ... } |
| 254 | + }; |
| 255 | + RightPrism<Polygon<4>{2.f}, double> prism2{3.}; |
| 256 | + RightPrism<RightPrism<Shape<42>, int>{3}, float> hyperprism{3.f}; |
| 257 | + \end{cppcode*} |
| 258 | + \begin{block}{} |
| 259 | + Enormous potential! We have yet to see what people will do with it! |
| 260 | + \end{block} |
| 261 | +\end{frame} |
| 262 | + |
| 263 | +\end{advanced} |
| 264 | + |
205 | 265 | \begin{frame}[fragile]
|
206 | 266 | \frametitlecpp[98]{Template specialization}
|
207 | 267 | \begin{block}{Specialization}
|
|
0 commit comments