Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Incorrect compilation of lambda function parameters?

    General and Desktop
    3
    5
    101
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      warchild last edited by

      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

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @warchild last edited by

        @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++]);

        this

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

        is a misuse of ,

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 4
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply Reply Quote 4
          • W
            warchild last edited by

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

            1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators last edited by

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

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply Reply Quote 1
              • First post
                Last post