frek wrote: |
---|
Up to now what I've gathered about meta programming is that it is generally used where there's some code that can be performed/evaluated at compile time which involves const/constexpr data/functions. But to me, this feature is almost useless. It's as though there's a code that when called it delivers the factorial of 10, a fixed number each time! We could use the exact number which equals to the return value of such a function, instead. Since nothing is at run time, so the benefits is almost nothing! :| But it must not be this way and I'm almost sure I'm mistaken. |
But that is only one part of TMP, here is the first paragraph of the wiki page:
wiki wrote: |
---|
Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates can include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time polymorphism. The technique is used by a number of languages, the best-known being C++, but also Curl, D, Nim, and XL. |
With constants, the idea is to calculate them
once at compile time, then use them
many times as an actual value (with no calculation time )at runtime. If there is a small number of constants, one could some other method to calc them (a spreadsheet say) then have them as a vector of literal values - this would achieve the same thing. Or one could write another c++ program to calc these values and store them in a file, then your program can read them from the file - but this involves the overhead of reading the file.
I couldn't read the boost page since I'm not used to it and therefore it can't make an effect to clear the ambiguity of the whole idea of meta programming to where or where use it. |
When trying to digest complex articles, try to get the overall picture into your head first. Then break it down into pieces, understand them, look at examples.
With the Hana library they talk about the 4 computational quadrants:
1. Runtime computations which are the usual computations we use in C++. In that world, we have runtime containers, runtime functions and runtime algorithms:
2. constexpr computations. There, we have constexpr containers, constexpr functions and constexpr algorithms: These maybe done at compile time.
3. heterogeneous computations. Heterogeneous computations differ from normal computations in that instead of having containers holding homogeneous objects (all objects having the same type), the containers may hold objects with different types. Furthermore, functions in this quadrant of computation are heterogeneous functions, which is a complicated way of talking about template functions. Similarly, we have heterogeneous algorithms that manipulate heterogeneous containers and functions:
4. type-level computations. In this quadrant, we have type-level containers, type-level functions (usually called metafunctions) and type-level algorithms. Here, everything operates on types: containers hold types and metafunctions take types as arguments and return types as results.
Here is another use of TMP, on my Linux system there is the type traits header file, it is #include <type_traits>
/usr/include/c++/11/type_traits
It may be a little hard to read, but one can see how they build up types:
There is
integral_constant
, then
true_type
and
false_type
and so on - there are lots of them. One can see how these type are made by inheriting from a previously defined type. All this is used by the things found on this page:
https://en.cppreference.com/w/cpp/types#Type_traits
I hope this helps :+)