notes-on-cpp.org (1847B)
1 #+TITLE: Notes on C++ 2 #+DATE: <2024-10-20 Sun 21:50> 3 #+LANGUAGE: en 4 5 This is a list of stuff to remember when programming in this hell of a language called C++. I hate every single second of my life when I'm programming with it. Unfortunatelly, I'm forced to some times. 6 7 By the way, this is mostly about C++98, except when noticed. The sanest version. Yes. Even the sanest version. 8 9 I'll update this rather frequently. 10 11 + Never, ever copy a =std::vector=. They create a new instance of the underlying memory every time you copy them. Always keep only a single instance and pass pointers to it. Yes, this means you have to dynamically allocate them when they're shared with lower stack frames. 12 + Never, ever copy =std::string= also. 13 + Always reserve space in =std::vector=. Never, ever create one without reserving sane space first. They usually grow in dumb always, for example starting at 0 capacity and growing by 50% at each increment, when the sane way would be starting at about 8 and grow by doubling. 14 + Never use =std::endl=. They force a flush onto the underlying stream. A newline will flush a life-buffered output stream anyways, and you definitely don't want to force flush a file output. 15 + Avoid =static= duration objects as their initialization and termination processes happens at a not well-defined time. 16 + Classes only have RTTI when they're virtual, so, if, for some reason, you (May God help you) need typeinfo for a non-polymorphic class, you must use a placeholder USELESS virtual function like =virtual void ImAClownForCodingInThisShittyLanguage()=. 17 + When you have a =std::vector<T>=, passing a =T*= to it's =push_back()= method may compile without errors and fuck you a lot. 18 + If you have a =std::vector<T*>=, calling it's =clear()= won't delete the memory of the objects the vector holds, tho it'll call it's destructors.