Question

There is this code:

auto fun = [](int x)->int {return x + 1; };
std::cout << typeid(fun).name() << std::endl;

The result is: Z4mainEUliE_ but c++filt doesn't seem to explain what is it. What is type of lambda expression?

Was it helpful?

Solution

§5.1.2/3 states:

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type

It goes on to say more, but that's the most important bit. A lambda is basically an instance of an anonymous class.

Incidentally, the demangled form of your lambda is main::$_0.

OTHER TIPS

The type of a lambda function is unspecified by the standard (§5.1.2):

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union classtype — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression.

It then goes on listing the exact properties a closure type should have.

Therefore there is no general type for a lambda function to have. The compiler will generate a new functor type with unspecified name for each lambda function

What is type of lambda expression?

The type of a lambda expression (the so-called closure) is an unnamed class type with a function call operator automatically generated by the compiler. The internal name the compiler will give it is unspecified.

According to Paragraph 5.1.2/3 of the C++11 Standard:

The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type — called the closure type — whose properties are described below. This class type is not an aggregate (8.5.1). The closure type is declared in the smallest block scope, class scope, or namespace scope that contains the corresponding lambda-expression. [...]

Also notice, that the name() member function of the type_info class (the type returned by typeid()) is also implementation-dependent, and the Standard does not require it to be meaningful for a human.

Per Paragraph 18.7.1:

const char* name() const noexcept;

9 Returns: An implementation-defined NTBS.

10 Remarks: The message may be a null-terminated multibyte string (17.5.2.1.4.2), suitable for conversion and display as a wstring (21.3, 22.4.1.4)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top