r/cpp_questions • u/kyan100 • Sep 13 '24
OPEN Why Linux community hates C++ so much?
It seems like they have an extreme disliking towards C++. Especially the kernel developers. Linus has even said he doesn't allow C++ in kernel just to keep C++ programmers away. Which sounds very weird because C++ seem to be used in all kinds of complicated systems, and they are fine.
175
Upvotes
2
u/tyler1128 Sep 14 '24
That's what abstractions generally are in native languages. Not always zero cost nor are C++'s all, but they often at least can be next to zero-cost. When we talk about virtual in C++, it's usually not zero-cost, it requires a relative address function call which takes a lot longer for any ISA to perform. There's a reason C++ doesn't virtualize every class or call.
If you want specifics, a virtual call requires two things - a pointer table added to the class, and then the instructions that looks up the pointer from memory, and do a relative jump to that place. A non-virtual function has the ability to either be inlined, meaning recreated in place where it is used, or called by a known address without memory access. Memory access is slow, and even if the address is cached or in a register, indirect jumping ruins a lot of optimizations.