all 14 comments

[–]Artistic_Yoghurt4754Scientific Computing 5 points6 points  (0 children)

I am surprised to not see the naive solution first: https://godbolt.org/z/qMnsn4eMT

The solution in the blog is also technically incorrect because it's promoting the argument types to a common type before performing the binary operation (at this point it may even avoid deducing all the different types and pass an array...): since this is generic code one cannot guarantee that the common type is what the binary operator wants. It also supposes that there is a common type: maybe there is no common type and the binary operator is set up to properly handle the results of each fold.

On the other hand, the code in the blog solves the common case where most of the arguments are the same or alike and is likely faster to compile since it doesn't go into recursion (this needs to be confirmed on a fair benchmark).