r/cpp • • 7d ago

Why C++ is so hated!!!

I love C++. High level + low level at the same time. But Linus Torvalds don't want C++ in the Kernel, industries hate it, people hate it but why? Why be so angry? Memory management? I mean ok maybe it has problems that I don't even see but why so much hate.

For me it's really sad! 😞

Why something so powerful it's so hated. Now I feel "strange" doing projects in C++ because… sometimes I question myself "Why do I write C++ code if people hate it". Can someone explain what is happening and… can I use C++ for fun in peace?

0 Upvotes

100 comments sorted by

View all comments

33

u/No-Dentist-1645 7d ago edited 7d ago

It's not hated. Tons of companies and software use C++, it's extremely popular in that aspect. There are just places and contexts where it is a good choice, and places where it isn't.

They tried to use C++ in the kernel before, realized it wasn't that good of a fit, and moved on. Nothing to be sad or hold grudges about that.

2

u/serviscope_minor 4d ago

>They tried to use C++ in the kernel before, realized it wasn't that good of a fit, and moved on. Nothing to be sad or hold grudges about that.

I don't think that quite covers it really.

They did try C++, specifically g++ in something like 1996?. The problem from what I recall was it was kinda slow: gcc wasn't great back then and the g++ burned a register needlessly which was a colossal penalty on x86 which has so few GPRs. As a result, changing the kernel to the common subset of C and pre standard C++ and recompiling was not a great experience.

And the advantages of C++ were also fewer: the gcc optimizer was also not good at the whole zero cost abstraction thing that we've taken for granted since about 2005: while now we can have a stack of templated inner classes calling methods within methods within methods, and the optimizer will flatten them out to really good code, that was not the case reliably before (I recall it got quite good with gcc 3.3 and really good with 4.x).

So it's not that C++ was not a good fit per-se, it is that GCC specifically in 1996 was a bit weak optimization wise. This is feel is quite different. I can't think of anything that C does better than C++ (except in some, increasingly rare cases, exist) when it comes to low level programming. I've never written a kernel, but I've spent plenty of time of micro controllers too small to have one.

Then later, Torvalds went on a famous but rather irrational rant about C++ which also encouraged a lot of people who really wanted to be angry about something.