r/cpp_questions • • 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

192 comments sorted by

View all comments

Show parent comments

2

u/tyler1128 Sep 14 '24

And, anyway, C++ abstractions are not properly abstractions, they are almost only syntactic sugar and zero-cost abstractions.

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.

1

u/Nychtelios Sep 14 '24

Yes, sure! Thank you for adding details to my comment!

What I was trying to say is that costly abstractions, like virtual, are not mandatory to use in C++, so it is not inherently less efficient than C. On the other hand, if you need a virtual function you can easily have it and you don't have to implement (and test) your own vtable, like Linux developers keep doing.

3

u/tyler1128 Sep 14 '24

Yeah, C++ isn't inherently less efficient than C and I never tried, at least, to imply that. You can write perfectly performant code at equal with C in C++. Using the convenience features of it also have potential consequences in memory and/or performance efficiency. That is not inherently a bad thing, but in some contexts it does matter. I still would hugely prefer to code in C++ rather than C, but as a programmer it's also important to understand the pros and cons of the language you use, which every language has.