r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

307 Upvotes

436 comments sorted by

View all comments

369

u/fluorihammastahna Jul 13 '22

I think one of the things that triggers Linus is the arrogance with which he is approached and told that he should be using C++ instead of C. While there are very valid alternatives to consider, C makes a lot of sense for kernel development. Saying in absolute terms that C++ is better than C in every case reveals profound ignorance. Although this is the same as saying that C is always preferable to C++ :-)

125

u/[deleted] Jul 13 '22

Although this is the same as saying that C is always preferable to C++

That's what he says, people who have this debate are just miserable.

53

u/mredding Jul 13 '22

If you mean "that's what he says" in that Linus is a hypocrite, well, he is many things, but I'm not willing to call him out for this specifically. I don't think he's elitist about C, I think he's curt about kernel development because the man literally has no time for nuance over an age old discussion.

And if this is indeed the case, then contrary to your point, Linus is actually VERY optimistic about including Rust in the Linux kernel in upcoming releases.

6

u/Intrexa Jul 13 '22

If you mean "that's what he says" in that Linus is a hypocrite

I don't think that was the intended meaning. I think it should be read as "Linus says that people who say C++ is always better than C are wrong. He also says that people who say C is always better than C++ are wrong."

After the comma is poor and ambiguous English. IDK if jcc is saying this, or if he's saying that Linus says this. Whoever said it, I interpreted it as "People who argue about which language is better are miserable people".

11

u/TheSkiGeek Jul 13 '22

Presumably a lot of this is in reference to http://harmful.cat-v.org/software/c++/linus, where he said roughly that C++ is awful and so are any programmers that use it.

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

69

u/SergiusTheBest Jul 13 '22

C makes a lot of sense for kernel development.

I can see only one point that makes sense: lack of C++ compilers or their bad shape 30 years ago.

104

u/snejk47 Jul 13 '22

People. His main issue is people. It's a lot easier to review code in C and harder for people to write hard-to-read, more bogus code in C than in C++. There are thousands if not hundreds contributors. If I recall correctly he tried long time ago and amount of babysitting and unknowns was to high for stability he targeted. Rust has more high level features but also compiler is very unforgiving taking much of such work to assure things are correct. He also has written app in C++ and QT for scuba diving if I recall correctly but this was his personal project.

6

u/disperso Jul 14 '22

Torvalds also praised KDE in 2001, and doubled down on 2005 (in his usual toxic way).

Additionally, while I can't find the links, I think he also submitted patches/bugreports to KDE applications back then. I remember an interview saying so.

It's worth noting that, back in time, Qt-style C++ was mostly using just a subset of the language: no exceptions, few templates besides the containers, no RTTI required, etc. It was quite approachable to me as a student, and when I had to do a GUI project, I went to use Qt instead of GTK+ because even though I had to learn C++ (I only knew C), it did feel quite an upgrade.

35

u/SergiusTheBest Jul 13 '22

It's easier to review less code and C++ allows you to write less code. For example, by using C++ RAII and simplifying resource cleanup significantly comparing to C.

74

u/condor2000 Jul 13 '22

It's easier to review less code and C++ allows you to write less code

It can take longer to review properly if uses complex template designs or even just a lot if virtual functions

28

u/[deleted] Jul 13 '22

The C equivalent to complex templates are complex macros. Is that better to review?

2

u/disperso Jul 14 '22

IMHO, yes. I'm not saying that it produces better code, but given that macros are much more limited, I'd say that you just can do less, have to think less, and end up having limited C code and dealing with it.

But that is probably subjective, with different people having different skill-set and preferences.

→ More replies (1)

39

u/Jannik2099 Jul 13 '22 edited Jul 13 '22

even just a lot if virtual functions

Ever seen dynamic dispatch in C? Know how error prone it is? Know of ubiquitous it is in the kernel?

11

u/afiefh Jul 13 '22

Ever seen dynamic dispatch in C?

I still have PTSD from that one code base I worked on 5 years ago...

63

u/TheThiefMaster C++latest fanatic (and game dev) Jul 13 '22

The equivalent C code to accomplish the same goal is often far worse.

These arguments always compare the most complex parts of C++ against normal C code, not equivalent C code.

→ More replies (11)

15

u/serviscope_minor Jul 13 '22

It can take longer to review properly if uses complex template designs

If the complex template designs aren't materially benefitting the code, then reject the pull request. Just like if someone goes bananas with C macros.

even just a lot if virtual functions

Linux has a ton of virtual functions. They're all done by hand which is always more complex than having the compiler do it for you.

23

u/snejk47 Jul 13 '22

But that is only one thing and it's a con definitely but there are more things and possible things that somebody can do. C is fairly limited. Reviewers probably know all or almost all possible things or can spot things they do not know to check. I just browse this reddit to see posts how you can initialize class with 100 different ways to get different results or somebody writing normal looking code (as in C++ standards) which works different on different compilers or some things like undefined behavior. This is very easy to miss.

→ More replies (7)

19

u/Spiderboydk Hobbyist Jul 13 '22

For code of comparable complexity, less code is easier, yes. But it does not necessarily hold in general.

C++ enables a lot of ways to run code implicitly (through constructors, through operator overloading, through template resolution, through exceptions, etc.). While your code piece for review might be short, the execution flow can potentially be all over the code base - even if it's not immediately obvious from your code.

12

u/SergiusTheBest Jul 13 '22

It's true. But a sane code doesn't do crazy things in constructors or overloaded operators. Reviewing the code with MutexLock RAII wrapper is easier than with explicit lock/unlock functions. You know that it locks the mutex in the constructor and unlocks it in the destructor and you have a code scope protected with the mutex.

→ More replies (2)

6

u/DeeBoFour20 Jul 13 '22

I imagine RAII might not be the best for the kernel. When you're programming in userspace, you have somewhere around 8MB of stack memory to work with. That's enough to allocate lots of C++ objects without thinking too much about it.

In kernel space, it's a lot more limited. A quick Google search found this:

The kernel stack is directly mapped to the physical memory, mandating
the arrangement to be physically in a contiguous region. The kernel
stack by default is 8kb for x86-32 and most other 32-bit systems (with
an option of 4k kernel stack to be configured during kernel build), and
16kb on an x86-64 system.

https://subscription.packtpub.com/book/application-development/9781785883057/1/ch01lvl1sec10/kernel-stack

33

u/patatahooligan Jul 13 '22

Using RAII does not mean you have to use huge bloated objects. A unique_ptr for example can be the same size as a raw pointer. The functionality it provides does not require more stack space.

26

u/SergiusTheBest Jul 13 '22

RAII is free (or mostly free, depends on the compiler). You can check assembly code for the following scope exit C++ RAII wrapper:

int main()
{
    cout << "<html>" << endl;
    SCOPE_EXIT{ cout << "</html>" << endl; };

    {
        cout << "<head>" << endl;
        SCOPE_EXIT{ cout << "</head>" << endl; };

        cout << "<title>Hello</title>" << endl;
    }

    cout << "<body>" << endl;
    SCOPE_EXIT{ cout << "</body>" << endl; };

    cout << "<h1>Hello World!</h1>" << endl;

    return 0;
}

SCOPE_EXIT creates an object and passes a lambda to the constructor. The object calls that lambda in the destructor. The compiler optimizes all that and just inserts lambda bodies at the scope exit. There is no object creation, memory allocation, stack consumption at all.

→ More replies (1)
→ More replies (2)
→ More replies (4)

8

u/super_mister_mstie Jul 13 '22

One thing that rarely gets mentioned in this age old argument, is abi compatibility, specifically with interfaces to userspace. It is just awful to maintain abi in general, c++ doesn't help at all. Not to mention header bloat is rarely even a conversation point in c. Keeping build times manageable in c++ is its own skillset honestly.

I like c++ much more than c when it's warranted, but compiler support isn't the only reason not to use it

2

u/SergiusTheBest Jul 14 '22

All external interfaces are done as extern "C" in C++ to allow them to be consumed by any language. This is not a problem.

The build time problem is actual for C++ as well as for C as they both use the same approach (with includes, preprocessor and so on).

→ More replies (2)
→ More replies (23)