4 ms·
I'd like to introduce you to the wonderful world of templates.
by Chabs 8y ago
I'd like to introduce you to the wonderful world of templates.
- bergesenha 8y agoDo you have any examples of templates incurring a runtime cost at -O0 compared to a “no templates” approach?
- deleted 8y ago[deleted]
- Chabs 8y agoExpression templates (ala boost::spirit) definitely come to mind. The callstack in a -O0 build can get very, very deep. Heck just using std::numeric_limits<int>::min() will incur a function call in -O0, but nobody cares since it gets optimized in -O1 and up. The same thing applies more and more as you invoke templates from templates, but, again, nobody cares since we are confident it'll get optimized/inlined easily. It's not nearly as prevalent in modern code since constexpr static variables are now a thing, but relying on these optimizations has historically been super important.
- simias 8y agoBut that's the thing, you have complete control over templates, there's no compiler magic involved. If you're unhappy with the way a template call sequence gets optimized you can tweak it completely. In your comment below you mention boost but that's kind of my point: boost is a completely optional third party library, not a built-in construct of the language like the coroutines discussed here. Boost might be relying on the compiler correctly optimizing some constructs but the C++ template mechanics themselves not so much.