Are lambdas inlined like functions in C++?

First off: the whole point of the design of lambdas in C++ is that they don’t have an overhead compared to function calls. That notably includes the fact that calls to them can be inlined.

But there’s a confusion of concepts here: in the C++ standard, “inline” is the linkage of a function, i.e. it is a statement about how a function is defined, not how it gets called. Functions that are defined inline can benefit from a compiler optimisation by which calls to such functions are inlined. It’s a different but highly related concepts.

In the case of lambdas, the actual function being called is a member operator() that is implicitly defined as inline in an anonymous class created by the compiler for the lambda. Calls of the lambda are translated to direct calls to its operator() and can therefore be inlined. I’ve explained how the compiler creates lambda types in more detail in another answer.

Leave a Comment