Using a small set(kernel-c++20) it can simplify the existing C code by quite a bit, you get ctor, dtor, class inheritance etc all for free. Currently the kernel is basically in object-oriented C anyways.
Are you referring to a specific subset of C++?
Converting the Kernel to C++
11–20 of 56 posts
Re: Converting the Kernel to C++
#12I'm skeptical that this is better than starting to rewrite the kernel in Rust. It would be better if everyone can focus on rewriting things into Rust rather than having a choice of rewriting into C++ or Rust. Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. It's also in kernel developers best interest to start learning Rust, and getting them to start earlier than la…
> Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. Did you read the entire email? Here's a quote that seems to directly contradict you: > converting C code to Rust isn't something that can be done piecemeal, whereas with some cleanups the existing C code can be compiled as C++. As far as I can tell, the author is correct. What is the pattern for converting C to Rust…
You can call Rust from C, and C from rust. You can convert it function by function.
Re: Converting the Kernel to C++
#13I'm skeptical that this is better than starting to rewrite the kernel in Rust. It would be better if everyone can focus on rewriting things into Rust rather than having a choice of rewriting into C++ or Rust. Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. It's also in kernel developers best interest to start learning Rust, and getting them to start earlier than la…
Yes, what C++ is supposedly good for – RAII, it actually got a little wrong:
1. Default construction / value initialization: Causes lots of initialization before assignment that is obviously unnecessary. Try allocating a buffer: `std::make_unique` surprisingly memsets the buffer.
2. Constructors: No way to fail without exceptions. That buffer example again: Without exceptions, `std::make_unique` will actually attempt to memset a nullptr on allocation failure … before your nullptr check (which btw makes the compiler entitled to delete your nullptr check as well).
3. Move is a (burdensome) runtime state that mandates nullability.
4. Destructors: Can't take arguments, forcing objects to contain implicit references to each other.
Rust's affine type system fixes 1-3, but not 4, which only a linear type system could: https://en.wikipedia.org/wiki/Substructural_type_system#The_...
Re: Converting the Kernel to C++
#14I'm skeptical that this is better than starting to rewrite the kernel in Rust. It would be better if everyone can focus on rewriting things into Rust rather than having a choice of rewriting into C++ or Rust. Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. It's also in kernel developers best interest to start learning Rust, and getting them to start earlier than la…
> Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. Did you read the entire email? Here's a quote that seems to directly contradict you: > converting C code to Rust isn't something that can be done piecemeal, whereas with some cleanups the existing C code can be compiled as C++. As far as I can tell, the author is correct. What is the pattern for converting C to Rust…
No it is not. First example: initialisation of structs
Re: Converting the Kernel to C++
#15Old but gold https://harmful.cat-v.org/software/c++/linus
EDIT: I failed to notice the date stamp, you can ignore this. Jeeze. Linus writes like C++ killed his dog or something. There's plenty of great C++ 'wins' over C that make just doing everyday programming tasks so much easier and simpler for no cost. You don't need to write so abstract and in the clouds that it becomes impossible to maintain - I certainly don't. And frankly, you can shoot yourself in the foot just as…
Re: Converting the Kernel to C++
#16Earlier quoted context omitted.
> Unlike this email, C can be converted into Rust piecemeal and integrate with the rest of the kernel. Did you read the entire email? Here's a quote that seems to directly contradict you: > converting C code to Rust isn't something that can be done piecemeal, whereas with some cleanups the existing C code can be compiled as C++. As far as I can tell, the author is correct. What is the pattern for converting C to Rust…
>What is the pattern for converting C to Rust piecemeal? You take a piece and either rewrite it yourself or use a transpiler that produces potentially unsafe Rust code which you then later would want to rewrite into safe Rust code.
An example of the latter: c2rust, which is a work in progress but is very impressive https://github.com/immunant/c2rust
It currently translates into unsafe Rust, but the strategy is to separate the "compile C to unsafe Rust" steps and the "compile unsafe Rust to safe Rust" steps. As I see it, as it makes the overall task simpler, allows for more user freedom, and makes the latter potentially useful even for non-transpiled code. https://immunant.com/blog/2023/03/lifting/
Re: Converting the Kernel to C++
#17Old but gold https://harmful.cat-v.org/software/c++/linus
EDIT: I failed to notice the date stamp, you can ignore this. Jeeze. Linus writes like C++ killed his dog or something. There's plenty of great C++ 'wins' over C that make just doing everyday programming tasks so much easier and simpler for no cost. You don't need to write so abstract and in the clouds that it becomes impossible to maintain - I certainly don't. And frankly, you can shoot yourself in the foot just as…
made it out to be. That post is over 15 years old at this point, which goes to the very point of the posted article - C++ has (subjective opinion here) improved a lot in that time.
No idea what his current opinion on C++ is. Personally I'd rather there was no C++ or Rust in the Linux kernel. IMHO it would be significantly less of a cognitive burden to convert the entire kernel gradually to C++ than Rust.
But I would necessarily hold a man to opinions he held 15 years ago when the landscape has changed so much in that time. Rust was only born the previous year (2006) and not championed by Mozilla until 2009. Yet here we are and there is support in the official kernel for it anyway, whereas C++? Not so much.
Re: Converting the Kernel to C++
#18Re: Converting the Kernel to C++
#19I don't think that Linus will allow this since he just commented that he will allow rust in drivers and major subsystems [1].
would have hoped see more answers or see something in here from actual kernel developers.
0: https://github.com/gcc-mirror/gcc/commit/5329b59a2e13dabbe20...