I have some questions
Where is the memory location of the C++ lambda's code and captured variable?
when lambda's memory be free? (condition)
"std::function::operator=()" copy code or code's pointer?
is there any deep copy method for std::function?
I have some questions
Where is the memory location of the C++ lambda's code and captured variable?
when lambda's memory be free? (condition)
"std::function::operator=()" copy code or code's pointer?
is there any deep copy method for std::function?
Lambda expression is just a convenient shorthand for creating a temporary functional object (and defining its corresponding class). The object simply contains the captured values as its immediate data members. In that regard, lambdas do not really introduce anything qualitatively new into the language.
The lifetime of the lambda object would be the same if you declared the class explicitly and created that temporary functional object manually. Its "memory" is located wherever memory for any other temporaries is located. The language does define where it is. Such objects are destroyed in accordance with the same rules all other temporary objects are destroyed, i.e. they are automatically destroyed at the end of full expression (aside from those exceptional situations where lifetime of a temporary gets extended).
There's no concept of "copying the code" in C++. std::function
object simply uses an implementation-specific technique of storing the functor it was initialized with (functional objects, including lambdas, regular function pointers, member function pointers) in combination with some unspecified type-erasure technique. There's no need to "copy the code" for that.
It is not clear what you mean by "deep copy" for std::function
object. The interface specification of that class does not provide for any distinction between deep and shallow copying.