Hidden features of C++
-
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(); }
-
For the curious and lazy like me: http://cpp.sh/6jyb
Now you have to explain why though
-
@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. -
@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.
-
@kshegunov said in Hidden features of C++:
Ooh, that is a cool website. Assembler right in a browser. Thanks!
-
The guy with the site: https://www.youtube.com/watch?v=bSkpMdDe4g4 (also a very good talk).
-
If you want to have more fun: https://www.ioccc.org/years.html
For example: https://www.ioccc.org/2018/burton1/prog.nowarn.c -
Here is another interesting site:
http://madebyevan.com/obscure-cpp-features/ -
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 asstr[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 abouti
not being indexable....EDIT "I think the compiler should": maybe I meant "I expected the compiler to"....
-
@fcarney said in Hidden features of C++:
digraphs
That's some bag-o-worms. My advice - don't dig there.
-
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++.
-
@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 :)
-
@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@>