Type of lambda function

There is this code:

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

Result: Z4mainEUliE_ , but the C ++ filter does not seem to explain what it is. What is a lambda expression type?

+7
source share
3 answers

Section 5.1.2 / 3 states:

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

The following goes on to say more, but this is the most important bit. A lambda is basically an instance of an anonymous class.

By the way, the demarcated form of your lambda is main::$_0 .

+10
source

The type of lambda function is not defined by the standard (ยง5.1.2):

The type of lambda expression (which is also the type of the closure object) is a unique, unnamed non-unit classtype โ€” called the closure type โ€” whose properties are described below. This type of class is not a collection (8.5.1). A closure type is declared in the smallest block region, class scope, or namespace region that contains the corresponding lambda expression.

He then lists the exact properties that the closure type should have.

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

+6
source

What is a lambda expression type?

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

According to clause 5.1.2 / 3 of the C ++ 11 standard:

The type of lambda expression (which is also the type of the closure object) is a unique, unnamed non-return type of the class โ€” called the closure type โ€” whose properties are described below. This type of class is not a collection (8.5.1). A closure type is declared in the smallest block area, in a class or namespace that contains the corresponding lambda expression. [...]

Also note that the member function name() class type_info (the type returned by typeid() ) is also implementation dependent, and the standard does not require it to be meaningful to humans.

In paragraph 18.7.1:

const char* name() const noexcept;

9 Returns: NTFS with implementation .

10 Notes: A message can be a multibyte string with a null character (17.5.2.1.4.2), suitable for conversion and display as wstring (21.3, 22.4.1.4)

+6
source

All Articles