jeudi 24 août 2023

Is it legal to call the pointer which points to a lambda that is out of scope?

Is it legal to call the pointer which points to a lambda that does not exist anymore?

Here is the demo code snippet.

#include <iostream>

typedef int (*Func)(int a);

int main()
{
 Func fun;

 {
    auto lambda = [](int a)->int{std::cout << a << std::endl; return a;};   
    fun =lambda;
 }

 fun(6); //Is it legal? The variable lambda does not exist anymore.
}

What about generic condition, say lambdas which have captures?

Aucun commentaire:

Enregistrer un commentaire