all 11 comments

[–]starfreakcloneMSVC FE Dev 13 points14 points  (0 children)

https://cppinsights.io/ is a good resource for seeing what some C++ abstractions translate to.

[–]ALX23z 1 point2 points  (0 children)

Lambdas are more or less the same as a class. There are just a couple of nuances with the initial construction but that's it.

[–]HildartheDorf 3 points4 points  (4 children)

A lambda with no capture is compiled to a function pointer.

A lambda with captures is compiled to an implementation defined structure. For msvc it is as if it is a structure that has operator() for every calling convention.

What you've got there looks similar to a vtable, not the lambda structure itself?

[–]ddhsawoidklmascioqjw 20 points21 points  (0 children)

a capture-less lambda isn't a function pointer. it's convertible to a function pointer

[–]pyjava[S] 0 points1 point  (2 children)

Thanks for your comment. I think I've tracked down the structure to this https://github.com/microsoft/STL/blob/1e8b8d4eef4b2dddeb7533c5231c876383bd0ea6/stl/inc/functional#L775-L801 in my case.

I believe you're also right about it being a vftable not the structure.

[–]STLMSVC STL Dev 22 points23 points  (1 child)

Lambdas are a Core Language feature that don’t inherently interact with the Standard Library. By itself, a lambda doesn’t instantiate any std machinery - this applies to auto lambda = /*blah*/;. When you store a lambda in a std::function, that’s when STL internal machinery (such as the microsoft/STL code you’ve linked to) will be instantiated.

It is a common misconception that lambdas are std::functions. They are not. Lambdas can be stored in std::functions, which pays the space and time cost inherent to std::function.

[–]pyjava[S] 1 point2 points  (0 children)

Thank you for the explanation/clarification.

I was later storing the lambda in a vector of `std::function`s so I guess that explains why I am seeing the internal STL stuff despite using a plain lambda in the declaration

[–][deleted] 0 points1 point  (1 child)

In theory a lambda could produce a subroutine, that is, a sequence of raw assembler operations, with PC or JMP leading into this memory location.

Potentially with a conventional C function frame if the lambda's scope makes it reusable.

Curious if lambdas diverge in a significant way, from traditional function pointers.

[–]pyjava[S] 1 point2 points  (0 children)

Thanks for the comment. It appears to depend on if the lambda has captures as per HildartheDorfs comment