3 ms·
I never understood why C++ programmers like to make a big deal out of RAII (one of these purple acronyms only the C++ community can come up with). Object lifeti
by patrec 6y ago
I never understood why C++ programmers like to make a big deal out of RAII (one of these purple acronyms only the C++ community can come up with). Object lifetime tied resource management seems like an utterly obvious idea, I mean how else would you do it? There is a natural synergy with deterministic destruction (via stack allocation or reference counting) and resource life time management and I suspect the only reason C++ gets credit is that it has long been the only popular language with both objects and stack allocation¹. But lisps have had exactly the same concept via with-* macros (which explicitly introduce a scoped resource and perform cleanup on normal or exceptional exit) for ages, and I assume this precedes C++ by quite a bit. Also, RAII in C++ is subtly broken: C++ programmers like to use it to relinquish system resources (such as file handles) where relinquishment can fail and C++ really has no way to deal with that.
> Bjarne Stroustrup gave control of C++ in 1990 and became just one person vote, whatever happened afterwards was no longer under his control.
Well, it's not like "C++90" was this beautiful and elegant language (MVP, fun interactions between overloading and C's implicit integer conversion rules etc) corrupted by later design by committee. Also, he pretty strongly endorsed at least C++11 ("Surprisingly, C++11 feels like a new language: The pieces just fit together better than they used to and I find a higher-level style of programming more natural than before and as efficient as ever.").
> If you want his vision for the language before that event took place, you can refer to C++ARM and The Design and Evolution of C++ books.
Any chapters/page-ranges in either (or elsewhere) that you specifically recommend? I've read a bunch of Stroustrups writings or interviews over the years, but I don't remember anything that struck me as particularly insightful or interesting.
> He is the very first to call the attention of WG21 to the wrong direction of too many footguns with "Remember the Vasa" paper,
True, and he also said that Boost whilst being high quality and you should totally use it sometimes goes a bit overboard in terms of complexity (no kidding), but given the footguns and convolutedness he is personally responsible for, it rings a bit hollow to me.
Concerning STL: that seems mostly Stepanov's baby (as you noted) and I'm not fully convinced on its merits; there are good things about it -- it is generic, it tries to separate algorithms from concrete data structures, and to make it possible to efficiently operate on a subset of a data structure. Lastly it made complexity bounds part of the API, which I believe may have been an innovation. But it's also quite cumbersome and error prone to use.
¹ Some older GC'ed languages expose stack allocation as an opt-in optimization (e.g. some lisps), but not as a true first class concept.
- pjmlp 6y ago> But lisps have had exactly the same concept via with-* macros (which explicitly introduce a scoped resource and perform cleanup on normal or exceptional exit) for ages, and I assume this precedes C++ by quite a bit. Indeed, but if you forget to use them no one is going to come around to remind you of that. In Java and .NET there are static analysis checks that validate possible missing uses of try or using. In RAII aware languages (it is not only C++ that has it), you don't need to forget to call anything. > Well, it's not like "C++90" was this beautiful and elegant language (MVP, fun interactions between overloading and C's implicit integer conversion rules etc) corrupted by later design by committee. Also, he pretty strongly endorsed at least C++11 ("Surprisingly, C++11 feels like a new language: The pieces just fit together better than they used to and I find a higher-level style of programming more natural than before and as efficient as ever."). It definitely was, hence why everyone was replacing C with C++ (Apple, Microsoft, IBM, Be) until the FOSS freedom fighters decided to push C as their main language. As for the books, my copies are 2070 km away from my current location, so not much I can advise. Anyway I am not here to change your mind regarding him.
- patrec 6y ago> In RAII aware languages (it is not only C++ that has it), you don't need to forget to call anything. Uhm, how about delete? C++ only introduced working smart pointers a quarter century in with C++11. In the case that the lifetime coincides with the lexical scope C++ is robust, but so is, IMO, the with-* macro approach even without static validation (because unlike Go-style defer the construct itself introduces the object you want, so there is nothing to forget; if you really only care about the lexically scoped case you can also make this the only public API, and rule out errors that way). I think the most compelling argument that can be made for RAII is that GC is a bad abstraction, because it special cases memory deallocation whereas "true" RAII can deal with fairly arbitrary resources (with the big caveat that de-allocation must never fail), but to really make a strong form of this argument I think you need something like Rust's ownership model; even the weak form needs more than C++<11 had. > It definitely was, hence why everyone was replacing C with C++ (Apple, Microsoft, IBM, Be) until the FOSS freedom fighters decided to push C as their main language. Beauty had nothing to do with it. Would I write serious software in C over C++? No, just for less terrible strings and resource management alone. But would you have picked C++ in the early 90ies over Turbo Pascal because of its greater beauty, or even substantial technical reasons? The case of the enlightened companies vs unwashed FOSS masses is also a bit more complicated, and it's not that obvious to me the FOSS people made the wrong choice. Microsoft: their software at the time was of notoriously low quality; early linux was significantly more stable (and performant). Be: I think I recall an ex-employee claiming on HN that their OS (despite innovative ideas) also had major quality issues. Apple: they sure did a lot of work over the years to avoid having to commit to C++. IBM: what software did they even write during that time? OS/2? There are also major interfacing issues to other languages. And massive conceptual complexity, and difficulty agreeing on some "sane subset" over a huge community. Finally modern C++ these days takes, what, 100x longer to compile than C? I think a big part of the success of open source is due to the development of new languages with vibrant eco-systems, I suspect a massive move to C++ would have made that much more difficult.