dirs

the c++ iceberg

as an intermediate / beginner c++ dev what i'm about to talk about is absolutely insane. so i stumbled upon a c++ iceberg. an iceberg is a meme format, where whatever's at the top is known to most, but as you go down things get more disturbing and unknown. here is the c++ iceberg:

"0[arr]": that is valid c++ syntax ("index[array]") and works exactly like array[index]. the reason for it is that an array of pointers or vectors is actually just: *(index + array), and the astute among us may see that it's the same as *(array + index) since addition is commutative. so order really doesn't matter at all.

"#define private public": this macro is a reference to "things you should commit before leaving your job". this list contains very bad macros one could destroy source code with. my favorite is "#define continue if (HANDLE h = OpenProcess(PROCESS_TERMINATE, false, rand()) ) { TerminateProcess(h, 0); CloseHandle(h); } break". just redefines continue to be malware, lmao.

"inline does not mean inline": whether you write inline or not, is irrelevant. the compiler doesn't give a shit. it will do it when it thinks it's faster not when you tell it to. however this seems obvious. most "premature-optimizations" are just lies. they don't matter since the compiler would just see your i++ and make it behave like ++i.

"c++ is not a superset of c": i think this one is a bit of a nitpick but yes not everything that runs in c, runs in c++ / there are some differences. My favorite is the restrict keyword, that has no equivalent in c++. To find out more about that: wikipedia and youtube vid

"iosfwd": if you're using a type from iostream in a header, you might be frustrated that you need to include iostream. well you don't, actually. just include iosfwd, which let's the compiler know the types exist. much faster, actual tip here, no bullshit.

"most vexing parse": take the following function, what is it doing? void convert(double x) { int i(int(x)); } // well it might seem like it's making a an integer named i by converting the double x; // but this could also be making a function named i that returns an integer and takes an integer x. // basically this ambiguity is called most vexing parse.

"spaceship operator": the spaceship operator (A <=> B), returns -1 if A is smaller, 1 if B is smaller and 0 if they're equal. It's kinda useful ig, but nothhing really crazy, the most useful not used operators are bitwise operators in my opinion, like XOR (^=) that makes it so you can switch two values without creating a variable in between.

"else if is a lie": basically any other statements such as while, for, etc can be used after an else. i thought this was known but i guess not.

"diagraphs": imagine you're writing with a character set that doesn't have some c++ symbols. How will you be able to code in your most hated programming language? don't worry c++ has your back. instead of writing ^= for XOR equals, you can write xor_eq. For the full list of keywords see cppreference.

"--> operator": this is just (x--) > y = x-->y; "--x>y" however is a much better way to write it; and isn't "++x=x<=++y=y" fun?

"protected abstract virtual base pure virtual private destructor": a reference to a dumbass quote by Tom Cargill. Here's an example of what the fuck he probably meant, since it's made purposefully to make it look like c++ is complicated: class Base { private: virtual ~Base() = 0; }; class Derived : protected virtual Base { private: ~Derived () {.......} };

"std::vector bool is broken": due to an error that dates back to c++98, vector bool uses an optimization with bits and not the standard bytes with vector bool. so it may return proxy objects that let you manipulate those bits. however those proxy objects aren't actual bools and for an alternative without this quirk use deque bool.