View Single Post
  #8  
Old 09-05-2019, 14:36
chants chants is offline
VIP
 
Join Date: Jul 2016
Posts: 737
Rept. Given: 37
Rept. Rcvd 48 Times in 30 Posts
Thanks Given: 671
Thanks Rcvd at 1,064 Times in 482 Posts
chants Reputation: 48
Exactly right. The class you mention is originally known as a functor with the operator parenthesis.

And very notably though, lambdas are slightly more efficient. Because no class is truly constructed. The constructor you wrote is not actually called - it can be optimized out by the compiler. There is no constructor/destructor just copying capture values and invocation of the () method and of course the object is stack constructed not dynamically allocated.

A really thorough analysis is here:
Quote:
https://web.mst.edu/~nmjxv3/articles/lambdas.html
Useful conclusions:
Quote:
1. Functors and lambdas are always passed a this pointer, whereas plain functions naturally are not. This consumes an extra register and 8 bytes of stack space.
2. Lambda "constructors" are inlined into the function in which the lambda is created. This significantly reduces the amount of copying performed (2 instructions for lambdas, 5 for functors), as well as avoiding a function call setup and teardown.
Reply With Quote
The Following User Says Thank You to chants For This Useful Post:
zeffy (09-05-2019)