Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Recurring C++ and Qt anti-patterns
Forum Updated to NodeBB v4.3 + New Features

Recurring C++ and Qt anti-patterns

Scheduled Pinned Locked Moved The Lounge
126 Posts 17 Posters 70.0k Views 10 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.
  • Chris KawaC Online
    Chris KawaC Online
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by Chris Kawa
    #36

    I had to dig through this thing once, only the real code was like a hundred times longer and more convoluted.

    // Library.h statically linked to and included in DLL and EXE
    struct SomeType
    {
       int typeId();
    };
    Q_DECLARE_METATYPE(SomeType);
    
    // Library.cpp
    int SomeType::typeId()
    {
        return qMetaTypeId<SomeType>();
    }
    
    // main app
    SomeType& var1 = getItFromDLL();
    SomeType& var2 = getItFromEXE();
    
    bool same = var1.typeId() == var2.typeId(); // nope
    

    Pretty ugly thing to debug, especially since once in blue moon it actually works :/

    kshegunovK 1 Reply Last reply
    2
    • B Offline
      B Offline
      Brunner2
      Banned
      wrote on last edited by
      #37
      This post is deleted!
      1 Reply Last reply
      0
      • sierdzioS sierdzio

        @Asperamanca said in Recurring C++ and Qt anti-patterns:

        This brings me to a philosophical question: Do I want to be able to emit a signal from a const method, although the slot(s) attached to the signal may well modify data the originating const method could not itself modify?

        Yes, it's very debatable :D I did find a few occasions where it was useful (latest example: modifying behaviour of QTreeView without patching Qt - I have emitted a signal from const overloaded method and did my modifications there), but I agree it does not feel "right".

        Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #38

        @sierdzio said in Recurring C++ and Qt anti-patterns:

        Yes, it's very debatable :D I did find a few occasions where it was useful (latest example: modifying behaviour of QTreeView without patching Qt - I have emitted a signal from const overloaded method and did my modifications there), but I agree it does not feel "right".

        Actually, I have zero problem with this. The way my mind works it makes perfect sense, as the signal is a message to a receiving class (any class). It's not the sender method that modifies the object state. It is the message. My mind differentiates between the two.

        1 Reply Last reply
        0
        • fcarneyF fcarney

          @aha_1980
          Apparently the standard allows for it:
          https://stackoverflow.com/questions/704466/why-doesnt-delete-set-the-pointer-to-null

          The creator himself wonders why it isn't so. Its like C++ is this beautiful, amazing, and now, WILD animal roaming free in cyberspace... Yeah, maybe the analogy isn't all that great, but it does conjure up a cool picture.

          Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #39

          @fcarney said in Recurring C++ and Qt anti-patterns:

          Apparently the standard allows for it:
          https://stackoverflow.com/questions/704466/why-doesnt-delete-set-the-pointer-to-null
          The creator himself wonders why it isn't so. Its like C++ is this beautiful, amazing, and now, WILD animal roaming free in cyberspace... Yeah, maybe the analogy isn't all that great, but it does conjure up a cool picture.

          Jumping back a few months on this one, but I think the decision to leave alone the pointer value upon an object delete is solid. If I understand the standard properly, the target of a delete can be an lvalue or and rvalue. So delete 0x34fc3d2200 should be a valid operation, right? How ya gonna change the value of an rvalue (in a traditional sense)?

          1 Reply Last reply
          0
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #40

            Imagine clearing some sort of array:

            for(type* ptr = some_array; something ; ++ptr)
            {
                 delete ptr;
            }
            

            Now imagine delete would zero that pointer. Do you see the problem? You would have to make another, temporary, pointer just so you zero the copy and your original doesn't get changed. In other words you're paying for what you don't use or even want. There's also problem of const pointers or pointers that you got from external APIs that do their own bookkeeping and might actually need that pointer value even after delete. It would create more problems than it solves.

            1 Reply Last reply
            5
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #41
                      int 🥩=1;
                      int 🧀=1;
                      int 🥬=1;
                      int 🍞=1;
                      int 🍅=1;
                      int 🥪=🥩+🥬+🍅+🧀+🥩;
                      cout << 🥪 << endl;
              

              Fails to compile in C++17...

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              1
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #42

                @fcarney said in Recurring C++ and Qt anti-patterns:

                    int 🥩=1;
                    int 🧀=1;
                    int 🥬=1;
                    int 🍞=1;
                    int 🍅=1;
                    int 🥪=🥩+🥬+🍅+🧀+🥩;
                    cout << 🥪 << endl;
                

                Fails to compile in C++17...

                What's this "int" stuff? Doesn't the 17 standard deduce the type based on the rvalue? Not that I think that is necessarily a good thing though.

                fcarneyF 1 Reply Last reply
                0
                • Kent-DorfmanK Kent-Dorfman

                  @fcarney said in Recurring C++ and Qt anti-patterns:

                      int 🥩=1;
                      int 🧀=1;
                      int 🥬=1;
                      int 🍞=1;
                      int 🍅=1;
                      int 🥪=🥩+🥬+🍅+🧀+🥩;
                      cout << 🥪 << endl;
                  

                  Fails to compile in C++17...

                  What's this "int" stuff? Doesn't the 17 standard deduce the type based on the rvalue? Not that I think that is necessarily a good thing though.

                  fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #43

                  @kent-dorfman said in Recurring C++ and Qt anti-patterns:

                  🍞

                  The real problem is this variable is unused.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  1
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #44

                    @fcarney said in Recurring C++ and Qt anti-patterns:

                    The real problem is this variable is unused.

                    So in 17 unused variables are errors instead of warnings?

                    1 Reply Last reply
                    0
                    • fcarneyF Offline
                      fcarneyF Offline
                      fcarney
                      wrote on last edited by
                      #45

                      @kent-dorfman said in Recurring C++ and Qt anti-patterns:

                      So in 17 unused variables are errors instead of warnings?

                      No, its just a bug in the code for a samich.

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      0
                      • Kent-DorfmanK Offline
                        Kent-DorfmanK Offline
                        Kent-Dorfman
                        wrote on last edited by
                        #46

                        @fcarney said in Recurring C++ and Qt anti-patterns:

                        No, its just a bug in the code for a samich.

                        Samich... Are you a yinzer?

                        1 Reply Last reply
                        0
                        • fcarneyF Offline
                          fcarneyF Offline
                          fcarney
                          wrote on last edited by
                          #47

                          @kent-dorfman said in Recurring C++ and Qt anti-patterns:

                          Are you a yinzer?

                          Had to look it up. Based on what I read, no. Not sure where I heard sandwich being called samich though. I am in western USA.

                          C++ is a perfectly valid school of magic.

                          1 Reply Last reply
                          0
                          • Chris KawaC Online
                            Chris KawaC Online
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #48

                            Being an a-hole as a recruiter:

                            What does o() mean?
                            What does o.o mean?
                            What does o->o mean?
                            What does o-->o mean?
                            What does o()--<=>--o() mean? Fun fact - crashes MSVC (yes, the compiler, not the compiled program)
                            What does [](){;o()++<=>++o();}() mean?

                            J.HilkJ 1 Reply Last reply
                            3
                            • Chris KawaC Chris Kawa

                              I had to dig through this thing once, only the real code was like a hundred times longer and more convoluted.

                              // Library.h statically linked to and included in DLL and EXE
                              struct SomeType
                              {
                                 int typeId();
                              };
                              Q_DECLARE_METATYPE(SomeType);
                              
                              // Library.cpp
                              int SomeType::typeId()
                              {
                                  return qMetaTypeId<SomeType>();
                              }
                              
                              // main app
                              SomeType& var1 = getItFromDLL();
                              SomeType& var2 = getItFromEXE();
                              
                              bool same = var1.typeId() == var2.typeId(); // nope
                              

                              Pretty ugly thing to debug, especially since once in blue moon it actually works :/

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on last edited by
                              #49

                              @chris-kawa said in Recurring C++ and Qt anti-patterns:

                              I had to dig through this thing once, only the real code was like a hundred times longer and more convoluted.
                              [Snip]
                              Pretty ugly thing to debug, especially since once in blue moon it actually works :/

                              Indeed. Although, this is windows specific. It works correctly on Linux as the symbol resolution happens at run time.

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              1
                              • Chris KawaC Chris Kawa

                                Being an a-hole as a recruiter:

                                What does o() mean?
                                What does o.o mean?
                                What does o->o mean?
                                What does o-->o mean?
                                What does o()--<=>--o() mean? Fun fact - crashes MSVC (yes, the compiler, not the compiled program)
                                What does [](){;o()++<=>++o();}() mean?

                                J.HilkJ Online
                                J.HilkJ Online
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #50

                                @chris-kawa wow, some of those you don't encounter every day...


                                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
                                0
                                • Chris KawaC Online
                                  Chris KawaC Online
                                  Chris Kawa
                                  Lifetime Qt Champion
                                  wrote on last edited by Chris Kawa
                                  #51

                                  Yeah, it was a bit out of topic. Here's one anti-pattern I encounter something like 9/10 code reviews:

                                  auto widget = new SomeWidget(some_widget);
                                  auto layout = new SomeLayout(some_other_widget);
                                  layout->addWidget(widget);
                                  

                                  It's not a correctness bug. It's a subtle performance one. Compare this with:

                                  auto widget = new SomeWidget();
                                  auto layout = new SomeLayout();
                                  layout->addWidget(widget);
                                  some_other_widget->setLayout(layout);
                                  

                                  If you don't see it - count how many times parents need to be changed and imagine there's not one but, say, 50 widgets and layouts.
                                  For extra sweetness do the same when the parent widget is visible - how many times layouts need to be recalculated?

                                  jsulmJ 1 Reply Last reply
                                  6
                                  • Chris KawaC Chris Kawa

                                    Yeah, it was a bit out of topic. Here's one anti-pattern I encounter something like 9/10 code reviews:

                                    auto widget = new SomeWidget(some_widget);
                                    auto layout = new SomeLayout(some_other_widget);
                                    layout->addWidget(widget);
                                    

                                    It's not a correctness bug. It's a subtle performance one. Compare this with:

                                    auto widget = new SomeWidget();
                                    auto layout = new SomeLayout();
                                    layout->addWidget(widget);
                                    some_other_widget->setLayout(layout);
                                    

                                    If you don't see it - count how many times parents need to be changed and imagine there's not one but, say, 50 widgets and layouts.
                                    For extra sweetness do the same when the parent widget is visible - how many times layouts need to be recalculated?

                                    jsulmJ Online
                                    jsulmJ Online
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #52

                                    @chris-kawa said in Recurring C++ and Qt anti-patterns:

                                    auto widget = new SomeWidget(widget);

                                    I hope this is a typo :-)

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    Chris KawaC 1 Reply Last reply
                                    1
                                    • jsulmJ jsulm

                                      @chris-kawa said in Recurring C++ and Qt anti-patterns:

                                      auto widget = new SomeWidget(widget);

                                      I hope this is a typo :-)

                                      Chris KawaC Online
                                      Chris KawaC Online
                                      Chris Kawa
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #53

                                      @jsulm Sure, sorry, fixed :)

                                      1 Reply Last reply
                                      0
                                      • Kent-DorfmanK Offline
                                        Kent-DorfmanK Offline
                                        Kent-Dorfman
                                        wrote on last edited by
                                        #54

                                        @chris-kawa said in Recurring C++ and Qt anti-patterns:

                                        It's not a correctness bug. It's a subtle performance one. Compare this with:

                                        It took me a few minutes but ok, I'm convinced... LOL

                                        1 Reply Last reply
                                        0
                                        • fcarneyF Offline
                                          fcarneyF Offline
                                          fcarney
                                          wrote on last edited by
                                          #55
                                          mtime.toString(tr("M/d/yyyy hh:mm AP"));
                                          

                                          C++ is a perfectly valid school of magic.

                                          aha_1980A 1 Reply Last reply
                                          2

                                          • Login

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