3 ms·
> The only super legit argument I've heard in favor of _not_ using templates or avoiding them in general is because of the code generation bloat And even then,
by sjolsen 12y ago
> The only super legit argument I've heard in favor of _not_ using templates or avoiding them in general is because of the code generation bloat
And even then, it's important to recognize that bloat is a relative measure. If you use just one instantiation of a template, you're not paying for multiple versions of the code.
If, on the other hand, you use multiple instantiations, the code will be bloated compared to, say, a single type-erased version— but will not be any bigger than if you had written several "instantiations" of the same code by hand. There's a tradeoff to be made, too, since a statically-typed version of a function will probably be faster than a type-erased version.
Anyway, w.r.t. bloat, templates aren't the problem; static polymorphism is (can be). Templates are just a way of implementing static polymorphism.
- kazinator 12y agoIndeed, if you use one expansion of a template, then the bloat is just that of whatever code the template generates, which would still be there if you wrote the same inline code by hand. If that code is inline code, you get the same inline bloat.