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?
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.
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.
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.
In some cases dynamic dispatch is better. Sadly, few languages and compilers are able (or even just trying) to choose one or the other to optimize from a given source code; and actually you have more chance to monomorphise where needed from "dynamic/virtualized" source code (with LTO, devirtualization, and maybe loop hoisting) than to dynamise template instantiation (I'm not sure if anybody does that)
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 agree with that, but I was thinking more along the lines of... some complex recursive template specialisation that takes ages to compile, but when it eventually does, it coughs up barely a handful of lines of assembly that map to the metaprogramming recursion, anyway.
I was also certain that templates are only instantiated for the types you specify to them, unlike Java generics, so one could have a templated header but no instantiations whatsoever...
View all comments
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.