auto a = [](){};
auto b = [](){};
vector<decltype(a)> v;
v.push_back(a); //ok
v.push_back(b); //compiler error
a and b have different type.
I am wondering if each lambda function actually is kind of anonymous class, whenever we create a lambda function, we create a new class with a random name which only visible to compiler ?
Yes, every lambda introduces its own unique type.
Now the same lambda can have multiple closures (lambda instances) associated with it in a few ways. C++14 return type deduction is the easiest:
auto nothing() {
return []{};
}
will always return the same type, but different instances. Similar things can be done by copying a lambda closure, or by passing a lambda in a type deduction context to a template function and storing it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With