156 points · by zerofrancisco · 6y ago · 246 comments
That said, the one thing that makes C++ better than other languages is the destructor. You can see everything else added since as making destructors more useful.
(I am old enough to tell you to get off my lawn. And have one.)
RAII is associated most prominently with C++ where it originated, but also D, Ada, Vala, and Rust.
https://en.wikipedia.org/wiki/Resource_acquisition_is_initia...
Topical sidenote D's got destructors as well:
If you think other language X supports RAII, you don't really understand it. It's not really compatible with obligate-GC. Finalizers, in particular, are not it.
The thing about destructors is that reclaiming memory is only their most trivial use. In C++, as in D and Rust, and Haskell, too, catching errors is the type system's most trivial use. It does the heavy lifting.
If your program isn't putting the type system to work, you're toiling away with a screwdriver while people around you are using impact drivers and forklifts.
With C++ RAII you can have a brain fart and forget about freeing a resource, but you're still OK.
Disclaimer: Not a language lawyer, just a fan.
C# has "using", Java has try-with-resources, Python has the "with", etc. - all of which support RAII patterns.
std::unique_lock<std::mutex> foo();
The unlocking of the mutex is then tied up with what you do with the returned mutex locker e.g. if you ignore it then it will be unlocked at the end of the current statement, if you assign it to a local variable (and don't move from that) then it will be destroyed when leaving that enclosing function, or you return it from that enclosing function to extend the length of the lock still further.You also can't compose those mechanisms so easily. For example I could make a class with a std::unique_lock<std::mutex> and a pointer to the data it protects and pass that around.
RAII is strictly more powerful and more regular than context managers.
[1] if you are implementing something like a container and dealing to raw memory you might need to call destructors of subobjects explicitly, but because it is needed to correctly reclaim memory, it is almost always done and all standard containers do it already. On the other hand in python list doesn't implement the context manager protocol.
No, exceptions are not the answer. NONE of the C++ projects I've worked on in the last 30+ years have used those in production. Exceptions are far, far worse trap.
It's funny that the teams across half a dozen companies I've been in have independently come up with just about the same set of guidelines about C++ features. It's almost like some of those features were bad ideas. :-)
On the other hand, this is exactly the problem that arises if you refuse to use exceptions, because then the constructor has no way to signal that it failed. Since constructors, by their very nature (acquiring resources, validating initializers etc), can fail, you have to resort to "is it valid?" flags, and your API clients have to check those flags or risk unspecified behavior.
Apple seems to think that anything that you put in dealloc should end up in the system (like unsubscribing from notifications).
C++ is a dangerous, powerhouse systems language. I think it really needs things like destructors, and I’m glad to see its continuing development.
Not as safe as proper RAII but very useful at the site of declaration to write scope(exit) close(file); or similar
Not quite. The syntax has become even more weirder and the language more complicated. I really do think that the standards committee have gone overboard adding in everything and the kitchen sink. The pace of change has been crazy for such a old and widely used language. The standards committee should just disband for a couple of years and go take a rest instead of adding in everybody's "favourite features" :-)
> The pace of change has been crazy for such a old and widely used language.
Not necessarily a bad thing, at least you can request specific standards at compile time so you can stick around at whatever standard you want and it should work for a very long time (there's still C++03 and older out in the wild), and those who want the features in the latest standard can upgrade.
The good thing about the STL additions is that you can simply ignore them, as they are not part of the core language, just like people have ignored iostreams since the 90s.
I think a lot of things were added in just to "get away" from C roots and make C++ its own language which IMO was a bad idea. For me, C++ is first and foremost a "better C with Classes" because i can keep that subset of the language in my head easily.
PS: Relevant other HN discussion: https://news.ycombinator.com/item?id=24649992
However it doesn't matter how many good parts C++3000 happens to have, if some crowds insist in writing C with C++ compilers.
Not specific to C++, but I'm definitely loving the addition of coroutines to the language. It makes multi-tasking a lot more clean especially for something like userspace networking.
I consider myself quite proficient at C. I wrote quite a bit of C++ from 2011 to 2014, while I was ramping up at programming. C++11 wasn't really a thing back then in my experience. The new features are really nice, but the syntax truly feels alien to me.
I am now looking forward to learning Rust, which seems more cohesive in general, and less of a bag of weirdly shaped features. Meanwhile, I'll probably keep on using C++ as "C with classes" when I have to. And I feel like codebases have to pick a subset of C++ they are comfortable with to be approachable to outsiders.
What modern C++ really needs is a rebranding. Something like "C22"/"CX"/"Z" to distinguish itself from old C++. Together with a guide on what the safe features and "unsafe"/deprecated features are the developers could get new certificates to prove that they are up to date. Companies/management could easily require that the new C++ is used and developers are actually capable. (This organizational issue is IMHO the most important part.)
https://docs.microsoft.com/en-us/cpp/code-quality/using-the-...
but yeah, there is more to that and I doubt C++ can be fully unsafe unless you set references and pointers (they can be invalidated) probably many other things as unsafe. c++ is unsafe by design
It’s a puzzle. One can only conclude that secretly C++ programmers like all the new feature whilst complaining bitterly about them in public.
If you're in the former group you'll be delving into all the template nitty gritty. If you're in the latter group you'll rarely need them (I think our 200k+ LOC C++ codebase uses std::enable_if three times).
I still like the language though. There's no obligation on any C++ dev to use all the new features.
Personally, I find Rust's syntax to be quite ugly compared to C++'s; horses for courses I suppose.
The language is almost totally unprincipled in my opinion so you just have to know the minutiae to reason about and work with novel constructions.
In the hands of an expert, it's an awesome tool. To me, it's a miserable chore.
> I am now looking forward to learning Rust
Prepare yourself. I personally think one of the major downsides to learning Rust is how quickly the language is moving.
I first started looking into Rust in ~2015, but it seems most hobby projects from that time won't even compile today.
It feels like there are non-breaking changes to the language almost weekly. I still cannot figure out if I should be using nightly rust, stable rust, or switching back and forth depending on the project.
Overall I think Rust is doing a lot of great things, but I'm not sure it is handling the "mess of features" aspect of programming languages particularly well.
Nah, couldn't be! It's just C++'s fault, surely!
Do you think anyone dropped into a foreign code base of Java 15, C# 9, PHP 8, Python 3.7, C2X will be able to understand all of it?
2 major language revisions have been released since this was recorded.
for(const auto& item : dummy_list) { ... }
It was fucking ugly and verbose use iterators for something so basic and simple.
std::sort(my_container.begin(), my_container.end());
I mean, yes, in 0.1% of all cases I would want to sort something that is not an entire container, and for that it's great that this exists. But for the 99.9%, why isn't there an overload that would allow me to do: std::sort(my_container); for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
std::cout << i << ' ';
}
notice the `for (int i:ints)` and the pipes filters.EDIT: more specifically, you can use sort on a range now (source : https://cppreference.com):
ranges::sort(s);[0] https://www.boost.org/doc/libs/1_72_0/libs/range/doc/html/in...
Documentation here: https://en.cppreference.com/w/cpp/ranges.
They are called "range adaptors" (weird naming IMHO).
template<typename T> void sort(T& t) {
std::sort(std::begin(t), std::end(t));
}What would otherwise be a trivial one liner with something like transform suddenly turns into a 3 line call
I remember talking with one of the world best HPC expert and he said that it's the most advanced language today when it comes to optimisation. Don't know if that still applies.
A question while we are at it:
// Am I the only one left on this planet using this style of bracketing:
void foo()
{
cout << "brackets like that are more readable!" << endl;
}I wrote quite a lot of HPC-like code lately. Can confirm.
The reasons include first-party support for hardware intrinsics on all platforms (SIMD is the most important, but also scalar stuff like popcnt, pdep/pext, etc.), full control over RAM layout which allows to optimize data structures for cache friendliness, and the ecosystem of high-quality third party libraries.
BTW it was him: http://www.theochem.ruhr-uni-bochum.de/~legacy.akohlmey/inde...
I also write a bit of Rust and I find Rust's nagging me to use the "default" brace style annoying.
IMO this is very much a personal taste issue, it depends on how someone grew up learning to read, what programming languages they grew up with, etc.
This is from 2014 so although it's useful it doesn't quite qualify. The other sources I can find are either even older, or they're tutorials about very specific features with a very narrow scope.
Bjarne Stroustrup's "A Tour of C++" (2018) is also frequently recommended.
I haven't worked with C++ for years, and it's nice to see there really has been some progress improving some of the C++ parts.
That article covers the big four new features:
- concepts
- modules
- coroutines
- ranges library
It mentions that modules will improve compilation times. How are compilation times these days? That was one of my least favorite aspects of C++ back in the day.
(scnr)