On Saturday, 23 April 2016 at 20:06:39 UTC, xtreak wrote:
map takes lambda as a template parameter and so does filter and many other functions. Sometimes they take something other than lambda as a template parameter. Eg. In case of to!int("5") int is a type and hence might need it as a template parameter but why does map and others take it as template parameter.

Adam D Ruppe pointed out in IRC it helps in inlining and optimization. Is there a thumb rule to decide this so that my functions too can benefit the performance and hence I could structure and understand my code better.

Non-template function parameters can not be inlined, since they're not determinable at compile-time, only at run-time. You can have two versions if you want, one with the function as a template parameter, and one with it as a function pointer.

I don't really think there is a rule of thumb to it other than using templates when you desire performance and also nicer syntax (in my opinion).

Templates also use up more space in the executable by the way, so if you're developing on a platform with very limited memory, then function pointers may be the better alternative.

Reply via email to