This is directed not just at the above commenter, but I invite replies from anyone who takes the effort to comment passionately against C++. Perhaps if we can understand some commonalities in peoples grievances, we could go back and address them.
I've used C++ for around a decade, both professionally and as a hobby, in various domains. I use it because I either need (or merely want, in many cases) the control over the resulting end product that it provides.
The grievances are well known, and most are being addressed in some form, so asking random commenters isn't going to change anything.
But for what it's worth: it just feels like the language is a lost cause.
Everything is so irredeemably hard (without breaking back-compat)- from syntax/name resolution/template expansion, and the accompanying problems with error messages and tooling; to build systems and dependency management, and the impossible task of ever getting anything standard there; to memory+thread safety, where every solution is a partial bandaid and there's no light at the end of the tunnel.
It's a big, important, widely-used language. It's just not pleasant to work with- and while that can and will improve, it will always be tied to its rather painful legacy.
I spent maybe ~15 years writing C++ over hobby + career timespan. Was a huge proponent of the language but spent the last 2-3 years over in Java land while doing all my greenfield work in Rust.
Came back to another lower level project written in C++/0x14 lately and it's a bit of a shock how my things I 'forgot' even on a modern codebase. Things like implicit constructor conversions, the fact that the type inference is pretty weak still(why can't I just construct unique_ptr w/out the type when I pass it direct[1]). Headers and package management is a total mess.
At this point unless I've got a hard constraint(like QNX or other yet supported platforms) I'm going to reach for Rust just about every time.
[1] Yes I know make_unique() is a thing but it's a C++/0x17 and just patches one part of type inference issues.
Even JavaScript which kind of felt like a lost cause to me about three years ago has redeemed itself - I was shown a "way to code JavaScript" which exclusively uses ECMA6, keeps very little if no state, and basically everything is a `const fn (...) => {}`. A breath of fresh air in its simplicity and safety. C++ doesn't seem to want to converge to this sort of thing, even as usable subset of the language.
I would say the best quality of C++ is that you can be reasonably certain of what the program is doing at the machine language level (though obviously C is better) but this is going to be overtaken by languages like rust, which have more sophisticated type system and unlike VM languages (which definitely have their place) leave safety considerations in the compiler.
I used to be really passionate about C++ back in the days, but it's just too damn complex. Yes, some new features really help; if I could bring one to a desert island it would be lambdas. Tuning C++ these days is mostly trial and error for me, there are so many gotchas to keep in mind that it's more or less impossible to juggle it all mentally.
From my experience there comes a point where the required effort tips the scales towards more convenient languages for most projects. But by the time you realize you're usually up to your neck in template goo that no one wants to translate.
Have you seen Terra? http://terralang.org/
The auto specifier[0] is frankly the most ergonomic and simple thing which I wish I could have in, say, C.
[0] https://en.cppreference.com/w/cpp/language/auto :: auto specifier (since C++11)
Having worked with C++ for over a decade, I much prefer C. C++ certainly has its useful features, but when I use C I find that I don't miss them much; and sometimes, especially when debugging, the "implicitness" of various things like automatic constructor/destructor calls are better made explicit.
As a rebuttal to the title of this video: "Hidden complexity is NOT simplicity."
There are hidden costs to technology choices. I work with a smart guy(also a C++ zealot) who loves CMake and constantly evangelizes it. CMake seems great but the problem is that it's yet another language/system which people need to learn to be productive in our environment. GNU Make has its problems, but I can also scoop up a dev off the street and there's a 90% chance they've had experience with GNU Make and can immediately understand what's going on. Now I'm not saying CMake sucks, it's got a lot of great features, but by using it you gave up something without possibly even knowing that you did. Simplicity is a feature. Banality is a feature. Unless you're getting something good in return, don't sacrifice them.
It's nigh impossible to write correct C and keep it thay way over the project's lifetime, C++ is too complex and so is Rust.
I think that's why Go is so attractive, it really is reasonably simple to wrap one's head around.
I don't usually write negative comments about C++ but I sure sympathize with them.
I write C++ at my day job (and have for years). My team's codebase is in its teenage years. It's code-reviewed and has decent test coverage, but in many cases the author doesn't know C++ as well as the reviewer, and the reviewer is rushed / trying to not be a huge bottleneck, so stuff slips through. New code gets written to mostly C++11 standards (soon, mostly C++17) and the newest company practices. Old code...still exists...and the new code still has to use it. We've modernized some pieces but not as much as we'd like. Company-wide people do a pretty good job of modernizing a lot of the libraries we use, but my team hasn't kept up as well for various reasons.
I hate memory errors. They're super hard to track down. I've spent significant time doing so. Thank god for ASAN and TSAN at least.
I also don't like crashes in general; my service has to be quite reliable (some operations are critical to big parts of the company and have a 99.9995% success within a certain deadline). And fast, so I can't just rewrite it in Java or Go.
A few examples of the sort of foot-guns C++ has:
* Data races: no equivalent of Rust's Send+Sync auto traits.
* string_view/reference/pointer outliving their backing memory: no lifetime annotations. Even the new proposal for them is very incomplete.
* std::optional and equivalents dereferenced incorrectly: no pattern-matching syntax, the decision for the most obvious way to deference (operator*) to be undefined behavior instead of a crash.
I wish the whole thing were written in Rust. Then the parts I have to pay super close attention to would all say "unsafe" in them. That covers memory safety (including data races). I'd also know to pay pretty close attention to "expect" and "unwrap", which cover most (not all) panics. Of course, more mundane logic errors can exist in any language, but freedom from those problems would be a huge relief.
I agree with this assessment of the C++20 lifetimes stuff. It's great, it will probably catch a lot of bugs, but it doesn't catch the worst ones and that's where you need it the most.
* CPU time per request: we do more work than we used to on average. For example, we do more asymmetric cryptographic operations now, and since introducing those operations we've increased the lengths of the keys. CPU time per request contributes not only to cost but to user-facing latency.
* overall request rate: we handle >256X as many requests as we did 12 years ago.
Those reasons are particular to my service, but I'd say the continued usage of C++ is convincing proof that plenty of people are willing to go to considerable effort to improve latency and efficiency. It'd be a lot easier to write things in a higher-level, GCed language otherwise.
> We introduce a new approach for implementing cryptographic arithmetic in short high-level code with machine-checked proofs of functional correctness. We further demonstrate that simple partial evaluation is sufficient to transform into the fastest-known C code, breaking the decades-old pattern that the only fast implementations are those whose instruction-level steps were written out by hand.
> These techniques were used to build an elliptic-curve library that achieves competitive performance for 80 prime fields and multiple CPU architectures, showing that implementation and proof effort scales with the number and complexity of conceptually different algorithms, not their use cases. As one outcome, we present the first verified high-performance implementation of P-256, the most widely used elliptic curve. Implementations from our library were included in BoringSSL to replace existing specialized code, for inclusion in several large deployments for Chrome, Android, and CloudFlare.
Could you use session keys to take some of the load off, exchanged using asymmetric crypto, or are you already doing that?
Also, cool thing about Rust is that you can start writing parts of a C or C++ code base in Rust, and slowly switch over. This is exactly what Firefox is doing with their browser. Could you convince your company to let you try to rewrite a small part in Rust?
And no matter how much I'd love to, rewriting in Rust at work is not gonna happen any time soon. There's tons of support built up for the major languages in terms of build tools, IDEs, code cross-referencing, automated refactoring, style guides, libraries, etc. It's understandably a difficult process to get a language added to that list.
Besides, my team struggles to find time to just modernize our C++. I wouldn't want to start converting to another language until I find time/staffing to actually commit to carrying through. And I know from trying it at home that rewriting a project in Rust takes me longer than expected and bindgen's C++ compatibility is less than perfect.
So I get my Rust fix with personal projects instead (when I can find time).
I prefer primitive rather than clever (i.e. pompous) solutions to problems and therefore choose C.
I also do not want to spend a significant portion of my time in the hallway discussing language features, as I hear many C++-choosers doing outside my office most weeks.
From this perspective, I could only be made happy with C++ if features were removed from it. I admit that I’ve hardly given the language a chance, but that is partly because I know I’ll make solutions even more pompous than the ones I’ve seen. I don’t think humans can help it.
Incidentally — in principle, most of this talk is applicable to _any_ software engineer. It's just that she's coming from a C++ point of view, and talking at a C++ conference. It's definitely worth a watch.
For example, in template metaprogramming we can get rid of tag dispatch and SFINAE thanks to static asserts, constexpr and if constexpr.
Likewise you might decide to shot your code with C style arrays, or use the STL data types with bounds checking enabled instead.
I feel a lot of that probably isn't conventionally achievable since C++ covers a lot of situations with different needs, but if 20% of the functionality could cover 80% of the needs at some point I think it'd be great for the language.
One thing to remember though is that there are two things at play here. The library writers tools, like TMP, and the users. The users can do a lot and never worry about SFINAE/tag dispatch. The library writers use it to allow the users of the library to have a potentially very simple experience.
Also, Greenfield C++ can be way different than pre 2011 C++. The cost of things that people would work around has gone way done to zero in some cases. Like returning a value from a function is like constructing it in place at the caller. So that takes away a need to use the heap to allocate it or to copy it when you return it.
See https://github.com/isocpp/CppCoreGuidelines/issues/1038 for an example of what I'm talking about.
So, readability increases, but the mental load of making sure that stuff doesn't break in subtle and non-euclidian ways keeps increasing, at least when refactoring existing codebases.
I do know what you mean. But that's not really the languages problem but of the coding guidelines and using code review to maintain legibility.
I bet they could actually pull off a break with old code if they would release tools, ala Abseil, which took care of migration for you. I think it's reasonable to accept that legacy code needs to be freshened up every so often to allow the language to evolve. We have Google translate for human languages, why don't we have it for code? It seems like a far easier task.
Things like meta-classes will drastically simplify end-user code.
C++ makes programmers become Icarus. Once they escape from the prison of C syntax, they try to fly higher, towards the goal of code that looks like Python but runs like C. They learn that template metaprogramming can help acieve this goal, so they use it. But template metaprogramming is actually the sun, melting their code into a pile of wax and feathers, sending them crashing back down into the ocean to drown.
Concepts are one of the most important features C++ needs.
When the performance is needed, the burden of using C++ seems like a small price to pay[0], but a lot of the time, the expressiveness offered by C# or Java, etc. is entirely sufficient. Which is why "enterprisey" software is dominated by Java or .Net, and why certain software like Autodesk Inventor 3D is written in C++.
[0] Which is why browser engines, etc., are written in C++. Maybe one day Rust will replace C++, but for the time being, C++ is the top of the abstraction/performance pyramid.
Note that for the above to apply you need have a domain where there will be enough uses to make the generic version worth it.
Of course, just like it is fun to write code for the IOCCC, there is some perverse fun in writing complex templates.
That you can use templates for something else is almost entirely useful only if you want to win something like the IOCCC.
With the "Curiously Recurring Template Pattern", you affect behavior from the bottom up and from the top down.
To be honest, I don't know if it has value for most tasks people care about (outside of libraries, I mean). But it was fantastic for optimizing OOP-abstracted desktop applications. If you have some subset of functions that need to be called a lot, then avoiding virtual functions really helps.
That said, almost all of those nicer variants do come with some added runtime costs. The general problem of making efficient generic code, without a bunch of boilerplate, and with a nice syntax, seems fairly difficult.
Sometimes. Other times, templates are simpler.
> I have the impression that people use it because they can
I think people use template metaprogramming because they see it in standard library, but they don’t understand the requirements for C++ STL were very different from the requirements of the code they’re personally developing.
I use C++ templates for following reasons.
1. For data structures such as custom containers.
2. For zero-overhead polymorphism. E.g. when I write algorithms that must work exactly same way with floats and doubles, or with char and wchar_t strings. Or for zero-overhead lambdas, putting them in std::function causes virtual function call overhead, lambdas are usually inlined.
3. For zero-overhead constants. There’s constexpr in modern C++, also modern compilers are good at propagating constants, but still a template argument is more reliable.
When I need complex metaprogramming, I often use other tools to generate the C++ code I need. On Windows it’s usually T4 templates with C# inside them, on Linux usually python. Compared to C++ templates, both T4 and python have much better support in IDEs (visual studio / PyCharm), including debuggers.
While it will never match the simplicity of Lisp, it is easier to get C++ accepted into most companies than Lisp.
And if one has access to a C++17 compiler, many tricks can now be replaced by more developer friendly constructs.
In Lisp the same error would be a runtime one.
And who knows, maybe by C++23 some form of reflection will be finally available.
If you mean ML/Haskell, then I agree.
If you mean other languages filling the same void then I don't. Idiomatic C++ uses templates: They're objectively safer than what preceded them, for one.
Speed.
Printing hello world might seem easy, now mastering Python....