mardi 23 février 2016

C++11 Multiline lambdas can deduce intrinsic types?

I use C++11 lambdas quite a lot, and I've often run into compile errors on multiline lambdas because I forgot to add the return type, as is expected, but I recently ran into one example that doesn't have this issue. It looks something like this:

auto testLambda = [](bool arg1, bool arg2)
{
    if (arg1)
    {
        if (!arg2)
        {
            return false;
        }

        return true;
    }

    return false;
};

This compiles just fine even though there's no return type specified. Is this just Visual Studio being dumb and allowing something it shouldn't, or can lambdas just always deduce intrinsic types?

I tried this with return values of all ints or floating point values and it also compiled just fine. I just found this to be really surprising so I wanted to be absolutely sure how it works before I start making assumptions and omitting return types that might break later on.

Aucun commentaire:

Enregistrer un commentaire