3 ms·
> one is writing code that is abstracted over types, and another is a clever trick called template metaprogramming I don't use either of these tricks much. Met
by Const-me 6y ago
> one is writing code that is abstracted over types, and another is a clever trick called template metaprogramming
I don't use either of these tricks much. Metaprogramming is especially bad, pretty much unreadable once written.
There's a third useful thing people do with templates - pass integer numbers to compiler. Unlike meta-programming, doesn't hurt readability.
There're instructions like _mm_shuffle_ps or _mm_srli_epi16 or _mm_extract_epi32 which encode an integer into machine codes. Instructions can encode memory references like `ptr [rax+32]`. And even without low-level trickery, it's sometimes useful to compile the same code multiple times, with changes somewhere. Templates is an easy way to express that in C++, here's an example: https://stackoverflow.com/a/59495197/126995 https://stackoverflow.com/a/59495197/126995. A nice related feature is `if constexpr` from C++/17, such branches are resolved at compile-time and therefore free.