Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is lifetime of lambda-derived implicit functors in C++?

Tags:

c++

lambda

The question is simple: what is lifetime of that functor object that is automatically generated for me by the C++ compiler when I write a lambda-expression?

I did a quick search, but couldn't find a satisfactory answer. In particular, if I pass the lambda somewhere, and it gets remembered there, and then I go out of scope, what's going to happen once my lambda is called later and tries to access my stack-allocated, but no longer alive, captured variables? Or does the compiler prevent such situation in some way? Or what?

like image 589
Fyodor Soikin Avatar asked Mar 05 '11 23:03

Fyodor Soikin


People also ask

What are functors in C?

A C++ functor (function object) is a class or struct object that can be called like a function. It overloads the function-call operator () and allows us to use an object like a function.

What happens when a lambda goes out of scope?

When a lambda object outlives one of its reference-captured objects, execution of the lambda object's function call operator results in undefined behavior once that reference-captured object is accessed. Therefore, a lambda object must not outlive any of its reference-captured objects.

Are functors the same as lambdas?

std::function<int(int,int)>; std::function<void(double)> ... Functors allow to write functional programs in C++. Lambdas are syntactic sugar to simplify this. With functors/lambdas classic patters from functional programming (e.g. map / filter /reduce) can be applied in C++.

What is lambda function in C++11?

In C++11 and later, a lambda expression—often called a lambda—is a convenient way of defining an anonymous function object (a closure) right at the location where it's invoked or passed as an argument to a function.

What are lambda functions in C++?

Going a bit further, lambda functions can also return another lambda function. This will open the doors of endless possibilities for customization, code expressiveness, and compactibility (by the way, there is no word like this) of code. Since C++17, a lambda expression can be declared as constexpr.

Do Lambda's have the same lifetime as function objects?

Once you understand that it becomes obvious that lambda's have the same value semantics as function objects, and so their lifetime is the same. Normal (allocated-on-the-stack) lifetime, but with the return value optimization? Wouldn't add be copied on return, not moved? Would a real lambda be moved?

How do lambda expressions have more power than ordinary functions?

A lambda expression can have more power than an ordinary function by having access to variables from the enclosing scope. We can capture external variables from enclosing scope by three ways :

What is implicit declaration of function in C?

Implicit declaration of the function is not allowed in C programming. Every function must be explicitly declared before it can be called. In C90, if a function is called without an explicit declaration, the compiler is going to complain about the implicit declaration. Here is a small code that will give us an Implicit declaration of function error.


1 Answers

Depends on how you capture your variables. If you capture them by reference ([&]) and they go out of scope, the references will be invalid, just like normal references. Capture them by value ([=]) if you want to make sure they outlife their scope.

like image 56
Xeo Avatar answered Sep 25 '22 20:09

Xeo