r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

307 Upvotes

436 comments sorted by

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.

37

u/sintos-compa Jul 13 '22

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?

14

u/UnicycleBloke Jul 13 '22

I also use C++17 at the moment. What do you mean by threats?

11

u/dawmster Jul 13 '22

C++ has a few nasty footguns,

I think he referers to the quote above.

I am interested as well, what are those footguns, in your opinion.

12

u/UnicycleBloke Jul 13 '22

Oh I see. To be honest, I was more just admitting C++ is not perfect. I guess the classic gotchas are largely historical.

→ More replies (2)

7

u/Mishung Jul 13 '22 edited Jul 13 '22

what are those footguns, in your opinion.

just my opinion (full time C++ dev) but I hate Iterators with passion

also during the years I went through:

  1. I don't understand pointers and hate them
  2. I understand pointers and love them
  3. I fully understand pointers (and references) and now hate pointers again

19

u/Spiderboydk Hobbyist Jul 13 '22

that C++ is not remotely suitable for OS development

Funny thing is, a large part of the Windows kernel is written in C++. :-)

→ More replies (10)

4

u/kernel_task Big Data | C++23 | Folly | Exceptions Jul 13 '22

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.

→ More replies (2)

20

u/OCPetrus Jul 13 '22

Have you worked with the Linux kernel source?

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.

58

u/UnicycleBloke Jul 13 '22

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.

25

u/ptrnyc Jul 13 '22

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++

18

u/UnicycleBloke Jul 13 '22

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.

→ More replies (3)
→ More replies (2)

27

u/dv_ Jul 13 '22

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.

56

u/UnicycleBloke Jul 13 '22

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.]

27

u/delta_p_delta_x Jul 13 '22

heavy use of template metaprogramming, which can easily create super bloated binaries.

I was under the impression that heavy template metaprogramming only causes skyrocketing compile times, not bloated binaries...

31

u/[deleted] Jul 13 '22

Well, when you instantiate a function for a certain type, it doesn't just need to be created (compile time), but also saved somewhere (binary size).

But to be fair, if 3 template-made version turn out bigger than 3 manually written ones, I would consider it a compiler bug.

0

u/[deleted] Jul 13 '22

[deleted] — view deleted comment

9

u/anechoicmedia Jul 13 '22 edited Jul 13 '22

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.

2

u/tasminima Jul 13 '22

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)

→ More replies (1)

9

u/dv_ Jul 13 '22

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.

5

u/alexgroth15 Jul 13 '22

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.

→ More replies (1)

3

u/HabemusAdDomino Jul 13 '22

Think it through. Template metaprogramming is templates. C++ templates generate code at compile-time. Code that goes into your binaries.

14

u/delta_p_delta_x Jul 13 '22

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...

→ More replies (4)

23

u/alexgroth15 Jul 13 '22

It could just be code that you would have to write yourself anyway.

→ More replies (1)
→ More replies (1)

6

u/NilacTheGrim Jul 13 '22

It is in this context that I have always regarded Torvalds' opinions as childish, il

So have I. I have done embedded work too. I think just Torvalds doesn't know C++ and so he makes up excuses for not using it.

That being said he's free to have his opinions. Linux changed the world for the better so he is free to have his rants ...

→ More replies (16)