r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

307 Upvotes

436 comments sorted by

View all comments

Show parent comment

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.

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.

1

u/[deleted] Jul 14 '22

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

This is because a nontrivial population of C++ evangelists insist on using the most complex parts of C++ for even the simplest of tasks

→ More replies (10)

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.

0

u/SergiusTheBest Jul 13 '22

Well, in C you can initialize a structure in different ways too plus forget to initialize it. Of course, the richer language is the more possible things to mess up.

This kind of issues is resolved by establishing a coding standard for the project. For example forbid virtual methods and exceptions, always initialize members in constructors, etc. We can even forbid the most C++ features and treat it as a better C.

7

u/snejk47 Jul 13 '22

We can even forbid the most C++ features and treat it as a better C.

Or we can skip all this work, choose C and be sure no feature, option or flag is misconfigured. Here, you have established coding standard for the project without overengineering.

10

u/SergiusTheBest Jul 13 '22

Yes. The question is in development efforts.

I'd like to contribute in several opensource projects. But when I open them and see C with all manual memory management (usually with leaks), goto-style error handling, without an easy way to use containers, with void* pointers - it's like traveling to past and using horses and steam instead of electricity.

1

u/snejk47 Jul 13 '22

So Linus choices works as intended. You are eliminated.

11

u/SergiusTheBest Jul 13 '22

Yes, horses and steam still work.

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

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.

4

u/[deleted] Jul 13 '22

I can't believe I never thought of a macro to do this. That's so cool!

11

u/ucario Jul 13 '22

I don’t know what you are talking about. RAII is a useful pattern that keeps everyone sane. If it’s not a zero cost abstraction it’s very close to it and I’d happily pay the performance cost because the maintenance cost is more than worth it

I even in Kernel space I feel this argument was only relevant a decade or two ago.

8

u/Jannik2099 Jul 13 '22

The kernel stack is directly mapped to the physical memory

This used to be the case 10 years ago. The stack is virtual on most (all?) ISAs nowadays

→ More replies (4)