Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. "connect" with "lambda" C++ code analysis , please.

"connect" with "lambda" C++ code analysis , please.

Scheduled Pinned Locked Moved C / C++ / MFC
databasec++linqdata-structuresfunctional
14 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • K k5054

    I'm basing this on the runtime error, which you believe is something to do with "wrong sender". That seems to me to be a QT issue, not a C++ issue. AFAIK, there's no such thing as a "sender" in the C++ standard. Which points to QT, not C++

    "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #5

    OK, it is Qt issue.. I'll give few days for somebody else to respond , then I will take it down. Is that OK?

    K L 2 Replies Last reply
    0
    • L Lost User

      OK, it is Qt issue.. I'll give few days for somebody else to respond , then I will take it down. Is that OK?

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #6

      I'd just leave it, unless more senior members disagree. Otherwise, there's a discussion that someone else might stumble on and wonder what was going on. Who knows, maybe someone will stumble on this at some time in the future and be able to explain what the issue is.

      "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

      L 1 Reply Last reply
      0
      • L Lost User

        OK, it is Qt issue.. I'll give few days for somebody else to respond , then I will take it down. Is that OK?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #7

        Salvatore Terress wrote:

        then I will take it down. Is that OK?

        No, because that leaves orphaned messages which make little sense to others reading it. Incidentally, I did look at this code earlier and tried to make it readable, by removing all those extra braces in the first sample, but it still made little sense. As to the second sample, why are you trying to use a lambda expression for all that code? Use a proper function/member subroutine so you can see better what is (supposed to be) happening. And, as k5054 says, this is most likely a Qt issue rather than simple C++.

        J 1 Reply Last reply
        0
        • K k5054

          I'd just leave it, unless more senior members disagree. Otherwise, there's a discussion that someone else might stumble on and wonder what was going on. Who knows, maybe someone will stumble on this at some time in the future and be able to explain what the issue is.

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #8

          Here is my analysis in a nutshell. The "issue" is with Qt "connect" function / process... I have the "connect" working as expected , but ... I am passing parameters / indexes - main menu and submenu... ...that is what I thought I was doing , but ... the "parameters " values are NOT dynamic AKA used when "connect" is activated - click on sub menu. In my test - the main menu should be 0 and the submenu anything from 0 to 7 The code I have found uses lambda and it analyze the "connect" AND RETRIEVES the dynamic values of both indexes. I need the indexes to do what the menu / submenu is about - process something... I have modified the code because i CANNOT debug / step thru the lambda code ! So - the lambda code does work BUT ... it only works/ retrieves main menu index of 0 That is the real error and I am seeking assistance to find out why it only works on FIRST menu. My uneducated guess is - the obvious place to look is where the code step thru "main menu list"...

          if(parent)
              {
              QMenu\* menu=qobject\_cast(parent);
              int index=0;
              const QList actions=menu->actions();
              for(const QAction \*act : actions)
                  {
                  if(act
          

          ->text()==title)
          {
          path+=QString("(%1)").arg(index);
          break;
          }
          index++;
          }
          }

          K 1 Reply Last reply
          0
          • L Lost User

            Here is my analysis in a nutshell. The "issue" is with Qt "connect" function / process... I have the "connect" working as expected , but ... I am passing parameters / indexes - main menu and submenu... ...that is what I thought I was doing , but ... the "parameters " values are NOT dynamic AKA used when "connect" is activated - click on sub menu. In my test - the main menu should be 0 and the submenu anything from 0 to 7 The code I have found uses lambda and it analyze the "connect" AND RETRIEVES the dynamic values of both indexes. I need the indexes to do what the menu / submenu is about - process something... I have modified the code because i CANNOT debug / step thru the lambda code ! So - the lambda code does work BUT ... it only works/ retrieves main menu index of 0 That is the real error and I am seeking assistance to find out why it only works on FIRST menu. My uneducated guess is - the obvious place to look is where the code step thru "main menu list"...

            if(parent)
                {
                QMenu\* menu=qobject\_cast(parent);
                int index=0;
                const QList actions=menu->actions();
                for(const QAction \*act : actions)
                    {
                    if(act
            

            ->text()==title)
            {
            path+=QString("(%1)").arg(index);
            break;
            }
            index++;
            }
            }

            K Offline
            K Offline
            k5054
            wrote on last edited by
            #9

            Salvatore Terress wrote:

            I have modified the code because i CANNOT debug / step thru the lambda code !

            That does seem to be an issue with lambdas - at least gdb doesn't seem to be able to step into lambdas. I haven't tried it myself, so I can only go by what I've seen elsewhere on the subject. But. Why don't you just extract the function definition from the lambda and define it as a normal function. I think that should work

            foo( a, b, c, [](int x, int y) { /* do something; */ return val; } );

            becomes

            int lambda_f( int x, int y)
            {
            // body from lambda called in foo(), above
            return val;
            }

            // ...
            foo(a, b, c lambda_f);

            "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

            L 1 Reply Last reply
            0
            • L Lost User

              Salvatore Terress wrote:

              then I will take it down. Is that OK?

              No, because that leaves orphaned messages which make little sense to others reading it. Incidentally, I did look at this code earlier and tried to make it readable, by removing all those extra braces in the first sample, but it still made little sense. As to the second sample, why are you trying to use a lambda expression for all that code? Use a proper function/member subroutine so you can see better what is (supposed to be) happening. And, as k5054 says, this is most likely a Qt issue rather than simple C++.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #10

              Richard MacCutchan wrote:

              why are you trying to use a lambda expression for all that code?

              I agree with that strongly. If the developer can't figure out the lambda code then they shouldn't be using it in the first place. That isn't being denigrating since to me they are almost always confusing so I never voluntarily use them. They do nothing but make the code obtuse and the more complicated it is the more that becomes true. It also has the following impacts. - Maintenance programmers must figure it out. - I have seen more than one senior developer use it incorrectly making the code less efficient. Seems almost like there could be a coding guideline in that. If methods must be, for example, less than 100 lines of code, then seems that lambda expressions should also be limited perhaps to something like 10 clauses (or less.)

              L 1 Reply Last reply
              0
              • K k5054

                Salvatore Terress wrote:

                I have modified the code because i CANNOT debug / step thru the lambda code !

                That does seem to be an issue with lambdas - at least gdb doesn't seem to be able to step into lambdas. I haven't tried it myself, so I can only go by what I've seen elsewhere on the subject. But. Why don't you just extract the function definition from the lambda and define it as a normal function. I think that should work

                foo( a, b, c, [](int x, int y) { /* do something; */ return val; } );

                becomes

                int lambda_f( int x, int y)
                {
                // body from lambda called in foo(), above
                return val;
                }

                // ...
                foo(a, b, c lambda_f);

                "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #11

                Yes, I am now using plain function to analyze results of "connect". My code is still pretty messy , hard to read , since I added debug... But I can step thru it , so the debug is "leftover from lambda"... I got my code doing what I need... In retrospect - the problem was what I suspected from the start understanding "connect" and PLACING it into correct place. PS As far as getting help from forums - it was ABOUT 80% of "RTFM" and other "fill the blanks" to make "small talk"...

                1 Reply Last reply
                0
                • J jschell

                  Richard MacCutchan wrote:

                  why are you trying to use a lambda expression for all that code?

                  I agree with that strongly. If the developer can't figure out the lambda code then they shouldn't be using it in the first place. That isn't being denigrating since to me they are almost always confusing so I never voluntarily use them. They do nothing but make the code obtuse and the more complicated it is the more that becomes true. It also has the following impacts. - Maintenance programmers must figure it out. - I have seen more than one senior developer use it incorrectly making the code less efficient. Seems almost like there could be a coding guideline in that. If methods must be, for example, less than 100 lines of code, then seems that lambda expressions should also be limited perhaps to something like 10 clauses (or less.)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #12

                  Please read my previous reply. It was not my choice to use lambda - I took the code from another forum contributor... Using lambda was not that difficult - the code flow was pretty logical. The issue was - for whatever reason I could not "step thru" the lambda code...

                  K J 2 Replies Last reply
                  0
                  • L Lost User

                    Please read my previous reply. It was not my choice to use lambda - I took the code from another forum contributor... Using lambda was not that difficult - the code flow was pretty logical. The issue was - for whatever reason I could not "step thru" the lambda code...

                    K Offline
                    K Offline
                    k5054
                    wrote on last edited by
                    #13

                    I fully understand that. What I'm saying is that there is no reason that you to have to use a lambda. You can extract the function body and use a normal function pointer instead. And a normal function can be debugged by putting a break point a the function entry point. I'm not sure what your stumbling point is here. Consider:

                    struct S {
                    int i;
                    double d;
                    };

                    bool cmp_double(const S& s1, const S&s2) {
                    return s1.d < s2.d;
                    }

                    int main()
                    {
                    std::vector vs;
                    // ... add some data to vs ...

                     // sort the vector using the cmp\_double function: 
                    std::sort( vs.begin(), vs.end(), cmp\_double);
                    
                    // sort the vector using an anonymous lambda function
                    std::sort( vs.begin(), vs.end(), \[\](const S& x, const S&y) { return x.d < y.d } );
                    

                    }

                    The point is, the two sorts do exactly the same thing. So there's no reason you can't extract the lambda and use the named function as a parameter. If you're having trouble understanding this, you need to go back to your study material and review lambdas. I can recommend Justin Turners C++ weekly you-tube blog [https://www.youtube.com/playlist?list=PLs3KjaCtOwSZ2tbuV1hx8Xz-rFZTan2J1\](https://www.youtube.com/playlist?list=PLs3KjaCtOwSZ2tbuV1hx8Xz-rFZTan2J1) Just follow the link and then search for lambda, and watch the videos. It might be worth doing even if you do understand lambdas. It's not like another perspective can't help with the understanding.

                    "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                    1 Reply Last reply
                    0
                    • L Lost User

                      Please read my previous reply. It was not my choice to use lambda - I took the code from another forum contributor... Using lambda was not that difficult - the code flow was pretty logical. The issue was - for whatever reason I could not "step thru" the lambda code...

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #14

                      Salvatore Terress wrote:

                      It was not my choice to use lambda - I took the code from another forum contributor

                      Pretty sure that is a choice. As suggested in the other post you can rewrite it. Not sure I have ever used a code sample where I did not modify it.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups