Hacker News new | ask | show | jobs
by shin_lao 5787 days ago
Templates are much more powerful than C with macros.

Templates are more than copy/paste code, the compiler can make strong assumptions about the code path during the optimization phase because it's actual code.

It's not just a question of inlining calls but mainly a question of being able to remove branches altogether. Branches is the performance killer, remember?

Examples:

- with template specialization you can make "compile time" switches (horrible to reproduce with C + macros) - you can compute values at compile time (not quite possible in C + macros) - inlining permits copy elision (an optimization that doesn't exist in C since return by value is a non sequitur) - the CRTP is faster than an object with vtables because it avoids indirect calls - removing of dead branches, by inspecting the templates recursion, the compiler can determine which branches will never be taken and doesn't compile them - probably more!