Pull to refresh

Comments 2

Firstly, the lambda function we wrote can`t be compiled to an assembly
function and is not a function in the sense of the definition given
above

Wrong. Lambda is compiled into assembly just like any other function. The only difference is that lambda cannot be converted to a function pointer. Because if you read the standard,

The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type, known as closure type, which is declared (for the purposes of ADL)
in the smallest block scope, class scope, or namespace scope that
contains the lambda expression. The closure type has the following
members, they cannot be explicitly instantiated, explicitly specialized, or (since C++14) named in a friend declaration:

In simple terms, lambda is a shorthand for

struct <UnnamedAnonimousType> {
  // lambda captures 
  return_type operator()(lambda args...) const {
    //lambda body 
  }
}

What I meant is that lambda function that captures something from the outside, can`t be compiled to a function, because in terms of the language, as you said, it is an object that own other objects.

As for the struct, I feel like this is more of an imaginary construct - you won`t see the compiler generate a constructor or a destructor for it or really pass it to another function - as it seems to always inline it.

Sign up to leave a comment.

Articles