4 ms·
> I remember insane discussions with people who thought adding two numbers with a template was "faster" than adding two numbers directly, even though the assemb
by anentropic 2y ago
> I remember insane discussions with people who thought adding two numbers with a template was "faster" than adding two numbers directly, even though the assembly language output was exactly the same.
I know almost nothing about C++ and have never programmed in it... but I thought that the point of that kind of 'template metaprogramming' was that the code got executed at compile time instead of runtime?
i.e. instead of generating identical assembly output the goal would have been to output a constant value
- sour-taste 2y agoAs per usual with c++ it depends. In some situations things are guaranteed to be compile time evaluated. E.g. even in early templates this would need to work: Func<1+2>(); Something like this there's no guarantees around: template <A, B> int Func() { return A + B; } For almost all compilers it should constant fold this into a constant but in theory it could end up with an add instruction. Basically we can't second guess the author here because it depends on the specifics.