Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Incorrect compilation of lambda function parameters?
QtWS25 Last Chance

Incorrect compilation of lambda function parameters?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 315 Views
  • 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 Offline
    W Offline
    warchild
    wrote on last edited by
    #1

    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.HilkJ 1 Reply Last reply
    0
    • W warchild

      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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


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

      1 Reply Last reply
      4
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        4
        • W Offline
          W Offline
          warchild
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            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


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

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved