Michael Bradley told me about this quote from Linus Torvalds
In fact, in Linux we did try C++ once already, back in 1992.
It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.
The fact is, C++ compilers are not trustworthy. They were even worse in
1992, but some fundamental facts haven't changed:
- the whole C++ exception handling thing is fundamentally broken. It's
especially broken for kernels.
- any compiler or language that likes to hide things like memory
allocations behind your back just isn't a good choice for a kernel.
- you can write object-oriented code (useful for filesystems etc) in C,
without the crap that is C++.
In general, I'd say that anybody who designs his kernel modules for C++ is
either
(a) looking for problems
(b) a C++ bigot that can't see what he is writing is really just C anyway
(c) was given an assignment in CS class to do so.
I understand Linus' point of view, but I don't agree with him. Infact, I think HE is writing C++ code without realizing it (plus he is wasting his time by hand-writing lots of code that C++ would generate automatically).
IMHO, it is mostly a question of HOW you think. Hardware guys (and I count Linus as such) think more concrete, in terms of the states and state-transitions of the devices and how to control them. These guys tend to write code that basically simulates hardware. For example they use bytes as if they were a bundle of 8 cables, being energized (1) or not energized (0) and every instruction turns some of them on and/or some of them off (I do that sometimes in the videos, too, although I tried to avoid it).
OOP - or better: polymorphic programming is more about expressing the semantics of the resulting system - the high-level idea of what the desired behavior is and then breaking that down further and further to more and more concrete sub-classes, until you reach the hardware-level.
This video explains that well.
Although I don't use exceptions here: since Linus mentioned them, let me say something about that topic: C++ exception handling is not easy to do
100% right, but it solves more problems than it causes (see the 3 videos on
http://exceptionsafecode.com). Without exceptions, C++ becomes very close to C, but you have stronger type-safety (I really like strict typing - it catches a lot of errors at compile-time that would otherwise cause run-time errors or worse) and you also get safer polymorphism. You can do polymorphism in C - and Linux does that (that is why I say Linus is actually writing C++ code), but it is harder, because you have to create the VTables manually, multiple inheritance is madness and you need to be more careful to not accidentally call the wrong "method". C++ solves these problems for you.