Important: Please read the Qt Code of Conduct -

Incorrect compilation of lambda function parameters?

  • Please if someone knows what is going on in this piece of code please explain.
    I have been coding for 20+ years and now I'm confused, am I getting to old for this?

    Im using Qt Creator 4.10, Desktop_Qt_5_12_3_MSVC2017_64bit.

        auto lam = [](double a, double b) { return a + b; };
        double arr[] = { 1.0, 2.0, 3.0, 4.0 };
        int i = 0;
        double result1 = lam(arr[i++], arr[i++]);
        double result2 = lam(arr[i++], arr[i++]);
        i = 0;
        int a = i++, b = i++;
        double result3 = lam(arr[a], arr[b]);
        a = i++, b = i++;
        double result4 = lam(arr[a], arr[b]);

    result1 = 2.0
    result2 = 6.0
    result3 = 3.0
    result4 = 7.0

  • Moderators

    @warchild said in Incorrect compilation of lambda function parameters?:

    this are unsequenced modifications to i and each compiler handles this differently as it is undefined behavour

    double result1 = lam(arr[i++], arr[i++]);


    a = i++, b = i++;

    is a misuse of ,

  • Lifetime Qt Champion


    IIRC, part of it is a question of how the compiler is grabbing the parameters of your function. There's no rule there and he can does it as it see fits. For example, gcc and clang do the reverse of each other. One goes from left to right and the other from right to left. They could also jump as they see fit if that can optimise some access to the data structure.

    So when playing with pointers that are going to be used to grab something as parameter to a function call, you better be explicit.

  • Yea, you right, I just read that evaluation of function parameters are unspecified.

  • Moderators

    Just an FYI, the order is also unspecified in C, but is fixed in left to right in Java

Log in to reply