I believe the relevant part of the standard is [res.on.objects], which states
If an object of a standard library type is accessed, and the beginning
of the object’s lifetime does not happen before the access, or the
access does not happen before the end of the object’s lifetime, the
behavior is undefined unless otherwise specified.
In your example, you access the std::function
by assigning to it. During this access, the destructor of the std::function
is called, ending the lifetime of the std::function
. But the access has not finished, so it is not the case that the access happens before the end of the object’s lifetime.
Therefore, the code has undefined behavior.