int y = 3;
auto f = [y](int x) { return x*y; };
this is a C++11 lambda. The complier (basically) converts it to:
struct __anonymous_name__ {
int operator()(int x) const { return x*y; }
int y;
};
__anonymous_name__ f = {y};
where everything with __
in the name is not actually named, just given names for exposition purposes.
At runtime, everything has a fixed type, no code is generated.
std::function<int(int)>
can store a copy of f
above, but that uses a type erasure mechanism that goes beyond the scope of this question. Note, however, that f
is not an object of type related to std::function<int(int)>
; C++ has more than one kind of polymorphism.
I also seriously doubt that Java/C# lambdas are JIT'd any more than the rest of your code.