4 ms·
There is a simple trick to using complex template meta-programming techniques correctly: Leave it up to library authors. It sounds tongue in cheek, but I am se
by sqeaky 10y ago
There is a simple trick to using complex template meta-programming techniques correctly: Leave it up to library authors.
It sounds tongue in cheek, but I am serious. In the std library, boost and other sophisticated libraries there are tons of template shenanigans and there is little to be gain for most application developers. The single thing complex template meta-programs buy you is compile time evaluation for things in the middle of algorithms and classes.
Which is useful, but usually for things like deciding how many object should be allocated at once or how big a working set is to keep everything in cache. These are usually the performance optimizations you care about after all the algorithmic ones have been dealt with and often save only a constant number of instructions. When this seemingly insane level of optimization makes sense the author of a library will often accept the needed data as a parameter.
With constexpr things like lookup tables can easily be computed at compile with much more sane code.
- Animats 10y ago"You are not expected to understand this", the language. (When it breaks, then you have to.)
- sqeaky 10y agoIf that were true then every javascript coder would need to "understand" every javascript runtime. And every Ruby coder would need to understand C in case their libraries break. Every Java programmer would need to know the implementation of their JVM. Every ... In practice when the tools we rely on break, we have other alternatives. We tend to start with web searches and asking questions on SO. We can try different versions or libraries. Most problems have workarounds that may or may not be applicable. Sometimes deep knowledge helps and I will tend to advocate for it. In the case of template metaprogramming, deep knowledge gains you very little and a common breakage will add 2 extra instructions to your runtime execution. If that matters you are already watching for it. As soon as you use COM, RPC, a web API or any scripting language you have already done something that would blow this cost away for an abstraction. Knowing enough to fix a problem doesn't always mean a deep understanding of every library and tool even in C and C++ land. There is simply too much for any 1 person or any dedicated team to know, we must rely on division of labor. That is why we find abstractions like COM, RPC, web APIs and scripting languages worthwhile, they allow someone else to handle some part of our cognitive load at the cost of runtime performance.