3 ms·
Templates only generate maximally-efficient code in theory. Generality kills them in practice; or, it makes the template code twisted to the point of insanity.
by makecheck 10y ago
Templates only generate maximally-efficient code in theory. Generality kills them in practice; or, it makes the template code twisted to the point of insanity. For example, there is no space-efficient way to model optional<T> for more than one value in a structure type; if you have several optional fields, it would make much more sense to be able to track optional state as single bits of one field (say) but the class structure prohibits such a space optimization and requires each template to track its own is-defined value. Pointless.
It is also essentially impossible to know that your code is well-tested: even when using something large and well-known like Boost, its authors cannot know what will happen when you substitute in 4 of your own types (my bet is “result not quite working as intended; obscure bugs added”).
I use templates but I am quite aware of code complexity and I essentially stop when the templating goes too far.
Somewhere along the line, we seem to have gotten into a mode of wanting generic code at all costs. Why? It is just fine to expose a few common specific methods when those are very helpful and clear (such as classes exposing a “string value”, “integer value” and “float value” instead of trying to provide some single partially-specialized template mess of a method that manages to still not quite work right in every scenario).
- Animats 10y agoSomewhere along the line, we seem to have gotten into a mode of wanting generic code at all costs. I know. Years ago I proposed a vector library for 2, 3, and 4 element vectors [1] as a standard. The immediate complaint was that it didn't use templates. [1] http://www.animats.com/source/graphics/algebra3.h http://www.animats.com/source/graphics/algebra3.h
- slavik81 10y agoThe trouble is that a reasonable requirement such as single-precision floating point support would require a developer to copy/paste/modify hundreds of LOC. The choice is really fork, duplicate or template. I don't blame glm for picking the latter. Every option is painful, but templates might be a little less so.