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. can anybody explain below lamda function syntax ?

can anybody explain below lamda function syntax ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 595 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1
    thread = QThread::create([=](){
           counting_memberFUnction();
        });
    

    actually i not get understand why lamda function written like below

    [=](){
           counting_memberFUnction();
        });
    
    Christian EhrlicherC Pl45m4P S 3 Replies Last reply
    0
    • Q Qt embedded developer

      @SimonSchroeder means [this] will work instead of [=].

      yes i am confused at syntax [=].

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #6

      @Qt-embedded-developer
      [this] is sufficient (and better) if you only need to access class members (variables or methods) in the lambda code.

      [=] gives this too, but also all local variables (passed by value, not reference) in the function where the lambda is defined. It is "convenient" but "lazy". You can instead always choose explicitly to put it all local variables and/or this to pass just the specified ones, which is "better" coding style.

      That is what [=] does. Note that there is a similar [&] which does just the same thing but passes the local variables by reference instead.

      1 Reply Last reply
      3
      • Q Qt embedded developer
        thread = QThread::create([=](){
               counting_memberFUnction();
            });
        

        actually i not get understand why lamda function written like below

        [=](){
               counting_memberFUnction();
            });
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Qt-embedded-developer said in can anybody explain below lamda function syntax ?:

        actually i not get understand why lamda function written like below

        Because this is the lambda syntax defined by the c++ standard.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3
        • Q Qt embedded developer
          thread = QThread::create([=](){
                 counting_memberFUnction();
              });
          

          actually i not get understand why lamda function written like below

          [=](){
                 counting_memberFUnction();
              });
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #3

          @Qt-embedded-developer

          Hi,

          I wrote this explanation some years ago

          • https://forum.qt.io/post/631874

          [...] is the scope/what is captured from current class to make it visible inside the lambda body (content).
          ( ... ) in there are parameters to the function itself.
          { ... } lambda body, containing the actual code which runs on execution.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          2
          • Q Qt embedded developer
            thread = QThread::create([=](){
                   counting_memberFUnction();
                });
            

            actually i not get understand why lamda function written like below

            [=](){
                   counting_memberFUnction();
                });
            
            S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #4

            @Qt-embedded-developer said in can anybody explain below lamda function syntax ?:

            actually i not get understand why lamda function written like below

            Which part of the lambda confuses you? Using [=] looks like laziness. It would be better (with the little code you provide) just to capture this which would be sufficient.

            Q 1 Reply Last reply
            0
            • S SimonSchroeder

              @Qt-embedded-developer said in can anybody explain below lamda function syntax ?:

              actually i not get understand why lamda function written like below

              Which part of the lambda confuses you? Using [=] looks like laziness. It would be better (with the little code you provide) just to capture this which would be sufficient.

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on last edited by
              #5

              @SimonSchroeder means [this] will work instead of [=].

              yes i am confused at syntax [=].

              JonBJ 1 Reply Last reply
              0
              • Q Qt embedded developer

                @SimonSchroeder means [this] will work instead of [=].

                yes i am confused at syntax [=].

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @Qt-embedded-developer
                [this] is sufficient (and better) if you only need to access class members (variables or methods) in the lambda code.

                [=] gives this too, but also all local variables (passed by value, not reference) in the function where the lambda is defined. It is "convenient" but "lazy". You can instead always choose explicitly to put it all local variables and/or this to pass just the specified ones, which is "better" coding style.

                That is what [=] does. Note that there is a similar [&] which does just the same thing but passes the local variables by reference instead.

                1 Reply Last reply
                3
                • Q Qt embedded developer has marked this topic as solved on

                • Login

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