Skip to content

Comment on C++ Compilation Speedparent

Comments

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!

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.