What type do lambdas get compiled into? [duplicate

2020-05-25 06:17发布

As I know all data types must be known at compile time, and lambda is not a type. Does lambda got translated into anonymous struct with operator() or std::function wrapped?

For example,

std::for_each(v.begin(), v.end(), [](int n&){n++;});

标签: c++ c++11 lambda
3条回答
The star\"
2楼-- · 2020-05-25 06:56

A variation of the as-if rule, the C++11 standard says:

§5.1.2/3 [..] An implementation may define the closure type differently from what is described below provided this does not alter the observable behavior of the program other than by changing:

— the size and/or alignment of the closure type,

— whether the closure type is trivially copyable (Clause 9),

— whether the closure type is a standard-layout class (Clause 9), or

— whether the closure type is a POD class (Clause 9).

I believe this is what people mean when they say that it's unspecified. However what's guaranteed as already stated in the other answers is the following:

Original author: Lightness Races in Orbit

[C++11: 5.1.2/3]: The type of the 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. 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. [..]

The clause goes on to list varying properties of this type. Here are some highlights:

[C++11: 5.1.2/5]: The closure type for a lambda-expression has a public inline function call operator (13.5.4) whose parameters and return type are described by the lambda-expression’s parameter-declaration-clause and trailing-return-type respectively. [..]

[C++11: 5.1.2/6]: The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.

查看更多
家丑人穷心不美
3楼-- · 2020-05-25 07:10

A lambda expression constructs an unnamed type, with each one having a different type. They are not std::function implementations. More info is provided here: What is a lambda expression in C++11? and here: How to convert a lambda to an std::function using templates

You can unveil the type on your specific compiler with a trick like this:

void foo(int);

int main() {
    auto a = []{ return 1; };
    auto b = []{ return 1; };

    foo(a);

    foo(b);

    return 0;
}

Compiling with clang on my mac gives:

/Users/jcrotinger/ClionProjects/so_lambda_type/main.cpp:11:5: error: no matching function for call to 'foo'
    foo(a);
    ^~~
/Users/jcrotinger/ClionProjects/so_lambda_type/main.cpp:5:6: note: candidate function not viable: no known conversion from 
'<lambda at /Users/jcrotinger/ClionProjects/so_lambda_type/main.cpp:8:14>' to 'int' for 1st argument
void foo(int);
     ^
/Users/jcrotinger/ClionProjects/so_lambda_type/main.cpp:13:5: error: no matching function for call to 'foo'
    foo(b);
    ^~~
/Users/jcrotinger/ClionProjects/so_lambda_type/main.cpp:5:6: note: candidate function not viable: no known conversion from 
'<lambda at /Users/jcrotinger/ClionProjects/so_lambda_type/main.cpp:9:14>' to 'int' for 1st argument
void foo(int);

@Barry points out that you can use typeid instead. If I print out typeid(a).name() and typeid(b).name() on my system, I get:

Z4mainE3$_0
Z4mainE3$_1

which demangle to

main::$_0
main::$_1

Just wanted to include this for completeness. I actually find the error message version a little more informative. :)

查看更多
Emotional °昔
4楼-- · 2020-05-25 07:12

From the standard §5.1.2.3:

The type of the lambda-expression... is a unique, unnamed non-union class type

It is its own type. Every time. So for instance:

auto a = []{ return 1; };
auto b = []{ return 1; };

a and b will necessarily have different types. They are both convertible to std::function<int()>, but not to each other:

std::function<int()> c = a; // OK
a = b; // NOPE

Adding a few more examples to add some clarity:

decltype(a) a2 = a; // OK, explicitly specifying the correct type

template <typename F>
void foo(F f) { ... }

foo(a); // calls foo<decltype(a)>, not foo<std::function<int()>
查看更多
登录 后发表回答