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 72.1k 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.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #66

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

    std::vector<T>::iterator end2(); // error, needs typename

    Yeah, this being an error (inside a template definition) really bugs me as well.

    I'm sure there is a perfectly good brainiac reason it barfs, but I could really see myself wanting to do something like this, as "end2" instead of end2()

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

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

      std::vector<T>::iterator end2(); // error, needs typename

      Yeah, this being an error (inside a template definition) really bugs me as well.

      I'm sure there is a perfectly good brainiac reason it barfs, but I could really see myself wanting to do something like this, as "end2" instead of end2()

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

      @Kent-Dorfman
      Apparently its a "dependent name":
      https://en.cppreference.com/w/cpp/language/dependent_name

      I have not taken time to understand it, but there is the "reason".

      C++ is a perfectly valid school of magic.

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

        I think I may need to stop coding in the evening. I ran into a weird bug that I cannot duplicate today:

        #include <vector>
        
        template<class T>
        class SomeObject
        {
            using Storage = std::vector<T>;
        public:
            SomeObject(size_t len){
                m_data.resize(len);
            }
        
            size_t getSize(){
                return m_data.size();
            }
        
        private:
            Storage m_data;
        };
        
        class UsesSomeObject
        {
        public:
            UsesSomeObject()
                : m_someval(0)
                , m_somedata(128) // if not initialized the whole object was spitting out weird data
            {
        
            }
        
        private:
            int m_someval;
            SomeObject<int> m_somedata;
        };
        

        I don't know if this had anything to do with templates or not. I was working with one at the time. There is a comment in the above code about not initializing m_somedata. I didn't have a default constructor or maybe it created one for me (not sure). Accessing the vector internal to the class had all sorts of "interesting" behavior. Then when I realized my error everything started working fine. It was just a very sneaky issues. However, on my compiler at work it is not letting me compile this. So I am not sure of the situation where it would let me compile this. Maybe if it creates its own default constructor. The lesson is make sure everything is getting initialized before using them!

        I will check tonight to see if I can simplify the actual condition that caused this. It was quite interesting and the errors didn't match the source of the problem.

        C++ is a perfectly valid school of magic.

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

          Not an antipattern, just disappointing. I cannot do this:

          std::vector<float&> frefs; 
          

          I know why. I know you can use std::reference_wrapper, but it is kinda messy to me.

          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
            #70

            I guess I don't have a problem with it because in the cases where I might ever consider such an abomination there are always pointers...yes, always pointers.

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

              I guess I don't have a problem with it because in the cases where I might ever consider such an abomination there are always pointers...yes, always pointers.

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

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

              always pointers

              We should start an anti-safe coding movement (I say this with disdain for idea of safety, there is nothing safe about systems level coding IMO, or coding in general) . The slogan would be "Always Pointers".

              C++ is a perfectly valid school of magic.

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

                I hate to break this to you guys but pointers and references are the same thing. References are just syntax constraint, something like const, so disappointment in this case would be like disappointment that you can't assign to a const value. IMO using std::reference_wrapper because you don't like those naked stars is just silly.
                As for "Always Pointers" - why so extreme? How about more mellow party like "pointers where they make sense"?

                kshegunovK 1 Reply Last reply
                2
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #73

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

                  pointers where they make sense

                  That is the point (hehe) its an extremist group.

                  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 Kent-Dorfman
                    #74

                    I tend to think of myself as a "moderate extremist": on the surface all agile, type-safe, and scope limiting...but in private I do stuff like macro-ize bitshift operations to save typing. My infatuation with pointers goes toward edumacating the noobs when they try to do large matrix processing using array indexes. It's like "hold my beer while I whack this kid"...and then I say "don't do that!"

                    1 Reply Last reply
                    3
                    • Chris KawaC Chris Kawa

                      I hate to break this to you guys but pointers and references are the same thing. References are just syntax constraint, something like const, so disappointment in this case would be like disappointment that you can't assign to a const value. IMO using std::reference_wrapper because you don't like those naked stars is just silly.
                      As for "Always Pointers" - why so extreme? How about more mellow party like "pointers where they make sense"?

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

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

                      IMO using std::reference_wrapper because you don't like those naked stars is just silly.

                      While I agree with you, it's rather funny (and somewhat ironic) such a class does exist.
                      The chant "pointers are bad", and even the more extreme "naked pointers are even badder" seems to have crept so ubiquitously into the way code's written (even moved past a fad I'd say) that we need a wrapper object to make assignable something which was designed into the language not to be, instead of simply passing by address ... strange world we live in ...

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      3
                      • B Offline
                        B Offline
                        Bur8rus
                        Banned
                        wrote on last edited by
                        #76

                        One example is using exceptions for control flow or as simply another way to return a value from a function.

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

                          @Bur8rus but...the C++ God himself himself wrote in the sacred texts that exceptions should be looked at as just another flow control route, and to not make any judgements other than that.

                          While in principle I agree with you, I'm glad my hands are not tied to keep me from committing an abomination like generic flow using exceptions.

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

                            Heh, bad flow control discussions made me think of one phrase:
                            goto hell ;-)

                            C++ is a perfectly valid school of magic.

                            Chris KawaC 1 Reply Last reply
                            1
                            • fcarneyF fcarney

                              Heh, bad flow control discussions made me think of one phrase:
                              goto hell ;-)

                              Chris KawaC Offline
                              Chris KawaC Offline
                              Chris Kawa
                              Lifetime Qt Champion
                              wrote on last edited by
                              #79

                              @fcarney said:

                              goto hell ;-)

                              I guess this calls for a classic:

                              int up;
                              throw up;
                              
                              1 Reply Last reply
                              2
                              • Kent-DorfmanK Offline
                                Kent-DorfmanK Offline
                                Kent-Dorfman
                                wrote on last edited by
                                #80

                                or

                                If if THEN then
                                

                                There once was this home computer language called BASIC, done as a ROM interpretor, that was supported on meager 4KB RAM 8-bit machines. If you wanted to do something quickly then you stuffed your machine code into a preallocated string variable, and then you did a function call like rv=SYS(code$) to execute the machine code directly.

                                artwawA 1 Reply Last reply
                                0
                                • B Bur8rus

                                  One example is using exceptions for control flow or as simply another way to return a value from a function.

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

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

                                  One example is using exceptions for control flow or as simply another way to return a value from a function.

                                  Depressingly, for those of us forced to use Python (with Qt) we are encouraged to use exceptions for flow handling etc. Under the mantra that Python exceptions are different from/more lightweight than C++ exceptions....

                                  kshegunovK 1 Reply Last reply
                                  0
                                  • JonBJ JonB

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

                                    One example is using exceptions for control flow or as simply another way to return a value from a function.

                                    Depressingly, for those of us forced to use Python (with Qt) we are encouraged to use exceptions for flow handling etc. Under the mantra that Python exceptions are different from/more lightweight than C++ exceptions....

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

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

                                    Depressingly, for those of us forced to use Python (with Qt) we are encouraged to use exceptions for flow handling etc. Under the mantra that Python exceptions are different from/more lightweight than C++ exceptions....

                                    They're not, obviously. However in all fairness exceptions have their place, just not in the way they're abused. I use them for quick escape through the stack in numerical code, where it'd be borderline stupid to sprinkle std::optional with if-ery everywhere to handle an error deep in the code. As everything, though, one should apply the very old and tested "common sense" ...

                                    Read and abide by the Qt Code of Conduct

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

                                      or

                                      If if THEN then
                                      

                                      There once was this home computer language called BASIC, done as a ROM interpretor, that was supported on meager 4KB RAM 8-bit machines. If you wanted to do something quickly then you stuffed your machine code into a preallocated string variable, and then you did a function call like rv=SYS(code$) to execute the machine code directly.

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on last edited by
                                      #83

                                      @Kent-Dorfman I still have C64 (my first computer bought in 1988) and I still use Basic for the fun of it :)

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      1 Reply Last reply
                                      1
                                      • kshegunovK kshegunov

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

                                        Depressingly, for those of us forced to use Python (with Qt) we are encouraged to use exceptions for flow handling etc. Under the mantra that Python exceptions are different from/more lightweight than C++ exceptions....

                                        They're not, obviously. However in all fairness exceptions have their place, just not in the way they're abused. I use them for quick escape through the stack in numerical code, where it'd be borderline stupid to sprinkle std::optional with if-ery everywhere to handle an error deep in the code. As everything, though, one should apply the very old and tested "common sense" ...

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

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

                                        They're not, obviously.

                                        I take it that is against my "Python exceptions are different from/more lightweight than C++ exceptions"(?) Well, apparently, this is true. I could (if required) give several references. I guess we cannot assume that a Python exception maps to a C++/OS/native exception....

                                        kshegunovK 1 Reply Last reply
                                        0
                                        • JonBJ JonB

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

                                          They're not, obviously.

                                          I take it that is against my "Python exceptions are different from/more lightweight than C++ exceptions"(?) Well, apparently, this is true. I could (if required) give several references. I guess we cannot assume that a Python exception maps to a C++/OS/native exception....

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

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

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

                                          They're not, obviously.

                                          I take it that is against my "Python exceptions are different from/more lightweight than C++ exceptions"(?)

                                          Yes, that's correct. More so I was referring to the "lightweight"-ness, not so much about them being different. I could agree if you can show a VM's exception to be lighter than a hardware's stack unwinding, but that's rather dubious. Basically that'd be like saying that Python's exceptions are faster than a C longjmp, arguable at best.

                                          Well, apparently, this is true. I could (if required) give several references.

                                          Eh, fine, amaze me.

                                          I guess we cannot assume that a Python exception maps to a C++/OS/native exception....

                                          Nor had I done that. The underlying tech is different to jump to such a conclusion.

                                          Read and abide by the Qt Code of Conduct

                                          JonBJ 1 Reply Last reply
                                          0

                                          • Login

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