r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

308 Upvotes

436 comments sorted by

View all comments

Show parent comments

12

u/alexgraef Jul 13 '22

Not sure if that answers your question - but C is basically just cross-platform assembly. That was the original intention, and to this date, what you can do is mostly pointer manipulation/arithmetic. Structs for example are just a fancy way for doing pointer arithmetic.

This all means that C code is very deterministic, regarding behavior as well as timing. Well suited if you want as little abstraction from the hardware as possible.

12

u/no-sig-available Jul 13 '22

Structs for example are just a fancy way for doing pointer arithmetic.

This all means that C code is very deterministic, regarding behavior as well as timing. Well suited if you want as little abstraction from the hardware as possible.

And C++ also has structs. :-)

So you can use the "little abstraction" when you build the hardware interface layer. Then you can add more abstractions when you build the next layer. You don't have to be close to the hardware all the way, all the time.

1

u/alexgraef Jul 15 '22

That was not the point. As C is a subset of C++, you can generally do everything in C++ that C would allow you.

However, C does not implement vtables for structs. And classes aren't just "a fancy way for doing pointer arithmetic". There's a lot more going on in the background.

2

u/no-sig-available Jul 16 '22

There's a lot more going on in the background.

No, there really isn't. And that was my point.

There is no vtable unless you add virtual functions to the class. And why would you when that is not needed?

You can have private members of a class (and a C++ struct). That is an added abstraction that has zero runtime cost.

In C++ you can have overloaded functions, so std::abs works for all types. No need to choose from abs, labs, llabs, fabs, fabsl, fabsf, cabs, cabsl, cabsf, imaxabs. Again an abstraction with no runtime cost.

Using C++ doesn't doesn't force you to immediately use multiple inheritance, dynamic allocations for everything, and make every function virtual.

→ More replies (2)