Lifetime of lambda objects in relation to function pointer conversion

§5.1.2/6 says:

The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.

Emphasis mine.

In other words: because it’s an address of a function, and functions have no lifetime, you are free to call that function whenever you’d like. Everything you have is well-defined.

It’s a bit as if you’d done:

func_t retFun2()
{
    int __lambda0(int)
    {
        return 2;
    }

    struct
    {
        int operator(int __arg0) const
        {
            return __lambda0(__arg0);
        }

        operator decltype(__lambda0)() const
        {
            return __lambda0;
        }
    } lambda;

    return lambda; // just the address of a regular ol' function
}

Leave a Comment