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. Hidden features of C++
QtWS25 Last Chance

Hidden features of C++

Scheduled Pinned Locked Moved Unsolved The Lounge
19 Posts 7 Posters 2.3k 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    I find it amazing that hidden gems exist like this in C++.
    Coding for dyslexics:

    #include <QCoreApplication>
    #include <QDebug>
    #include <cstring>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // reverse indexing
        char str[]  = "Hello World";
        char str2[] = "Beano Could";
    
        qInfo() << str;
    
        for(int i=0; i<strlen(str); ++i){
            int i2 = strlen(str) - 1;
            i[str] = (i2-i)[str2];
        }
    
        qInfo() << str;
    
        return a.exec();
    }
    

    C++ is a perfectly valid school of magic.

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      For the curious and lazy like me: http://cpp.sh/6jyb

      Now you have to explain why though

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

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

        I wonder if the operator [] somehow distinguishes which parameter is which when called. I have no idea if it produces the same code when indexing str[i] versus indexing i[str].

        C++ is a perfectly valid school of magic.

        1 Reply Last reply
        0
        • fcarneyF fcarney

          I find it amazing that hidden gems exist like this in C++.
          Coding for dyslexics:

          #include <QCoreApplication>
          #include <QDebug>
          #include <cstring>
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              // reverse indexing
              char str[]  = "Hello World";
              char str2[] = "Beano Could";
          
              qInfo() << str;
          
              for(int i=0; i<strlen(str); ++i){
                  int i2 = strlen(str) - 1;
                  i[str] = (i2-i)[str2];
              }
          
              qInfo() << str;
          
              return a.exec();
          }
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @fcarney said in Hidden features of C++:

          I find it amazing that hidden gems exist like this in C++

          Why do you find it amazing, and what's gemmy about it?

          Have fun with it: https://godbolt.org/z/gPiYxP
          The compiler just treats everything as pointer(s) and does the pointer addition as needed.

          Read and abide by the Qt Code of Conduct

          fcarneyF 1 Reply Last reply
          4
          • kshegunovK kshegunov

            @fcarney said in Hidden features of C++:

            I find it amazing that hidden gems exist like this in C++

            Why do you find it amazing, and what's gemmy about it?

            Have fun with it: https://godbolt.org/z/gPiYxP
            The compiler just treats everything as pointer(s) and does the pointer addition as needed.

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

            @kshegunov said in Hidden features of C++:

            Why do you find it amazing, and what's gemmy about it?

            Because its interesting and fun. It took a bit, but I found this explanation:

            What square brackets really mean
            
            Accessing an element of an array via ptr[3] is actually just short for *(ptr + 3). This can be equivalently written as *(3 + ptr) and therefore as 3[ptr], which turns out to be completely valid code. 
            

            Once its explained it makes complete sense. I was imagining some operator overloading function juxtaposition of terms nonsense.

            C++ is a perfectly valid school of magic.

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

              @kshegunov said in Hidden features of C++:

              https://godbolt.org/z/gPiYxP

              Ooh, that is a cool website. Assembler right in a browser. Thanks!

              C++ is a perfectly valid school of magic.

              kshegunovK 1 Reply Last reply
              1
              • KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by
                #7

                @fcarney that's nothing special, it just simple pointer operation/arithmetic

                @VRonin cool website, didn't know it!

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                1
                • fcarneyF fcarney

                  @kshegunov said in Hidden features of C++:

                  https://godbolt.org/z/gPiYxP

                  Ooh, that is a cool website. Assembler right in a browser. Thanks!

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

                  The guy with the site: https://www.youtube.com/watch?v=bSkpMdDe4g4 (also a very good talk).

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  2
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    If you want to have more fun: https://www.ioccc.org/years.html
                    For example: https://www.ioccc.org/2018/burton1/prog.nowarn.c

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

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

                      Here is another interesting site:
                      http://madebyevan.com/obscure-cpp-features/

                      C++ is a perfectly valid school of magic.

                      1 Reply Last reply
                      0
                      • JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11
                        i[str] = (i2-i)[str2];
                        

                        I'm going to throw this in here, and doubtless wait for others to disagree: I think the compiler should generate a warning on i[str]. Do I realize it's the same as str[i] --- yes, I do. Do I realize it's just the same as *(i + str), which I wouldn't complain about --- yes, I do. Bit I'd still like to see a friendly, 2019, open source compiler warning about i not being indexable....

                        EDIT "I think the compiler should": maybe I meant "I expected the compiler to"....

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

                          @JonB said in Hidden features of C++:

                          doubtless wait for others to disagree

                          Or the obligatory "My <insert language> doesn't do this. That is why it is superior."
                          cough cough rust cough cough... ;)

                          (disclaimer: I have never programmed rust)

                          C++ is a perfectly valid school of magic.

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

                            Now I am kinda scared:

                            qInfo() << 3<:"Foobar"];
                            

                            Outputs 'b'.
                            But this:

                            std::cout << 3<:"Foobar"];
                            

                            Crashes

                            I am really struggling as to how that translates to an index in the first case, but crashes in the second case.

                            C++ is a perfectly valid school of magic.

                            fcarneyF 1 Reply Last reply
                            0
                            • fcarneyF fcarney

                              Now I am kinda scared:

                              qInfo() << 3<:"Foobar"];
                              

                              Outputs 'b'.
                              But this:

                              std::cout << 3<:"Foobar"];
                              

                              Crashes

                              I am really struggling as to how that translates to an index in the first case, but crashes in the second case.

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

                              @fcarney said in Hidden features of C++:

                              std::cout << 3<:"Foobar"];

                              Nevermind, it needs "\n" to output. It was crashing cause I was killing it... doh! I rarely use cout anymore.

                              C++ is a perfectly valid school of magic.

                              fcarneyF 1 Reply Last reply
                              0
                              • fcarneyF fcarney

                                @fcarney said in Hidden features of C++:

                                std::cout << 3<:"Foobar"];

                                Nevermind, it needs "\n" to output. It was crashing cause I was killing it... doh! I rarely use cout anymore.

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

                                Also,
                                digraphs

                                Why? Why?!

                                C++ is a perfectly valid school of magic.

                                kshegunovK 1 Reply Last reply
                                0
                                • fcarneyF fcarney

                                  Also,
                                  digraphs

                                  Why? Why?!

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

                                  @fcarney said in Hidden features of C++:

                                  digraphs

                                  That's some bag-o-worms. My advice - don't dig there.

                                  Read and abide by the Qt Code of Conduct

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

                                    I think the take-away from this discussion is that if you want to write code to really piss off someone else who has to read or maintain it then use C++.

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

                                      I think the take-away from this discussion is that if you want to write code to really piss off someone else who has to read or maintain it then use C++.

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

                                      @Kent-Dorfman
                                      No, C++ readability/maintenance has nothing on this little gem language, which has always been a favorite of mine: https://en.wikipedia.org/wiki/Brainfuck. Here, for example, is Hello World! in its entirety:

                                      ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
                                      

                                      And you can go to https://sange.fi/esoteric/brainfuck/impl/interp/i.html to paste it in and run :)

                                      ODБOïO 1 Reply Last reply
                                      1
                                      • JonBJ JonB

                                        @Kent-Dorfman
                                        No, C++ readability/maintenance has nothing on this little gem language, which has always been a favorite of mine: https://en.wikipedia.org/wiki/Brainfuck. Here, for example, is Hello World! in its entirety:

                                        ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
                                        

                                        And you can go to https://sange.fi/esoteric/brainfuck/impl/interp/i.html to paste it in and run :)

                                        ODБOïO Offline
                                        ODБOïO Offline
                                        ODБOï
                                        wrote on last edited by ODБOï
                                        #19

                                        @JonB there is also Malbolge, but i don't thik this languages are designed to write real programs.

                                        hello world

                                        ('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#"
                                        `CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@>
                                        
                                        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