r/cpp_questions • u/AnyDefinition2327 • 1d ago
DISCUSSION Why C++?
C++ is a general-purpose, performance-focused programming language used across many different fields, and I'm really enjoying learning it. However, I keep seeing a lot of experienced developers criticize it heavily. Linus Torvalds famously trashed the language, even calling it a "horrible language":
"C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much, much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C."
He basically called C++ developers idiots. Even though he made this comment years ago in 2007, to be exact, it's starting to make me doubt my choice. And it's not just Linus; other tech pioneers and plenty of people on Reddit talk negatively about C++ as well. Different people have different critiques about the language claims such as, it's memory unsafe, violates clean language design principles, and more.
So, are these critiques mostly aimed at older versions of C++, and did modern C++ (C++11 and beyond) actually fix these issues? What are your thoughts?
5
u/khedoros 1d ago
If you're looking for a language that doesn't have anyone respectable that hates it, you're going to be looking for a long time.
Torvalds talked about disliking the degrees and types of abstractions that C++ provides, reliance on libraries that limit portability and understanding of their underlying behavior, wrapping state in objects that you now have to manage the lifetimes of and deal with layers of little inefficiencies, like vtable lookups of virtual functions.
I don't think so. I think that modern C++ has made a lot of the things that Torvalds was complaining about easier to use...but also made the language itself more complex. More decisions, more abstractions, and a lower percentage of the language is used in any given project.
8
u/Xccelerate_ 1d ago edited 1d ago
Opinions are opinions!
C++ is not perfect, neither is the Linux Kernel. Just recently, GrapheneOS on X.com said bad things about Linux.
Elon Musk was praising Rust a few years back. Recently some code was written for something related to his rockets and it was written in C & C++ Why didn't he choose Rust after praising that much?? DYOR
When people like something too much they start to hate everything else. This is just that. There are so many ways that dangling pointers and UB can be avoided if one can actually write Modern C++ code, And not 1998's C with Classes.
There are so many ways a chef can burn or cut his hands while making food. It isn't the fire's fault if the chef is that much careless. Fire is fire. You touch it and it burns you.
The hate for C++ is stupid and beyond me. People are just content farming these days with hot takes.
The compilation speed of C++ is fast, file sizes are low and well optimised. C++ brings great abstraction with speed that simply no other language matches. Rust takes forever to compile. C makes you write too many lines of code compared to Modern C++ for trivial tasks. Modern C++ is a lot safer than C.
15
u/nithix8 1d ago edited 1d ago
there are only 2 types of languages.
one that everyone complains about and one that no one uses.
7
u/mredding 1d ago
To be fair, Linus is allowed to be wrong, and he also threw his most famous fit on the topic in ~1992; the language was still evolving and pre-standard. He himself was FRESH out of college - so what's the opinion of a college graduate worth, really? He released the Linux kernel v0.1 in 1991, and it was just enough to login to the university and read his email. No doubt it must have been perfect... He was operating off the benefit of the C89 standard, just a couple years prior.
I've been programming for 37 years and don't find his arguments valid. Neither does the industry - the various industries that write some of the most performant code on the planet in C++. Video games, trading, even the foundational Python modules are C++. Operating systems are written in C++, too.
That's true but so what? A lot of substandard programmers exist in every language. If C was the bastion of competent programmers, Linus wouldn't be infamous for his explosive reactions to kernel contributors.
You can write crap in any language. It's easy. I don't even know what kind of point he's trying to make, because there is none here.
Said the man who admitted he CANNOT FOR THE LIFE of him write a basic C++ program to save his own life... What the rest of us must do to accomplish our accolades must be a complete mystery to him.
You show me all these problems, and I'll show you a pile of shit C++ written by amateurs. I could show you PLENTY of amateur code by myself - I'm not a competent developer in dynamic languages and I have the humility to admit it.
It's fine so long as it's an academic exercise and these people learn. It's not fine if you draw your conclusions once in 1991 and sit on that for the rest of your life like you know something.
Yes, these issues have been adequately addressed, but DO understand that naysayers can never be satisfied. These issues had solutions in C++98. Smart pointers existed in Boost since 2000, and you could have implemented them yourself using late pre-standard C++. Believe me I know, I was there.
C++ is a language that gives you the tools to build the abstractions. You're not supposed to write code in terms of raw pointer, like BAD procedural C, you're supposed to build smart pointers, and then build your solution in terms of that. You don't use a raw
int, because when do you ever want just anint? It's always aweight, or aheight, or acount, or anoffset, or some other more specific type that's merely implemented in terms ofint. If you can't be bothered, then C++ isn't the language for you. In fact, I can't think of a language that is.13
u/earlyworm 1d ago
C++ is a wonderful language choice if everyone on your team has 10 years of experience with the language and is hyper-vigilant and meticulous by nature.
6
u/Sugarisnotgoodforyou 1d ago
I am far from qualified to being able to speak about C++ from an advisory standpoint, but what I can say is that for all the complaints that it gets, it offers high performance benefits for infra and latency-sensitive systems because it gives you direct control over memory layout, allocation, concurrency, and the compiled instruction stream. Don't get me wrong, it's not that other languages don't offer similar benefits with sometimes better trade-offs but a lot of large legacy codebases were written in C++ because that was the best option at the time. Learning it now offers you strong job market prospects just because it is largely used. If it was a different language for a lot of those codebases (say if a point in history made everyone move to Java or C#) then you would benefit more by learning something else.
There's a joke people often say "C++ is a horrible language, but there's no language better" or something like that. Don't worry about it too much and learn Rust on the side if it really bothers you in your spare time.
5
u/Discordbothost 1d ago
C++11 made it easier to show who owns an object with std::unique_ptr. Lifetime bugs are still possible. A reference into a vector dangles if the vector grows and reallocates. I'd turn on AddressSanitizer in your practice projects so a bad access points you back to the code.
4
u/alfps 1d ago edited 1d ago
C++ is a lot safer than C. Virtual methods provide safe downcasting (of the
thispointer). Standard library classes provide safe containers. Smart pointers provide some safety for memory management.newanddeleteprovide some safety for raw memory allocations and deallocations, which fornewinclude a guarantee that you either get an initialized ready to use object, or no state change and an exception that must be handled.Unfortunately the C++ exception handling, although it could, provides no way to guarantee that throwing an exception won't allocate memory dynamically. g++ does allocate dynamically, apparently with MSVC it can be avoided, but the language provides no guarantee. And this means that for some embedded programming environments where dynamic allocation is banned, exceptions are also banned, and that takes the safety belts off precisely in the environments where they're really needed...
Static checkers can alleviate the problems but not totally prevent them. Because a main reason why C++ is so widely used is that it supports doing some possibly dangerous things. Automatic safety comes at a cost that can be prohibitive. Hopefully in the future with C++ one may be able to opt in to automatic safety, and even have that by default. With current tools the default choice is still "the programmer knows what he/she's doing, just do it please".
4
u/KhyberKat 1d ago
TL; DR; C++ is different than 20 years ago, although it continues to carry baggage. Many older issues have been addressed.
We'll never hear the end of Linus's rant, but IMO he has valid points in context, and that context has a lot to do with the C++ programming environment at the time -- as you hint.
Around 2007 we were at a tail end of a decades long OO-mania and C++ was also going through growing pains. Its OO model was okay, but fell short in areas (e.g., multiple dispatch, managing deep OO hierarchies) and was tricky in others (e.g., multiple inheritance, covariant return values, operator overloading etc.). The OO momentum of the time pushed C++ development, both for the language and guidelines for use, into new, and, frankly annoying, territories - e.g., everything had to be an object or be part of a hierarchy, functions had to be very short, etc.
Templates and metaprogramming continued to grow and brought their own annoyances. Template bloat was a real thing, and many programmers just didn’t take to generic programming.
Exceptions had questionable efficacy. They introduced code overhead even when not thrown, impacted the heap when actually thrown, and languages like Java or C# had things like exception specifications -- something that C++ attempted, but not successfully.
Language and library support continued to vary among compilers, which discouraged people writing cross platform or widely distributed code from embracing new features.
In my experience, a lot of the negativity came from, and continues to come from, entrenched C programmers who didn't (or don't) what to learn an emerging technology. For this (small?) minority, learning C++ today is as daunting as when they avoided it 20 years ago.
3
u/Sbsbg 1d ago
Two programs written from the same specification, one in C and one in C++. I guarantee that the C program will have more bugs. It is simply harder to make solutions in C. The difference in bug count will grow on lager programs.
The problem in comparing C and C++ is that you compare typical C programs with typical C++ programs. No sane programmer would try creating a typical C++ program in C as that would be extremely hard.
3
u/conundorum 1d ago edited 1d ago
For context, Linus complained about it because a lot of inexperienced C++ devs were trying to make genuinely awful changes to the Linux kernel, and had no clue what they were doing, how the language actually worked, or how the kernel actually worked.
(E.g., they didn't understand that
newcallsmalloc()anddeletecallsfree(), and were usingnew&deleteinside the memory manager, in spots where you never callmalloc()because a failure can bring the entire OS down. And they didn't understand how exceptions work, or what happens when one is actually thrown, yet they were using them everywhere instead of trying to fit the C-style return error codes (and they were mixing exceptions with C return codes; you want to stick with one or the other, things get messy when they're mixed). Plus, they didn't understand what the C equivalent to their C++ code was, or where & how the C++ standard library uses the C standard library under the hood (which is crucial when trying to make a C++ component for a C code base).)There are parts of the Linux kernel that would see notable improvement if rewritten in C++, especially by a programmer that's well-versed in both C and C++ (so they can properly segue between the two, and design their C++ code to fit the pre-existing C code seamlessly), and knows how to combine "full C++" and "C with Classes" in a way that looks like pure C to the rest of the code base. (E.g., it's known that an error that made headlines for a little bit in the 2010s was caused by C-style
if-based error handling, and would've been prevented if the kernel had been designed to use C++ exceptions from the ground up... assuming the exception handling system (and compiler!) was written properly, to prevent unnecessary allocations and extraneous stack-based objects; some compilers (including gcc, the Linux compiler) don't know how to throw exceptions without allocating, which is really bad if the exception was because of a bad allocation.) The issue was just that most of the people who wanted to rewrite it in C++ were nowhere near skilled enough to actually write an OS, in either C++ or C.And apart from that, Linus tends to be extremely outspoken, and has a relatively short temper, so he's quick to insult entire languages if one or two programmers don't meet his standards. And one of his standards at the time was literally "all C++ is automatically bad because it's C++ instead of C", out of an old C-vs.-C++ rivalry that was kinda popular at the time. He didn't know how C++ worked, and some of his comments were flat-out untrue and showed his ignorance of the language. His comment would've been dismissed as just someone who tried to learn the language and ragequit, if not for the name attached to it. He's changed his opinion since then, after learning enough C++ to use it himself on a small project or two. He probably wouldn't let it in the Linux kernel, but he won't make blatantly incorrect rants about it now; the reasoning is valid now (it's similar enough to C that anything you can do in C++ can be done in C, he's not familiar with it enough to give it the stringent review that something as crucial as the Linux kernel needs, and it doesn't bring enough new features to the table to be worth using over C; notably, it has the exact same memory safety issues as C, but programmers seem less aware of the problems & solutions on average, which is why he allows Rust (which demands memory safety at gunpoint)).
(It also helps to understand that Windows, which is Linux's primary competition, and which Linus is very outspoken against, is written primarily in C++. So a lot of his mid-2000s C++ hate might have come from the vitriol he had against Windows at the time.)
Now, for the other issues... C++, ultimately, sees dislike because it's something that grew out of C, instead of being designed from the ground up as its own language. Nearly every original feature was designed with the express intent that it must be possible to implement that feature in C, because the first C++ compiler actually cross-compiled C++ code into C code, then ran it through an actual C compiler. And this piggyback compilation process both limited the language's early growth and forced certain design features; even now, it's still possible to treat C++ as a superset of C. (It isn't one, to be clear, but the similarity is great enough that it can be treated as one if you're willing to limit yourself.) As a result, a lot of its oldest features & library members are weird things that typically wrap around a C feature or library member under the hood (e.g.,
newexists to automatically construct what youmalloc(), and most if not all of the C++ I/O system either is secretly wrapped around C'sFILEpointer or has to act like it is). So, it can end up feeling "all over the place" as a result, and inherited a lot of C's issues.The most popular one to dunk on, by far, is memory safety. Now, C & C++ can be the most memory-unsafe languages, but that's what gives them their power and their performance. Every other language tries to shy away from accessing memory directly, and especially from letting you manage your memory yourself... but C & C++ just say "go for it", give you a toolbox, and step out of the way so you can do your thing. This has the potential to let them have both the best and the worst memory safety in the industry, and to even let the programmer adjust memory safety to the task at hand. (E.g., you can write speedy but unsafe code if you're writing for hardware that has built-in safeguards, or switch to safer code when it's possible for the memory to come under attack. You can also adjust the amount of resources used for safety, and their performance cost (if any), as needed.) Of the two, C++ tends to be seen as "more" unsafe because C++ is more complex, yet C++ programmers on average tend to be less aware of how to write safe code; this makes the language as a whole look unsafe, especially when compared to C (where every programmer was forced to learn memory safety in the trenches, with a box of scraps, or their code would explode; the pros didn't have the luxury of an OS that could try to handle safety for them). And the argument is biased in C's favour by a lot of very meticulously maintained code bases (such as the Linux kernel), which makes C look safer because we typically end up comparing C's best with C++'s average (and/or slightly below-average).
[It doesn't help that C has relatively simple memory management, and had decades to solidify its memory management rules. Meanwhile, C++ is more complex, largely thanks to class design in particular, and hasn't had enough time to solidify as a result; the rule of three/rule of five wasn't obvious at first, and a lot of safety issues are from people not understanding it. (Or understanding how it leads into the rule of zero, which solves many of these issues.) And type-punning through a
union, which was a common trick in C, is disallowed in C++; this forces people to use other mechanisms when you really do need to reinterpret data without copying it, and most of the tricks people turn to tend to be unsafe. The library mechanisms for type safety didn't initially exist, so people tended to roll their own; a lot of issues come from teensy-weeny oversights in the common pre-2011 solutions, and from people not knowing how to use the mechanisms we got in 2011 & beyond. And so on. Ultimately, the biggest issue was that for the longest time, C++ tried to be its own thing and step away from old C-isms, before it was ready to step away from the memory safety C-isms; most things were fine to do on its own, but it tried to cut the memory safety apron strings a few years too soon. Kinda like a teen that thinks they know everything, but isn't ready for the real world yet.]If you use it right, C++ (and C) is just as memory-safe as Rust, if not safer. If you use it wrong, it can be as leaky as a sieve. It's just that most people, or at least the ones that make the news, don't know how to use it right yet. (And we're all still trying to figure out what "use it right" actually means from a purely C++ perspective, without having to rely on grandpa C's guidance. We don't have enough "old hat master magi" pros yet, though C++11 & later have helped a lot.)
2
u/Hkiggity 1d ago edited 1d ago
Im sure their dislike has a linear relationship relationship to c++’s lifetime. As c++ adds more and more features. And gets more and more verbose.
But as you said, it’s used in many industries. That alone is a good reason to learn it. Especially if you want a job. I mean really, go find jobs that are strictly for C. You’ll be hard-pressed.
Plus, learning c++ will make you good at C. Especially if you have a data-oriented design mindset.
It’s not about not using C++ but understanding why some programmers dislike it. Which is mostly the overhead of virtual functions and inheritance and a plethora of features that aren’t entirely useful. Also compile time and other stuff.
2
u/FewEffective9342 1d ago
Cpp is lang with 40y of history. It will have some legacy baggage with the backwards compat requirements. So its harder and has more pitfalls. It lets you shoot yourself in the foot almost by design sometimes. And the compiler is not there to catch you. Fact is that there is tons of stuff written in c++ and its popularity is that its very (basically seamlessly) compat with C, and that lang has even bigger code base. There is a bit of history and proper moves (making c++ fit with C) with cpp which equals big business with lots of inertia. Whatimeanbythat is that there are lots (gazillions lines) of code in cpp, tried and tested, maintained as is without any end of the world, so the world will keep spinning with cpp no matter how many hissy fits and anger management sessions Torvalds has about it. He is not putting any money where his mouth is. Take what you want from it, and yes, one of the things you should take is that business dont run of the most refactored and sane straigtfwd latest code solutions. Is runs on what it runs on and the first giant there was c, then cpp capitalized on that (look at what cpp was initially sold as: c with classes, it basically compiled to c and then to asm and bin) and gained lots of adoption quickly in existing infrastructure.
All the new "non-compat with anything, all esle bad, we the best bc we started from scratch" mindset langs like rust will also have their fair share of backward compat compromises in 40years, with the only distinction from cpp that rust wont have similar vast adoption bc of its uniq snowflakenesnes and by that time there wil be some other optimostic c/cpp/java/rust killer langwith modern commodoties which rust will not be able to have, bc of now its backwards compat requirements. But it wont even be a point to argue whether its bad or not bc rust will be just dead (if it wont get adopted as properly in the industry). So the "prominent" folks having shit fits about cpp is mainly bc they cant inherently do nothing about eliminating it, for them it seems unnecesaary complex, they have modern better (easier) alternatives, but they dont have the capital to halt al the businesss until they rewtite it all in rust or whatever pop lang they have nowadays.
Cpp wont make u stupider. In fact mastering will give you fair advantage and good undestanding about vast techniques and approaches. E.g. some javascript bois tried to sell me some typescripts generics or whatever like something mind blowing and out of the ordinary... meanwhile i have templates and sfinae available like from before i was born. Cpp is equiv of "simpsons already did it" in compsci world, if you know it, i have your foot in the door in lots of places.
2
u/no-sig-available 1d ago
So, why are you asking the idiots for an opinion? :-)
3
u/phylter99 1d ago
You’ll find that everyone has an opinion and you’ll find that developers are very strongly opinionated. C++ has a strong history and has been used to develop a lot of software. You don’t get there with a bad language.
Modern C++ is quite good. It’s complex but there’s a reason it is.
Now I’m saying all this, but also consider that C++ hasn’t been my primary language in many years. I tend to write C# or Python. My opinion of C++ hasn’t changed though and with newer additions to the language it’s only become better with age.
2
u/Rafadazdoit 1d ago
Most “idiots” have moved onto Python btw.
1
u/me94306 1d ago
I've taught introductory classes in C and C++. When I finished a semester class in C, I felt that I had given the students a good foundation in the language. C++ is a much more complex language and at the end of the class, I felt that students were far from being prepared to be competent C++ programmers. Where Bjarne says that it is used by "substandard programmers" he is referring to people who took a ten week class, like I taught, and believe that they are experts.
C++ has grown by acretion. As problems were identified with how the language was used, new features were added to compensate, which introduced their own issues, which led to more features; repeat for decades.
C is a language that with some effort one can develop a comprehensive understanding of its features and flaws. C++ in far more complex and the same understanding is extremely difficult if not impossible to develop.
1
u/no-sig-available 1d ago
No, according to Linus, all C++ developers are substandard, forever. If you fall into the trap, you will be too. /s
1
u/United_Camel5250 1d ago
you would got different anwsers in different subs.
1
u/Independent_Art_6676 7h ago
being famous lends a little weight, but in this regard Torvalds is just another dev with an opinion. And he was possibly having a bad day or something... if he had stopped to think, he would have realized that the vast majority of problems caused by bad c++ coders are because those coders came into C++ knowing C and brought their trash with them.
0
u/fortsnek274 1d ago edited 1d ago
Yeah, it is! The syntax and excessive back compat. Let's get rid of those C-style arrays.
But there are good things. Like destructors, containers, algorithms, more type safety... and reflection soon enough.
But the point of contention right now is *provable safety. Which is why it's C++ vs Rust and not C++ vs C.
-3
u/Kadabrium 1d ago
C++ is a toy, educational, classical and liturgical language. Use it as you would use classical Latin. For real world communications, it isn't practical.
0
u/[deleted] 1d ago
[deleted] — view deleted comment