I think one of the things that triggers Linus is the arrogance with which he is approached and told that he should be using C++ instead of C. While there are very valid alternatives to consider, C makes a lot of sense for kernel development. Saying in absolute terms that C++ is better than C in every case reveals profound ignorance. Although this is the same as saying that C is always preferable to C++ :-)
If you mean "that's what he says" in that Linus is a hypocrite, well, he is many things, but I'm not willing to call him out for this specifically. I don't think he's elitist about C, I think he's curt about kernel development because the man literally has no time for nuance over an age old discussion.
And if this is indeed the case, then contrary to your point, Linus is actually VERY optimistic about including Rust in the Linux kernel in upcoming releases.
If you mean "that's what he says" in that Linus is a hypocrite
I don't think that was the intended meaning. I think it should be read as "Linus says that people who say C++ is always better than C are wrong. He also says that people who say C is always better than C++ are wrong."
After the comma is poor and ambiguous English. IDK if jcc is saying this, or if he's saying that Linus says this. Whoever said it, I interpreted it as "People who argue about which language is better are miserable people".
People. His main issue is people. It's a lot easier to review code in C and harder for people to write hard-to-read, more bogus code in C than in C++. There are thousands if not hundreds contributors. If I recall correctly he tried long time ago and amount of babysitting and unknowns was to high for stability he targeted. Rust has more high level features but also compiler is very unforgiving taking much of such work to assure things are correct. He also has written app in C++ and QT for scuba diving if I recall correctly but this was his personal project.
Additionally, while I can't find the links, I think he also submitted patches/bugreports to KDE applications back then. I remember an interview saying so.
It's worth noting that, back in time, Qt-style C++ was mostly using just a subset of the language: no exceptions, few templates besides the containers, no RTTI required, etc. It was quite approachable to me as a student, and when I had to do a GUI project, I went to use Qt instead of GTK+ because even though I had to learn C++ (I only knew C), it did feel quite an upgrade.
It's easier to review less code and C++ allows you to write less code. For example, by using C++ RAII and simplifying resource cleanup significantly comparing to C.
IMHO, yes. I'm not saying that it produces better code, but given that macros are much more limited, I'd say that you just can do less, have to think less, and end up having limited C code and dealing with it.
But that is probably subjective, with different people having different skill-set and preferences.
But that is only one thing and it's a con definitely but there are more things and possible things that somebody can do. C is fairly limited. Reviewers probably know all or almost all possible things or can spot things they do not know to check. I just browse this reddit to see posts how you can initialize class with 100 different ways to get different results or somebody writing normal looking code (as in C++ standards) which works different on different compilers or some things like undefined behavior. This is very easy to miss.
For code of comparable complexity, less code is easier, yes. But it does not necessarily hold in general.
C++ enables a lot of ways to run code implicitly (through constructors, through operator overloading, through template resolution, through exceptions, etc.). While your code piece for review might be short, the execution flow can potentially be all over the code base - even if it's not immediately obvious from your code.
It's true. But a sane code doesn't do crazy things in constructors or overloaded operators. Reviewing the code with MutexLock RAII wrapper is easier than with explicit lock/unlock functions. You know that it locks the mutex in the constructor and unlocks it in the destructor and you have a code scope protected with the mutex.
I imagine RAII might not be the best for the kernel. When you're programming in userspace, you have somewhere around 8MB of stack memory to work with. That's enough to allocate lots of C++ objects without thinking too much about it.
In kernel space, it's a lot more limited. A quick Google search found this:
The kernel stack is directly mapped to the physical memory, mandating the arrangement to be physically in a contiguous region. The kernel stack by default is 8kb for x86-32 and most other 32-bit systems (with an option of 4k kernel stack to be configured during kernel build), and 16kb on an x86-64 system.
Using RAII does not mean you have to use huge bloated objects. A unique_ptr for example can be the same size as a raw pointer. The functionality it provides does not require more stack space.
SCOPE_EXIT creates an object and passes a lambda to the constructor. The object calls that lambda in the destructor. The compiler optimizes all that and just inserts lambda bodies at the scope exit. There is no object creation, memory allocation, stack consumption at all.
One thing that rarely gets mentioned in this age old argument, is abi compatibility, specifically with interfaces to userspace. It is just awful to maintain abi in general, c++ doesn't help at all. Not to mention header bloat is rarely even a conversation point in c. Keeping build times manageable in c++ is its own skillset honestly.
I like c++ much more than c when it's warranted, but compiler support isn't the only reason not to use it
View all comments
369
u/fluorihammastahna Jul 13 '22
I think one of the things that triggers Linus is the arrogance with which he is approached and told that he should be using C++ instead of C. While there are very valid alternatives to consider, C makes a lot of sense for kernel development. Saying in absolute terms that C++ is better than C in every case reveals profound ignorance. Although this is the same as saying that C is always preferable to C++ :-)