| Start |
|---|
| Calculating Factorials Factorials can be computed at compile-time using template metaprogramming techniques.
In the above code, we evaluate the The metafunction itself relies on recursively instantiating the same metafunction with smaller values. The Since template metafunctions execute at compile time, their results can be used in contexts that require compile-time values. For example: Automatic arrays must have a compile-time defined size. And the result of a metafunction is a compile-time constant, so it can be used here. Limitation: Most of the compilers won't allow recursion depth beyond a limit. For example, Since C++11, the Additionally, The body of Since C++14, many restrictions for Or even: Since c++17 one can use fold expression to calculate factorial: |
| Iterating over a parameter pack Often, we need to perform an operation over every element in a variadic template parameter pack. There are many ways to do this, and the solutions get easier to read and write with C++17. Suppose we simply want to print every element in a pack. The simplest solution is to recurse: We could instead use the expander trick, to perform all the streaming in a single function. This has the advantage of not needing a second overload, but has the disadvantage of less than stellar readability: For an explanation of how this works, see T.C's excellent answer (opens new window). With C++17, we get two powerful new tools in our arsenal for solving this problem. The first is a fold-expression: And the second is |
| Iterating with std::integer_sequence Since C++14, the standard provides the class template and a generating metafunction for it: While this comes standard in C++14, this can be implemented using C++11 tools. We can use this tool to call a function with a |
| Tag Dispatching A simple way of selecting between functions at compile time is to dispatch a function to an overloaded pair of functions that take a tag as one (usually the last) argument. For example, to implement The In this example, Tag dispatching can give you code that's much easier to read than the equivalents using SFINAE and Note: while C++17's |
| Detect Whether Expression is Valid It is possible to detect whether an operator or function can be called on a type. To test if a class has an overload of Since C++17, where For detecting if an operator, such as These can be used to use a |
| If-then-else The type This struct contains a pointer to |
| Calculating power with C++11 (and higher) With C++11 and higher calculations at compile time can be much easier. For example calculating the power of a given number at compile time will be following: Keyword Note: In C++11 Advantages: Comparing this to the standard way of compile time calculation, this method is also useful for runtime calculations. It means, that if the arguments of the function are not known at the compilation time (e.g. value and power are given as input via user), then function is run in a compilation time, so there's no need to duplicate a code (as we would be forced in older standards of C++). E.g. Another way to calculate power at compile time can make use of fold expression as follows: |
| Manual distinction of types when given any type T When implementing SFINAE (opens new window) using To help us with that, the standard already provides two types analog to The following example show how to detect if a type There are three steps in the above code (sometimes you only need two): Since
It is a good habit to provides "helper helper templates" that let you directly access the value: In C++17 and above, most helper templates already provide a |
| Generic Min/Max with variable argument count It's possible to write a generic function (for example RemarksMetaprogramming (or more specifically, Template Metaprogramming) is the practice of using templates (opens new window) to create constants, functions, or data structures at compile-time. This allows computations to be performed once at compile time rather than at each run time. |
Read Article
https://develop.horje.com/learn/1434/reference