4 ms·
It is a diservice to C++, to paint RAII as modern C++. We were already making use of RAII in C++ compilers for MS-DOS during the C++ARM (the C++ equivalent of
by pjmlp 1y ago
It is a diservice to C++, to paint RAII as modern C++.
We were already making use of RAII in C++ compilers for MS-DOS during the C++ARM (the C++ equivalent of K&R C book), one such example would be Turbo Vision C++ variant and Borland International Datastructures Library (BIDS).
If anything, it is a pity that 30 years later, it is still something we need to educate people about, as it isn't as adopted as it should be.
EDIT:
If this is supposed to be Modern C++, at the very least provide C++23 example in 2025, instead C++ from 2011.
Meaning import std, using a custom deleter in std::unique_ptr (it doesn't manage only heap types), making use of =default to get back compiler generated member functions, as it would do the right thing in the example for the fd handle.
- grues-dinner 1y agoModern C++ does give you move semantics, which makes RAII a lot more expressive, especially with smart pointers. One things about RAII that I find is that, a bit like inheritance, it's actually not something you have actually do nearly as much as the emphasis it gets in the first chapters of all the books implies. How often do you actually write a move constructor or even a destructor in workaday code? Most of it is done for you by the stdlib (unique_ptr, say) or libraries. Of course it needs to be understood, and understood well, but it feels like landing a plane: you have know it well and it's not unusual and certainly not wrong, but also flying is mostly doing things other than landings.
- tialaramex 1y agoThe C++ 11 move is terrible because it's not the move semantic people actually wanted - which is what people took to calling "destructive" move, the feature Rust provides - instead it's this weird confection where we're scooping the guts out of one object and transferring them into a new object, leaving something hollow behind in order to satisfy the requirements of 1980s C++ programs. In some cases this can be optimised to the same machine code, but not always, and semantically it's not what we actually wanted anyway. In terms of RAII this means arranging that after the move the "moved from" object remembers it no longer has any resources and so when it is destroyed it won't clean up - much easier to just design the language properly.
- ninkendo 1y agoRAII, like a lot of C++ concepts, finds the most use in library-like code (stdlib especially), where it needs to be super generic. Application developers get the benefits of these concepts by simply using these libraries, even if they don't have to care about them themselves. As an application developer, you mostly focus on classes as plain-old-data and functions to work on them, so you don't need to mess about with custom destructors or move-constructors or other RAII stuff.
- stingraycharles 1y agoThere’s still a huge part of the industry that writes a mix of C-style C++. Modern C++ is all about template metaprogramming, concepts, move semantics, ranges, etc. But there’s a significant cognitive load will all of that.
- bluGill 1y agoThe term modern C++ means RAII. Those other things are common and sometimes useful in new code that can use them. However anyone who says 'modern C++' is talking about RAII not those other things.
- pjmlp 1y agoAlready possible in C++ARM, as any look into 1990's C++ frameworks documentation on digital archives would prove, regarding RAII. Or even articles in The C/C++ Users Journal, Dr Dobbs, PC Techniques, Microsoft Systems Journal. Unfortunately many that write C with C++ compilers, only started paying attention around C++11.
- bluGill 1y agoThe term Modern C++ predates C++11. C++11 gave us move constructors which makes RAII much more powerful, but we were doing it for a long time in C++. Well some of us were doing it, there are a lot of people who are writing the type of code that gives C++ a bad name (even today)
- stingraycharles 1y agoIsn’t “modern” a term that’s relative to the current time, rather than a fixed point in time? Like, modern architecture, fashion, cuisine all change over time. Why would “Modern C++” refer to a fixed point in time somewhere in the 90s?
- green7ea 1y agoAuthor here, what you say is entirely correct — I didn't emphasize enough that this was all possible before in a more limited way. Those parts were cut out to try to make the article more focused on understanding RAII. The modern parts covered in the article are the move semantics making RAII more expressive and the standard library embracing the concept throughout. I think the idea of modern is to convince everyone to give these 'new' techniques a try if they aren't already using them.
- pjmlp 1y agoIt is always a good thing to spread knowledge, however when C++11 is modern, what C++14, C++17, C++20, C++23, C++26 are supposed to be? There are already plenty of memes on C++ conferences regarding that.
- einsteinx2 1y ago> what C++14, C++17, C++20, C++23, C++26 are supposed to be? Postmodern C++?
- pjmlp 1y agoThat would be C++14, if C++11 is the modern one.
- green7ea 1y agoI would see that C++11 started the modern C++ era and the later standards have refined and perfected it. I would be very sad to have to go back to C++98, it would feel like a different language — I would only be inconvenienced to go back to C++11. Maybe ranges changes that ;-).
- motorest 1y ago> It is a diservice to C++, to paint RAII as modern C++. I think you're missing context. The term "modern C++" is used to refer to idiomatic C++ introduced in C++11. It has been that way for a couple of decades. https://learn.microsoft.com/en-us/cpp/cpp/welcome-back-to-cpp-modern-cpp https://learn.microsoft.com/en-us/cpp/cpp/welcome-back-to-cp... The article touches on the topic of move semantics and move constructors. That's modern c++. That's not your C++98 RAII. In the same vein, "modern CMake" is also the term used to refer to the declarative style that followed the release of CMake 3, which dates back years.
- AlexeyBrin 1y agoHow many modern compilers will let you write import std; in a C++ file and build the executable without shenanigans ? GCC 15 still does not let you compile a C++23 code with: g++ -std=c++23 -fmodules foo.cpp Without manually generating the std module first. Until all compilers will directly accept the import keyword it won't be adopted on a large scale by regular programmers.
- pjmlp 1y agoVisual C++ and clang. Many projects do just fine sticking to a single compiler. Not for everyone, but they exist nonetheless, including on FOSS world.
- AlexeyBrin 1y agoAFAIK, Clang (like GCC) needs an extra step to compile the standard modules files. It does not work, as one would reasonably expect, with something like: clang++ -std=c++23 fname.cpp (I know you can use Cmake to solve it - but I want a simple solution something that can be shown to a junior programmer.)
- deleted 1y ago[deleted]