r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

308 Upvotes

436 comments sorted by

View all comments

26

u/MrRubberDucky Jul 13 '22

56

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Jul 13 '22 edited Jul 13 '22

He seems to be lacking a lot of knowledge about about C++ - at least in those days. And he also doesn't seem to care. And that's just bad because even a little bit of C++ without even touching the stuff he obviously is so scared of would improve kernel code a lot.

When I wrote my first Windows NT 4.0 kernel device driver in the mid 90s, I happily used C++ there because of the power of abstractions. And it didn't hurt!

7

u/tesfabpel Jul 13 '22

Wikipedia says that the core part of the kernel is written in C and Assembly (just like Linux), while the graphics subsystem is written in C++.

Anyway, just quickly thinking about it, it makes at least some sense NOT to use C++ for a critical part like the kernel since it has many footguns that you need to always make attention to: for example, when you define a class or struct you have to be careful with unary constructors, copy / move constructors and assignments operators, copies are always behind the corner (but they can be optimized by the compiler).
I think those are things that are important in a kernel and the copy-by-default semantic of C++ alongside with the copy constructor / assignment op (instead of the move-by-default in Rust or the copy-by-default of PODs in C without any copy constructor / assignment op) may create some "difficulties".

16

u/no-sig-available Jul 13 '22

Wikipedia says that the core part of the kernel is written in C and Assembly (just like Linux), while the graphics subsystem is written in C++.

The Windoiws NT kernel was initially designed around 1990. The options were totally different then.

many footguns that you need to always make attention to:

When you write an OS kernel, we assume that you are careful and know what you are doing. :-)

Copying a POD in C++ is exactly the same as copy a POD in C. Adding member functions to the struct, instead of manually passing a pointer as the first parameter, also makes no difference (except possibly for improved readability).

If you want to make an object move-only, in C++ you can delete the copy constructor and copy assignment. In C you cannot.

-------------

Linus was obviously shown some low quality C++ code once, and decided that all C++ code is bad forever. And with that attitude, people who know how to write good C++ have not bothered to show him any of their code. So he has proven himself right.

→ More replies (3)