Antti wrote:Solar, what is your opinion on Linux kernel community's position as far C++ is concerned?
tl;dr
I don't consider the Linux kernel devs in general to be trustworthy in a language discussion, and I don't discuss technology with people when I have to doubt their word.
----
Which position, exactly? Over the years they have spouted so much nonsense on the subject that it's hard to nail down anything specific.
I think the worst part about it all is that they are dishonest. Let's take my favorite example,
the LKML FAQ section 15.3. Because I'm bored, let me pick it apart for you.
LKML15.3 wrote:Why don't we rewrite the Linux kernel in C++?
Strawman. I have
never seen anyone asking for that in earnest.
Ever. We'll get to what the "frequently asked question" actually is later on.
LKML15.3 wrote:(ADB) Again, this has to do with practical and theoretical reasons. On the practical side, when Linux got started gcc didn't have an efficient C++ implementation, and some people would argue that even today it doesn't.
[WP:WHO]. "Some people would argue" is weasel wording.
LKML15.3 wrote:Also there are many more C programmers than C++ programmers around.
For one,
I challenge that claim. Secondly, how many of those "many more C programmers" are complete beginners fresh out of "C as your first programming language" -- and therefore people you
do not want commiting Linux kernel patches (and tying up resources for no practical gain)?
While I, personally, recommend
and teach C++ as first programming language, generally speaking C++ programmers today are usually people who are on their third, fifth, or seventh programming language and have some solid experience. Also, working with C++
does give you a better idea of how to manage your code. (See "OO programming" further down.)
But I admit that's just my gut feeling. Same as it's (ADB)'s gut feeling that the pool of C programmers is superior. Let's call this one a draw.
LKML15.3 wrote:On theoretical grounds, examples of OS's implemented in Object Oriented languages are rare (Java-OS and Oberon System 3 come to mind), and the advantages of this approach are not quite clear cut (for OS design, that is; for GUI implementation KDE is a good example that C++ beats plain C any day).
This is a howler. Not only because you can perfectly well write C++ without going OO at all, but because later on they're boasting that the Linux kernel already
is using OO techniques, just using C to implement them. (We'll get to that.)
LKML15.3 wrote:(REW) In the dark old days, in the time that most of you hadn't even heard of the word "Linux", the kernel was once modified to be compiled under g++. That lasted for a few revisions. People complained about the performance drop. It turned out that compiling a piece of C code with g++ would give you worse code. It shouldn't have made a difference, but it did. Been there, done that.
Later on they quote Linus about this "experiment" having taken place in 1992. That's six years before the first C++ standard was ratified, and firmly pre-templates (which are, IMHO, much more defining what C++ is about than OO is)...
I'd like to see the tests they ran that gave those results, because I can't really picture how the mere switch from gcc to g++ should have any significant performance impact. But using an unpublished 25-year-old benchmark of a given pre-standard compiler as an argument against a
language to this day is lame.
LKML15.3 wrote:(REG) Today (Nov-2000), people claim that compiler technology has improved so that g++ is not longer a worse compiler than gcc, and so feel this issue should be revisited. In fact, there are five issues. These are:
- Should the kernel use object-oriented programming techniques? Actually, it already does. The VFS (Virtual Filesystem Switch) is a prime example of object-oriented programming techniques. There are objects with public and private data, methods and inheritance. This just happens to be written in C. Another example of object-oriented programming is Xt (the X Intrinsics Toolkit), also written in C.
Compare this with the earlier wholesale dismissal of OO programming (and, as I said, "C++" is not equivalent to "OO" is not equivalent to "C++").
LKML15.3 wrote:What's important about object-oriented programming is the techniques, not the languages used.
But if the language used is actually
supporting your programming paradigm with native syntactic elements, you don't have to get all tricky with "void *", partial structs, init() functions and forgetting to clean up your resources when the handle goes out of scope...
Also, no C textbook I've seen so far really goes into "OO C", gives best practices etc... so we're looking at ad-hoc-ery done in a language not actively supporting the paradigm they're using, while at the same time dissing a language that does
for doing so...?!?
LKML15.3 wrote:Should the kernel be rewritten in C++?
Strawman again, see above.
LKML15.3 wrote:This is likely to be a very bad idea. It would require a very large amount of work to rewrite the kernel (it's a large piece of code). There is no point in just compiling the kernel with g++ and writing the odd function in C++, this would just result in a confusing mix of C and C++ code. Either the kernel is left in C, or it's all moved to C++. To justify the enormous effort in rewriting the kernel in C++, significant gains would need to be demonstrated. The onus is clearly on whoever wants to push the rewrite to C++ to show such gains.
Quoting for completeness. A discussion here would be much more about inertia to change, incremental / partial conversions etc., all of which has little to do with C++ per se.
LKML15.3 wrote:Is it a good idea to write a new driver in C++? The short answer is no, because there isn't any support for C++ drivers in the kernel.
Circular reasoning. Hey, pal, you've been asked if it were a good
idea. Which, well, implies
that it is made possible. (For example by accepting the patch someone actually commited, see below.) Answering that it isn't a good idea because you're intentionally roadblocking the option is dodging the subject.
See the "header" discussion further below.
LKML15.3 wrote:Why not add a C++ interface layer to the kernel to support C++ drivers? The short answer is why bother, since there aren't any C++ drivers for Linux.
Now he's
really pulling our leg here. So it isn't a good idea to write a C++ driver because there's no support for it. And adding support for it isn't a good idea because there are no C++ drivers?
Really?
LKML15.3 wrote:However, if you are bold enough to consider writing a driver in C++ and a support layer, be aware that this is unlikely to be well received in the community. Most of the kernel developers are unconvinced of the merits of C++ in general...
...the reasons for which,
today, the FAQ has utterly failed to communicate...
LKML15.3 wrote:...and consider C++ to generate bloated code.
Given that they're using the very same techniques that C++ compilers use to get an OO-ish architecture, just using ad-hoc-ery instead of language / compiler support, I'd
really like to know what the heck he's talking about.
LKML15.3 wrote:Also, it would result in a confusing mix of C and C++ code in the kernel.
Err... no. No C++ programmer is "confused" by a mix of C and C++. And a non-C++ coder will not be looking at my C++ driver source. Please walk on, nothing to see here.
LKML15.3 wrote:Any C++ code in the kernel would be a second-class citizen, as it would be ignored by most kernel developers when changes to internal interfaces are made.
Actually, kernel developers ignore you
anyway when they make changes to internal interfaces.
This happened over and over again. Madwifi. Xorg / XFree86. Some graphics chip driver I cannot remember the name of anymore. Just to name three instances where I, personally, was hit by a case of "we've changed the interface and now the associated driver / support code doesn't work anymore".
Their answer, then and now, always was "bugger the maintainer or fix it yourself".
Drivers are second-class citizens.
And now they claim that they would... I don't know... ignore you
more when your code is in C++? How would
that look like, exactly?
LKML15.3 wrote:A C++ support layer would be frequently be broken by such changes (as whoever is making the changes would probably not bother fixing the C++ code to match), and thus would require a strong commitment from someone to regularly maintain it.
I snigger at how he seems to be under the impression that such a "support layer" would be something that would require a "strong commitment", when it's basically just about the following (because, honestly, no-one is asking for exception handling or RTTI at this point).
LKML15.3 wrote:Can we make the kernel headers C++-friendly?
This is what people have actually asked for. They want to write a driver, and they want to write it in C++. They can't, because the Linux headers use C++ keywords. They ask this to be changed, so they can write their driver -- which will be up to
them to maintain -- in the language they
want to maintain. They're disenfranchised.
And the arguements get outright ridiculous.
LKML15.3 wrote:This is the first step required for supporting C++ drivers, and on the face seems quite reasonable (it is not a C++ support layer). This has the problem that C++ reserves keywords which are valid variable or field names in C (such as private and new). Thus, C++ is not 100% backwards compatible with C. In effect, the C++ standards bodies would be dictating what variable names we're allowed to have. From past behaviour, the C++ standards people have not shown a commitment to 100% backwards compatibility. The fear is that C++ will continue to expand its claim on the namespace. This would generate an ongoing maintenance burden on the kernel developers.
Let's have a look at that "ongoing maintenance burden". C++11 added:
- alignas, alignof, char16_t, char32_t, constexpr, decltype, noexcept, nullptr, static_assert, thread_local
C++20 will add:
- char8_t, concept, consteval, requires
Yea... terrible. Good thing that the C standard namespace doesn't expand, hm?
C99:
- inline, restrict, _Bool, _Imaginary, _Complex
If you're using the convenience macro includes in <stdbool.h> and <complex.h>, then "bool", "complex" and "imaginary" are off-limits as well. The types (char16_t, char32_t) are standard typedefs you should also stay away from in C.
C11:
- _Alignas, _Alignof, _Atomic, _Generic, _Noreturn, _Static_assert, _Thread_local
These with the convenience macros alignas, alignof, atomic_bool, atomic_int, (...), noreturn, static_assert, and thread_local.
I rest my case. But (REG) doesn't, so...
LKML15.3 wrote:Note that someone once submitted a patch which performed this "cleaning up". It was ~250 kB in size, and was quite invasive. The patch did not generate much enthusiasm.
Someone
did the work. (So much for the "Eric claim" further down that says it cannot be done in reasonable time.) But they were "meh" about it, because then they'd lose one of their main arguments about not "allowing" C++ drivers or C++ code in general -- the technical impossibility. Figures.
LKML15.3 wrote:Apparently, someone has had the temerity to label the above paragraph as "a bit fuddy". So Erik Mouw did a short back-of-the-envelope calculation to show that searching the kernel sources for possible C++ keywords is a nightmare. Here is his calculation and comments (dates April, 2002):
This Erik then does a line count on the sources (
all the sources, not just the headers...), and then does "calculations" assuming that you'd step through those sources
manually looking for C++ keywords, taking 5 seconds per line.
He then makes a remark that you would need to write "a *very* intelligent search-and-replace tool for [automating] that".
You know what? All I need to write is
one regex that makes Vim jump to the next potential keyword candidate... of which there are... wait a second... 132128 lines in today's kernel... which is two orders of magnitude less already, and could probably be further reduced significantly by better filtering and automatic handling of recurring cases.
Instead of pointing that out, (REG) goes on to paint a bleak picture of what would happen if you "just compile with C++ and fix all compiler errors".
At which point I believe they are intentionally trying to sell us for fools.
LKML15.3 wrote:My personal view is that C++ has its merits, and makes object-oriented programming easier. However, it is a more complex language...
That's the second morsel of truth in the whole section, the first having been that a rewrite of the whole kernel would be a momentous undertaking for comparatively little (short-term) gain. But that's not what we're discussing here, we're talking about
starting a project in C++ or not.
LKML15.3 wrote:...and is less mature than C.
Depending on what you call "mature". Being the older language, and having a (much) smaller feature set, in a way C can't
help but being more "mature". Obsolete and inconvenient would be other adjectives one could use.
LKML15.3 wrote:The greatest danger with C++ is in fact its power. It seduces the programmer, making it much easier to write bloatware. The kernel is a critical piece of code, and must be lean and fast. We cannot afford bloat. I think it is fair to say that it takes more skill to write efficient C++ code than C code. Not every contributer to the linux kernel is an uber-guru, and thus will not know the various tricks and traps for producing efficient C++ code.
Again, at first glance this sounds like it makes perfect sense. But given that the
structure of the kernel code is
already mostly OO, and that things like initialization, inheritance, and destruction are done in an ad-hoc manner... somehow I doubt that the risk for bloat is so much less. In the end, bloat happens when someone writes sub-par utility code that is then used in a sub-optimal way. This happens regardless of the language involved. And C++'s capability to write
good utility code should be beyond doubt by today.
On the contrary, I think it takes an uber-guru to make sense of the Linux kernel's C plumbing,
especially when you are not
also a C++ coder who has a mental picture of "virtual table" etc. to superimpose on it. We call these uber-gurus "kernel devs". It shouldn't
take a training in "WTF is this s***?" up front to do kernel work.
The section ends with a statement by the head troll, Mr. Torvalds himself:
LKML15.3 wrote:The fact is, C++ compilers are not trustworthy.
What?
LKML15.3 wrote:
- the whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels.
Agreed, as far as kernel space is concerned. Don't use it. End of story.
LKML15.3 wrote:
- any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel.
Neither the compiler nor the language do hide memory allocations. A couple of the classes in the standard library do. But then,
so does fopen(). The most important thing that proper C++ "hides" is the need to
free those resources (and the resource leak if you don't)...
LKML15.3 wrote:
- 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.
Feel free to make up (d).
So, bottom line: What the Linux kernel devs are giving you are lots of (outdated) opinion and self-contradictory or circular reasoning.
Or, to put it differently, lies, damn lies, and propaganda.
That's not the basis I'd like to discuss pro's and con's of languages on, just like I don't discuss pro's and con's of git vs. SVN with the git crowd. You get your intelligence insulted and cussed at, but you can't get them to sit down and actually talk specifics. They just repeat what they've talked themselves into believing.