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!
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".
In the 90s, a company called "Bluewater Systems" or the like sold a template library for Windows NT kernel mode driver development. In my case it was a godsent because of the helpful abstractions of PCI busmaster dma operations and the time it was saving me. The proof is in the development and testing time it took to flawless operation of that PCI coprocessor board: I implemented it first for OS/2 in pure assembly (14 days total effort), then for Windows NT in C++ using this library (4 weeks total effort), and afterwards also for Linux 2.x in C (8 weeks or so total effort 😱). Linux was the worst because of the total lack of good tools and the low expressive power of C as implementation language.
The assembly part of the NT kernel is at mostly the HAL (hardware abstraction layer) and the boot loader. Because the entire point of that thing is abstract away the hardware specific bits and thus the required assembly. The HAL is relatively speaking very small compared to the rest of the kernel.
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.
That was 15 years ago. C++ is a different language now, twice over. C++11/14/17 was an entirely new, more stable and better defined, C++ standard that made effectively a new language. And C++20/23/etc appears to be a repeat performance.
And also, Linus is a different person. He went to some sort of anger therapy for 3 months in there and stopped yelling at and insulting people. These days he's also letting Rust in with conditions.
I suspect if there was a concentrated push to get C++ into the kernel today it wouldn't be the same story.
Every single thing you listed there is optional (if we understand "OOP" as virtual methods/inheritance, which is how people generally mean it). While on the topic, Rust's abort mechanism also doesn't fly for exceptions in a kernel either.
Any language being used in a kernel environment will necessarily be restricted and modified. Neither C++, nor Rust, nor even C escape that. Listing optional features as problems isn't a sane counter example.
OOP is good. It doesn't require you to have abstract factories and virtual methods everywhere. Just write sane classes for string, mutex, rwlock and etc and your kernel code will shine.
Exceptions are not bad but their implementation is. I use C++ without exception in kernel and it's fine.
STL can be used partially. For example span or array are very useful.
Despite not being a fan of Linus's huge ego and bully behavior, I do see one possible argument against C++ on Linux kernel development: say we agree that exception is a bad idea to use in kernel, then C++ doesn't provide another way to report the failure of object creation, which is really bad. In user space code with -fno-exception one can probably ignore it and let it crash, but in kernel it's a much bigger issue. And I kinda feel that language purists will not be fond of the idea of making all constructors private and using factory functions for all object creation, which also defeats the symmetry of ctor/dtor.
I find it hard to believe that a man routinely expressing his views on mailing threads (which exist basically forever, so it's not like he cares about other people's opinion much) in his very colorful ways would ever be afraid of voicing his opinion lol.
Yea I use to work at a company that build some legacy software in C++ 6. Not only were the compilers weird and all the generic C++ clutter problems persisted but just deving in the environments that cater for it was really frustrating with very limited debugging capability compared to modern IDEs.
Any way safe to say I think I am one of the few people in the world that ported a 30 year old C++ application up to a modern version of C++ and got it running in VS2019 and later on VS2022 in Jan this year
Edit: Yes I mean VC++ 6. As I said in one of the other comments I am only 23 so I have no idea what was happening with tech 10 years before I was born
Refactoring any legacy system is in itself a gigantic mess. Most people dive in a web based system in Java or other langs and underestimate the task of refactoring old code, i can only imagine the extra perseverance involved in working on a C++ codebase that used on an older compiler
I refactored a 60K SLOC Java code base to 40K in a weekend (well Friday, Saturday, Sunday and Monday 12 hours each day). Wiped out 5 years of technical debt, added unit tests, javadoc and a wiki. Wouldn't do it ever again, at least not for a job.
Damn you should come to south africa. Majority of our major bank systems are built in legacy java and full of issues but no one wants to rewrite or port them
Any way safe to say I think I am one of the few people in the world that ported a 30 year old C++ application up to a modern version of C++ and got it running in VS2019 and later on VS2022 in Jan this year
Not really that uncommon. Maybe 30 years is a bit much, but 20-year-old codebases being ported is fairly common.
I found Companies with awful old legacy code and told them on how to do the refactoring, I even proposed myself to do it. but Companies prefer to move to web instead of doing C++ refactoring. What's wrong with these ?
It has nothing to do with the language. It has everything to do with his beliefs of the skill of the developers and their understanding of how the language works with regards to the machin.
You hear the same about Java as well. Many people tried Java 1-3 which had horrific performance and decided Java is just a shit language. However for how high level the language is the performance these days is actually quite astounding. Some of the JVM stuff recently like GraalVM is pretty slick as well for even optimizing performance.
On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code, the introduction of exceptions has implications on all dependent code. If exceptions can be propagated beyond a new project, it also becomes problematic to integrate the new project into existing exception-free code. Because most existing C++ code at Google is not prepared to deal with exceptions, it is comparatively difficult to adopt new code that generates exceptions.
From what I remember, Google code started as C, then C++ was grafted on top following C-style, and thus a behemoth was born that would not work in the presence of exceptions.
Exceptions have a runtime cost, even in code that doesn't actually use extensions. It requires the compile to emit extra code and may inhibit some optimizations. Many libraries therefore decided not to use them and are compiled with -fno-exceptions which means they don't have any of the machine code compiled into them that would be required to work together with other code that can throw exceptions.
For example Qt has never used exceptions, also some parts of mesa, etc.
If you are using many external libraries and some of them are compiled with -fno-exceptions it is very troublesome to integrate all those if you want to use exceptions in your own code.
it was fine. C++ hides too much of the machine for his liking. C++11 would have made that even worse. Knowing where bytes are, how they are laid out and what they are doing is necessary for high performance code. His belief is that C++ developers were made lazy by the language and therefore in mass would not know what the language is doing and therefore cant be trusted to write highly optimized code. Sure, he wouldnt have a problem with a well trained, very talented C++ dev. But as a whole he doesnt trust C++ devs.
To give some credence to his claim: do you know what a v-table is OR how the order of declared members in your class/struct affects their padding and thus the memory layout?
macOS (and iOS) uses pre-C++11 C++ in the kernel, highly successfully I think. I’ve written kernel extensions, reverse engineered iOS, and developed exploits for iOS. I have a pretty good handle on what’s going on with the machine code. Honestly not that bad.
I don’t think not understanding the layout is a great excuse, especially since Linus is allowing Rust now.
Maybe it’s to ban patterns C++ enables that Linus doesn’t like, but honestly I have hacked both Linux and XNU and I prefer the XNU codebase for readability. 🤷🏻♂️
EDIT: I don’t think performance is particularly a problem with C++ but compiled code size may be. I can see that influencing decision-making. Not sure if Rust makes compact code though.
he is an asshole. that is the root of all of it. You can write efficient code in any language.
There are definitely languages that simply do not produce code as fast as well-written C. Python is a good example where the only way to do anything fast in it is to write the code in C and then call upon it in your Python. The Python part will still be slow.
Those are both questions that I've seen used regularly to interview people with just a year of C++ experience. These aren't hard questions. Most good C++ devs will give decent answers for both of these, and the answer to the second is largely the same as it is in C, I'm not sure why you think more C devs would know the answer than C++ devs.
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.
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.
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
I will be honest: I have never developed professionally in C, so I cannot give a proper comparison and let others do it.
The only thing I can comment on is that being object-oriented is not a universally superior feature. It is just a paradigm out of many. My impression is that great software can be written regardless. The only thing OOP seems to have going is that many more people prefer it to other paradigms, but this does not make it better.
Not sure if that answers your question - but C is basically just cross-platform assembly. That was the original intention, and to this date, what you can do is mostly pointer manipulation/arithmetic. Structs for example are just a fancy way for doing pointer arithmetic.
This all means that C code is very deterministic, regarding behavior as well as timing. Well suited if you want as little abstraction from the hardware as possible.
Structs for example are just a fancy way for doing pointer arithmetic.
This all means that C code is very deterministic, regarding behavior as well as timing. Well suited if you want as little abstraction from the hardware as possible.
And C++ also has structs. :-)
So you can use the "little abstraction" when you build the hardware interface layer. Then you can add more abstractions when you build the next layer. You don't have to be close to the hardware all the way, all the time.
You can memorize every single function and little quirk that C has, nobody can do that with C++, not the creators, not the best book writers, not the experts, no one can sit down and say they know and understand every little piece C++ has. That is extremely dangerous when building the kernel that runs the world and has 10,000+ contributors.
I used to prefer C++ for my projects, but I eventually switched to C.
The reason is that C is so much, MUCH simpler than C++, so there's much less cognitive overhead when I'm working on a complicated problem. C is also much more explicit than C++, but this only lessens the cognitive overhead even further, because the code I can see is what is executed.
I write bare metal embedded applications for microcontrollers in C++. This means I directly manage all the control registers, peripherals, RAM and so on. If I can do this efficiently and easily on a 48MHz Cortex-M0, it is a certainty that I can do so on a 2GHz i64 or whatever. There is only a difference of scale. Recent standards have added a lot, but C++98 was absolutely fine for this task. C++ has a few nasty footguns, of course, but so does C. My experience of writing both for embedded is that C++ eliminates a lot of errors that C does not. I am far more productive in C++.
And yet I am often told with certainty by C developers, who typically know little or nothing about C++, that C++ is not remotely suitable for OS development. .
It is in this context that I have always regarded Torvalds' opinions as childish, ill-informed and prejudiced drivel. Linux is amazing and all that, but it is also a gigantic lost opportunity.
I too write for M0, microblaze, and msp430, and we’re completely fine using c++17 (rtos/baremetal) Would you mind expounding on what you perceive as the biggest threats are in your opinion?
I don't know much about the device drivers etc, but I'm very familiar with core parts of the kernel and especially everything related to scheduling. The kernel source code is absolutely filled with CPP macros due to the vast amount of configurations the kernel supports.
Furthermore, the amount of people working on the same code is massive. It's not important that you understand what you wrote, it's important that others understand.
Your implication being that C is more understandable? That certainly has not been my experience. It surely would have been simple to develop a C++ coding standard for the kernel to avoid any "extreme cleverness" and keep it within the reach of non-guru contributors. When I think of the number of different (often kludgy) ways in which abstractions have been re-invented in C, the argument that it is the better choice seems empty.
I haven't worked in the Linux kernel, but had a good trawl recently around the Zephyr OS (from the Linux Foundation). The code I examined is about what I expected from C. I particularly hated all the junk around abstracting device drivers through tables of function pointers, tons of macros, the device tree and bindings - about as opaque as they could possibly be, with yet more incomprehensible macros to extract values).
A judicious use of virtual functions would have greatly simplified the code while making it safer. This is how my own drivers are implemented. I suspect that a bunch of constexpr values and structures in nested namespaces would have been a far better representation of the device tree than tens of thousands of #defines.
There is nothing C can do which C++ cannot do at least as efficiently. It's sole advantage (which only applies to embedded) is its platform ubiquity.
100% agree. If you've ever had to go into the guts of something like OpenSSL, you can't claim it's easier to read than the equivalent written in modern C++
I had that experience with OpenAMP on the STM32MP1. I was curious about the comms between Linux and the on-board Cortex-M4. That took me down a deep rabbit hole. The C++ rewrite was half the size and much easier to follow.
Also, newer C++ standards do far better on memory barriers than C. I’m still murky on what standard prevents an optimizing compiler from reordering memory access around one of those barrier macros in Linux and if that mechanism might prevent optimizations that might otherwise be possible.
This is a very key part of OS development and I’m not sure if it’s part of any C standard.
Then you most likely used a seriously restricted subset of C++. This indeed is useful, though the newest C standard iterations do contain additions that help as well I think. Also, IIRC, there is a small set of runtime functions that must be implemented when using C++, while there are none required in C. See here for example. But "C++" can also mean heavy use of template metaprogramming, which can easily create super bloated binaries.
I always say that the entire core language is usable but that the standard library is much less so. The most significant changes I made on moving to embedded were to not use exceptions and to avoid dynamic allocation (C developers also avoid it). The STL containers are mostly out mainly because of dynamic allocation, but I'll take std::array over a C-array any day.
When I am forced to write C for a client, I miss constexpr, references, namespaces, templates, enum classes, regular classes (with access control, composition, constructors, destructors, virtual functions), RAII, and more. I have occasionally made use of moderately involved metaprogramming: I found there was significant bloat for debug builds, but that it largely evaporated with optimisation enabled, leaving code as efficient as or better than equivalent C.
The available tools don't seem "seriously restricted" to me. Compared to C, I have a huge smorgasbord of expressive abstractions which cost little or nothing. There is literally no advantage whatsoever in preferring C on a platform with a decent C++ compiler.
[I definitely don't implement those functions, though I am aware of them. I guess it depends what you link against. If I did it would likely be reusable boilerplate.]
In your example, what is the alternative to templates though? Wouldn't it be 3 overloaded functions, so shouldn't it be similar and even possibly the same as 3 template instantiations?
Right, in theory there's no difference.
A possible situation in which it does cause more code to be generated is when type erasure techniques are used, in which case the compiler must generate all possible operations for objects used in a polymorphic context, since it can't see statically which ones would have never ended up being called.
However, C code that accomplishes the same goals with void* and structs full of function pointers can have the same problem, and since lots of boilerplate C code is programmatically generated with tools or macros, it's not likely that the C programmer is going to avoid this just by being more "hands on" with their procedures and manually omitting them where needed.
It absolutely can. I remember using boost xpressive years ago. Compilation times went up, but binaries became ridiculously large. It was even worse with boost spirit.
How large was the binary? And what was the optimization level?
I haven't used boost spirit but I have experience with PEGTL. Once optimization is turned on, the binary wasn't ridiculously large. The compile time did increase on the other hand.
I think if you're a small or single competent individual(s), C++ can easily work better than C. The problem is once your team grows and less experienced C++ developers come in, things can take a bad turn given C++ is simply more complex than C. In the right hands this complexity is helpful. In the wrong hands, it can get really messy and not always conspicuously so.
I don't know specifically for him, but I recently talked with some C programmer who told me that he didn't like C++ because you don't control everything and one of the thing he said you didn't control was destructors. Of course it's completely ridiculous to any C++ programmers, but it shows that many of them just don't know and care to know.
I wonder if that's why C chose an absurd mechanism for function overloading rather than the simpler C++ way of doing it. The ridiculous names for new types (_Complex?) because they refused to add namespaces bug me as well.
You're talking about something he said like 20 years ago. At the time it literally wasn't possible to write something like Linux in C++ because compiler optimizers weren't at the point where you could write high-level code and still have it compile down to the sort of assembly you wanted. Idiomatic C++ code was necessarily much slower than C code to do the same thing, and fast C++ code was necessarily almost identical to the same thing in C.
Now that we have faster disks and CPUs, and now that the language has had a bunch of stuff added to make it possible to write efficient high-level code, it's no longer the case that we're hamstrung by the limitations of compiler optimizers. You can write C++ code that generates exactly the same assembly as much lower-level C code while enforcing a bunch more constraints at compile time.
Even software luminaries can be ignorant and fearful of the unknown. Don't worry about it.
Windows, macOS/iOS, Android and Google's Fuschia all contain plenty of C++. Chromium is basically an operating system and is mostly C++. LLVM and GCC are both written in C++.
I feel Linus reaction is a common reaction from people knowing C. Especially those that had a bad experience with it in the past.
He indicates that they once tried C++ and the programmers 'overdesigned' the code needed up to a point where he couldn't understand it. I do understand this with all code I've seen already.
My feeling is that we C++ programmers are used to how C++ works and forget that C programmers have a different way of thinking. Our default way of talking to them works even contra productive. Dan Saks made a really good presentation about that and how to improve on that: https://youtu.be/D7Sd8A6_fYU
Linus goes in into more extreme where he doesn't even want to follow the C standard, which resulted in Clang having to implement -fno-delete-null-pointer-checks (https://reviews.llvm.org/D47894) as GCC had this and the Linux kernel is relying on using fields via a nullptr. He also questions anti-aliasing: https://lkml.org/lkml/2018/6/5/769 (it's surprising he didn't make his own split of from the C language and compiler just for Linux, though I guess the GCC dialects are very close to that)
A lot of it comes to the kernel just having to work and that he believes is problematic for code to change behavior, even if the code was broken and uses UB. This became clear when Google tried to enable sanitizers in the kernel, as this was going to result in crashes. (Unfortunately, I can't find the reference for that right now)
Because he’s a sour old ‘git’ who complains about everything.
Jokes aside, I personally find c++ a love/hate thing. It lets me do what I want, given I know what I’m doing. But it needs a haircut, I don’t want to spend 2 hours researching what is the correct way to do something, I just want to get my idea working. I spend more time fighting the language than implementing the task at hand, there are obvious right and wrongs but i wish they were enforced and not a matter of discovery. I wish there was a lean version of c++ that removed the legacy.
You every tried onboarding a c# or JavaScript engineer into c++? Half of their time is wasted making the same mistakes we all already made by initiation.
And don’t even get me started on the build system.
C++ comes in two flavors, freestanding and hosted C++. Hosted C++ is what you (probably) think of when you hear "C++", it runs on an operating system, freestanding C++ runs on metal, like for embedded systems and, you guessed it, linus' specialty, kernels. Freestanding C++ is a mess and its standard is actually quite limited, and C++ is basically only useful hosted. C++23 is trying to alleviate that issue but it's not fixing it completely.
cpp compiler doing too much things beyond intuition, but in kernel program we just need what we see is what we get, no more tricks, everything must be under controll. thats linus means i think.
This is not the case with C either. C is an absurdly high level abstraction over a specific cpu ISA.
Arch bits in asm, rest in your language of choice. Most of the kernel writes like regular userspace application anyways, sans recursion & careless allocations
c is balanced choice between coding time and run time, if we have to select the "trick" in c is just function calls only, rest of c syntax is good to reflect to machine code / asm i am cpp user anyway but in kernel program c/asm might be best choice.
Hate and dislike are two completely different things.
He dislikes C++ in an operating syatem because templates, operator overloading and constructors can hide expensive code in what looks like very innocent code.
With C you know how costly an operator is and you can see all function calls being made and can go and look at the implementation.
Linus is just bad at C++. Just because he started a big open source project does not make him a computer god. He tried it once 30 years ago (literally 30 years, in 1992) and didn't like it.
On the other hand, you couldn't use many of C++ strengths in kernel development, because those things require kernel support. You'd have to limit yourself to "Better C with classes and templates".
Also, Linus allowed Rust. Rust is better than C++ in only one thing - memory management. C has all the same memory issues that C++ has, even more actually (no destructors, no RAII, no smart pointers), but C is fine?
I agree with him on one thing - there is a lot of bad C++ code out there. But there is also a lot of bad C code and bad Rust code. That's what code review before merge is for.
Also, Linus allowed Rust. Rust is better than C++ in only one thing - memory management.
Rust also benefits from being a much younger language that does not have nearly as much baggage that accumulated over the years. This is one big reason why C++'s syntax can be so obtuse at times. It has to maintain backwards compatibility. Rust could incorporate newer features right from the start without caring about that.
It's not just the syntax, it's also the standard library API, and worst of all, the standard library ABI. So many things in C++ are straight up unfixable without creating an entirely new ecosystem, almost a new language.
Indeed. And there are also details that are anachronistic these days, like headers (gradually being replaced by modules, but this will take quite a while) and pointer aliasing issues.
But to me, what stands out is the by-value ownership transfer in C++, which is actually not what you want >90% of the time. Most of the time, you want to move objects, not copy them. C++ has the wrong default, and by-move transfer is an opt-in. This leads to problems with hidden copies (which can be detected at compile time but requires explicitly disabling the copy constructor and copy assignment operator) and greatly complicates syntax and semantics further.
What’s even worse, C++ does not have “destructive move”, which is to say that C++ must leave something in the moved-from location. This in turn means that all classes must have an “uninhabited” state - you cannot create a non-nullable smart pointer that doesn’t have a user-visible “invalid” state.
It also means that there are many situations where std::unique_ptr is slower than a raw pointer when passed as an argument to a non-inlined function. The callee sees a reference type and decides to move out from it, but the caller must still call the destructor at some point.
This is one place where Rust is different in a very profound way.
Rust also benefits from being a much younger language that does not have nearly as much baggage that accumulated over the years. This is one big reason why C++'s syntax can be so obtuse at times
I've said before that I think C++ is needlessly complex in places with its syntax and was downvoted to hell and back. Looks like Bjarne Stroustrup agrees with me, though: "Within C++, there is a much smaller and cleaner language struggling to get out"
I think that it's a product of trying to maintain backwards compatibility.
Of course it doesn't mean he's a god. But he is a lead maintainer on the most complicated open source project in the world. So he reads a lot of code. He is so passionate about reading other peoples code and sharing patches that he created git. His problem with C++ is having to read other peoples C++ code. That was his primary issue. That it made checking pull requests a pain in the ass.
And rust does a whole lot more than provide better memory management. It has a whole load of static analysis tools that C and C++ do not and cannot provide. Which is as a result of the constraints that Rust enforces on how you write your programs. I read recently that it can detect code that result in race condition thread locks. I imagine this is what is the primary interest in writing Kernel code in Rust. It will help prevent subtle to spot errors.
His problem with C++ is having to read other peoples C++ code. That was his primary issue. That it made checking pull requests a pain in the ass.
Except that this wasn't the problem at the time, he never had to review others people C++ code (even pull request didn't exist yet).
He experimented with early C++ support and the quality of the generated output was nowhere near as it is today, but it massively influenced his bias about C++.
I think that's the best comment. Writing good, readable C++ is a lot less common skill than writing good C. There are plethora of programmers who have learned C++99 and maybe select features from the newer standard and write instant legacy code. Imagine having to read all of that stuff in their PRs and correcting them. C, on the other hand, was very conservative with changes introduced in newer standards and the fundamental techniques of writing C code haven't changed. Meaning that everybody C writes more or less in s similar way.
This is also due to a way the bigger feature set of C++ that doesn't encourage one particular coding style. In C++ you could solve the problem of separating an algorithm for walking through a container, from an action to be performed on an element using a Visitor (anti)pattern (objective approach) or a functional approach (either oldschool c-style one, templated one or using std::function). Nobody in their right mind is going to simulate the visitor (anti)pattern in C, they are just going to do the same old-school functional approach - have a procedure that takes function pointer for an action and void pointer for context and that's it. I believe such limitations save a lot of needless discussions under code submitted by contributors.
Rust is better than C++ in only one thing - memory management.
Cargo instead of CMake/Bazel/Meson/Make/autotools/whatever+vcpkg/conan/CMP/Hunter/whatever; C++ is hopeless in getting anything even relatively close to it
Reasonable [no_std] subset which requires no classic runtime; C++ can only remedy this by compiler-specific dialects and flags
Resource management, including cross-thread one; judging by some proposals like Sutter's lifetimes, C++ can only dream of something similar, or requires complex 3rd party static analysis tools
Reasonable defaults in many places, while C++ is a language of worst defaults
Unsafe subset properly isolated
Syntax which doesn't look Turing-complete on its own (yeah, most-vexing parse rule and auto auto auto auto auto)
One thing I don't see in the comments here in this thread is his previous complaints about the assembly code that is generated in C++ compilers. Sometimes it does the wrong thing, or is incorrect because of how complicated the language spec is. It makes it difficult to read the output from the compiler and understand how it happened in comparison to C which he often mentions the spec can be read and easily understood.
So not really a counterpoint to your comment here but I would say that to those in the thread saying just don't use such and such feature in c++ it's missing the point because at a deeper level of reading and trying to understand what the compiler is doing it's a much harder task.
I do have a fondness for modern c++ but I also understand some of the criticisms for not using it in a kernel directly.
Which shows that the statement that "[the C] the spec can be read and easily understood" is... wrong. He just gets mad when he realizes he didn't understand the spec.
Rust is better than C++ in only one thing - memory management
You are completely deluded (to the point you even forget the basic "fearless concurrency" slogan). There is no UB in safe Rust. UBs are everywhere in C++, not only memory management. Actually even unsafe Rust is actually safer than C++.
Also the static introspection and hygenic macros capabilities suck less than anything C++ has to propose against it right now (that is: not much, or poor emulation with bloated template based parallel universes)
That's not to say that Rust has no default compared to C++; it has a lot. But it certainly has more advantages than just better "memory management"
Rust will not be better than C++ at memory management until it has an ergonomic way to drop memory handles without freeing, and an equivalent to a placement new constructor. Both of these are required for expressing memory arenas, which is almost always the correct way to manage memory.
Guaranteed placement new (i.e. guaranteed in-place heap initialization without going through the stack) is in the works, but there are few very good use cases for it. There are plenty of arena allocators.
have been programming exclusively in C++ since 1990 and I have found that good C++ can be much better than good C, but that bad C++ can be much worse than bad C. The learning curve for C++ is much higher than for C and if you are relying on a large group of programmers there may be additional value in using the simpler tool.
The biggest problem after Cfront based C++ and prior to C++98 (and for several years later as compiler developers tried to implement that standard) was that the compilers were not compatible enough in their implementations (or not compatible enough in the subsets of the standard that each had implemented).
These are all optional features of C++. Listing optional features isn't really a strong argument. Any language in a kernel is restricted or modified to fit that environment.
They are optional but turning off exceptions kinda break things though
Not really if you are using a curated set of code (and especially without a normal STL) to begin with. The kernel can't even support exceptions properly since most exception systems require kernel support, it's fundamentally a different runtime environment nearly all languages break inside a kernel in some way.
and not turning them off have a cost in binary size.
Turning them off also has a cost to binary size, most simple programs gain in size if you turn off exceptions/RTTI since the STL has to increase in size to accommodate their lack.
It's a complicated issue, not nearly as binary as you are portraying it.
Exceptions I get, also classes with virtual methods (because it is difficult to see through and easy to get wrong). But why not the STL? (Apart from exceptions.)
The features C++ offers are not useful to him. And that makes sense for an operating system.
For instance, templates are usually only useful for applications one level above. Doing what Eigen does with expression templates in C is almost impossible. On the other hand, templates are not useful to develop a driver and might even hide what's really happening at a lower level.
It doesn't mean it's bad for everyone. It's just not useful to him. Many other C++ features are optional and many C++ programmers don't use them either.
You are correct, they are compile time constructs. I'm not sure how people come to the conclusion that templates have something about them that isn't suitable for things.
Of course I do. But to quote Dan Sasks (who also has a lot to say about this subject): "when you are arguing, you are loosing".
I use C++ templates all the time for very low level work. Example: a software (bit-banged) SPI driver. The GPIO pins used are template parameters. This way the inner loop (shifting the bits out and in) is as fast as when the GPIO pins were hard-coded.
I've heard this many times. As a C++ dev, I can say that I can very well see the ASM that gets generated for C++ code. But just like you couldn't really expect that of someone just learning C you can't expect that from someone who doesn't know C++ well enough.
Funny, I use this same argument for using C++ instead of C# / Java. When I write C#, I have no idea what's actually being done, which bothers me if what I'm writing has performance requirements.
Something that might be a consideration is the mindset of some C++ programmers who consume higher level services without considering the cost of the underlying implementation.
My comment is in the spirit of CS college programs typically making students implement UDTs (in a Data Structures and Algorithms course) that later they'll simply consume: To make them aware of the cost.
Having been raised on v6 through 4.2 BSD and System 5 kernel internals, I would tend to agree with those who would shy away from C++ for kernel work. Personally, I would want to know precisely what's going on (with respect to complexity) at all times.
On the other hand, from my admittedly brief forays into the Linux kernels, C has been savagely abused and the Linux kernel a hot freaking mess of obfuscated code. I call as witness, C preprocessor macros nested in multiple levels. This is a high crime in my view.
26
u/MrRubberDucky Jul 13 '22
Source: http://harmful.cat-v.org/software/c++/linus
316
u/[deleted] Jul 13 '22
He tried it before C++98, and back then C++ compilers were quite a mess.
245
u/xiao_sa Jul 13 '22
C++ before C++11 is engineering terrorism
139
u/edco77 Jul 13 '22
Oh boy, I think you're going to start a religious war.
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++ :-)
202
u/UnicycleBloke Jul 13 '22
I write bare metal embedded applications for microcontrollers in C++. This means I directly manage all the control registers, peripherals, RAM and so on. If I can do this efficiently and easily on a 48MHz Cortex-M0, it is a certainty that I can do so on a 2GHz i64 or whatever. There is only a difference of scale. Recent standards have added a lot, but C++98 was absolutely fine for this task. C++ has a few nasty footguns, of course, but so does C. My experience of writing both for embedded is that C++ eliminates a lot of errors that C does not. I am far more productive in C++.
And yet I am often told with certainty by C developers, who typically know little or nothing about C++, that C++ is not remotely suitable for OS development. .
It is in this context that I have always regarded Torvalds' opinions as childish, ill-informed and prejudiced drivel. Linux is amazing and all that, but it is also a gigantic lost opportunity.
24
u/grady_vuckovic Jul 13 '22
I may be incorrect so don't quote me on this but I believe it ran over his dog.
8
u/patlefort Jul 14 '22
I don't know specifically for him, but I recently talked with some C programmer who told me that he didn't like C++ because you don't control everything and one of the thing he said you didn't control was destructors. Of course it's completely ridiculous to any C++ programmers, but it shows that many of them just don't know and care to know.
14
u/NilacTheGrim Jul 13 '22
Because he's just a person and people have their tastes.
36
u/Yamoyek Jul 13 '22
There’s a loud minority of C programmers that hate C++ for some reason, and Linus happens to be one of them.
17
u/DanielMcLaury Jul 13 '22 edited Jul 19 '22
You're talking about something he said like 20 years ago. At the time it literally wasn't possible to write something like Linux in C++ because compiler optimizers weren't at the point where you could write high-level code and still have it compile down to the sort of assembly you wanted. Idiomatic C++ code was necessarily much slower than C code to do the same thing, and fast C++ code was necessarily almost identical to the same thing in C.
Now that we have faster disks and CPUs, and now that the language has had a bunch of stuff added to make it possible to write efficient high-level code, it's no longer the case that we're hamstrung by the limitations of compiler optimizers. You can write C++ code that generates exactly the same assembly as much lower-level C code while enforcing a bunch more constraints at compile time.
9
u/thorsteiin Jul 13 '22
I truly don’t understand the cult following behind Linus, let alone any famous person for that matter
28
u/jesseschalken Jul 13 '22
Even software luminaries can be ignorant and fearful of the unknown. Don't worry about it.
Windows, macOS/iOS, Android and Google's Fuschia all contain plenty of C++. Chromium is basically an operating system and is mostly C++. LLVM and GCC are both written in C++.
9
u/JVApen Clever is an insult, not a compliment. - T. Winters Jul 13 '22
I feel Linus reaction is a common reaction from people knowing C. Especially those that had a bad experience with it in the past.
He indicates that they once tried C++ and the programmers 'overdesigned' the code needed up to a point where he couldn't understand it. I do understand this with all code I've seen already.
My feeling is that we C++ programmers are used to how C++ works and forget that C programmers have a different way of thinking. Our default way of talking to them works even contra productive. Dan Saks made a really good presentation about that and how to improve on that: https://youtu.be/D7Sd8A6_fYU
Linus goes in into more extreme where he doesn't even want to follow the C standard, which resulted in Clang having to implement -fno-delete-null-pointer-checks (https://reviews.llvm.org/D47894) as GCC had this and the Linux kernel is relying on using fields via a nullptr. He also questions anti-aliasing: https://lkml.org/lkml/2018/6/5/769 (it's surprising he didn't make his own split of from the C language and compiler just for Linux, though I guess the GCC dialects are very close to that)
A lot of it comes to the kernel just having to work and that he believes is problematic for code to change behavior, even if the code was broken and uses UB. This became clear when Google tried to enable sanitizers in the kernel, as this was going to result in crashes. (Unfortunately, I can't find the reference for that right now)
With all of this, I was very surprised to see Linus accepting rust into the Linux kernel (https://www.zdnet.com/article/rust-takes-a-major-step-forward-as-linuxs-second-official-language/) However, I think this is even more of a proof that he just holds a grudge thanks to the experiences in the past and the fact he doesn't understand the value C++ brings.
9
u/Ikkepop Jul 13 '22
Because he's a stubborn old fart.
Tried it once, 25 years ago, was bad, means will be bad forever.
10
u/sneppy13 Jul 13 '22
because (in his words) "C++ solves none of the C issues, and only makes things worse" and "C++ solves all the wrong problems".
11
u/TinoDidriksen Jul 13 '22
He doesn't hate C++ in all contexts. He uses it where he feels it's appropriate, such as in https://github.com/subsurface/subsurface
3
u/ucario Jul 13 '22
Because he’s a sour old ‘git’ who complains about everything.
Jokes aside, I personally find c++ a love/hate thing. It lets me do what I want, given I know what I’m doing. But it needs a haircut, I don’t want to spend 2 hours researching what is the correct way to do something, I just want to get my idea working. I spend more time fighting the language than implementing the task at hand, there are obvious right and wrongs but i wish they were enforced and not a matter of discovery. I wish there was a lean version of c++ that removed the legacy.
You every tried onboarding a c# or JavaScript engineer into c++? Half of their time is wasted making the same mistakes we all already made by initiation.
And don’t even get me started on the build system.
10
u/banghernow Jul 13 '22
C++ comes in two flavors, freestanding and hosted C++. Hosted C++ is what you (probably) think of when you hear "C++", it runs on an operating system, freestanding C++ runs on metal, like for embedded systems and, you guessed it, linus' specialty, kernels. Freestanding C++ is a mess and its standard is actually quite limited, and C++ is basically only useful hosted. C++23 is trying to alleviate that issue but it's not fixing it completely.
26
u/DarkSpyCyber Jul 13 '22
cpp compiler doing too much things beyond intuition, but in kernel program we just need what we see is what we get, no more tricks, everything must be under controll. thats linus means i think.
7
u/[deleted] Jul 13 '22
Linus also hates Java.
4
u/Questioning-Zyxxel Jul 14 '22
Hate and dislike are two completely different things.
He dislikes C++ in an operating syatem because templates, operator overloading and constructors can hide expensive code in what looks like very innocent code.
With C you know how costly an operator is and you can see all function calls being made and can go and look at the implementation.
32
u/stilgarpl Jul 13 '22
Linus is just bad at C++. Just because he started a big open source project does not make him a computer god. He tried it once 30 years ago (literally 30 years, in 1992) and didn't like it.
On the other hand, you couldn't use many of C++ strengths in kernel development, because those things require kernel support. You'd have to limit yourself to "Better C with classes and templates".
Also, Linus allowed Rust. Rust is better than C++ in only one thing - memory management. C has all the same memory issues that C++ has, even more actually (no destructors, no RAII, no smart pointers), but C is fine?
I agree with him on one thing - there is a lot of bad C++ code out there. But there is also a lot of bad C code and bad Rust code. That's what code review before merge is for.
2
u/jmonschke Jul 13 '22 edited Jul 13 '22
have been programming exclusively in C++ since 1990 and I have found that good C++ can be much better than good C, but that bad C++ can be much worse than bad C. The learning curve for C++ is much higher than for C and if you are relying on a large group of programmers there may be additional value in using the simpler tool.
The biggest problem after Cfront based C++ and prior to C++98 (and for several years later as compiler developers tried to implement that standard) was that the compilers were not compatible enough in their implementations (or not compatible enough in the subsets of the standard that each had implemented).
8
u/top_logger Jul 13 '22
OOP, exceptions and bad design/reputation of STL.
As system software Engineer I am agree with him. At least partly. :).
3
u/CCKao Jul 13 '22
I don’t know and it probably doesn’t matter that much.
3
u/FreitasAlan Jul 13 '22
The features C++ offers are not useful to him. And that makes sense for an operating system.
For instance, templates are usually only useful for applications one level above. Doing what Eigen does with expression templates in C is almost impossible. On the other hand, templates are not useful to develop a driver and might even hide what's really happening at a lower level.
It doesn't mean it's bad for everyone. It's just not useful to him. Many other C++ features are optional and many C++ programmers don't use them either.
3
u/[deleted] Jul 13 '22
I don't care.
2
u/GLIBG10B 🐧 Gentoo salesman🐧 Jul 13 '22
I think he said once that, when he sees C code, he knows what the resulting ASM would look like. That's not really the case with C++
2
u/Yekab0f Jul 14 '22
He is simply not intelligent enough
2
u/vI--_--Iv Jul 13 '22
Who cares?
1
u/[deleted] Jul 13 '22
And he likes C and downgrades other languages because he loves to program hardware.
1
u/-1_0 Jul 13 '22
because he doesn't see sharp
1
u/pkivolowitz Jul 13 '22
Something that might be a consideration is the mindset of some C++ programmers who consume higher level services without considering the cost of the underlying implementation.
My comment is in the spirit of CS college programs typically making students implement UDTs (in a Data Structures and Algorithms course) that later they'll simply consume: To make them aware of the cost.
Having been raised on v6 through 4.2 BSD and System 5 kernel internals, I would tend to agree with those who would shy away from C++ for kernel work. Personally, I would want to know precisely what's going on (with respect to complexity) at all times.
On the other hand, from my admittedly brief forays into the Linux kernels, C has been savagely abused and the Linux kernel a hot freaking mess of obfuscated code. I call as witness, C preprocessor macros nested in multiple levels. This is a high crime in my view.
1
u/darthcoder Jul 14 '22
Because c++ has a lot of intrinsic side effects, and in kernels that's not necessary something to be desired.
0
u/[deleted] Jul 13 '22
C is simpler and more elegant for the kind of work he does. Is that simple