Home > front end >  What is the type of a lambda expression for the compiler?
What is the type of a lambda expression for the compiler?

Time:12-01

So, if we run the gdb on the following source code :

//...
auto myLambda1 = [&](int x){ printf("Hi, I got x=%d here\n",x);};
auto myLambda2 = [&](double y){ printf("Hi, I got y=%f here\n",y);};
//...

I will get :

(gdb) ptype myLambda1
type = struct <lambda(int)> {
}
(gdb) ptype myLambda2
type = struct <lambda(double)> {
}

So what is a lambda expression for a compiler? Why it's a struct

CodePudding user response:

The type of a lambda expression is a class type, but it has no name. The most important feature of the class is that it has a public operator() so that it can be used in a function-call expression syntax.

Tons of technical details are in the Standard section [expr.prim.lambda.closure], which begins with

The type of a lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type, called the closure type, whose properties are described below.

  • Related