7 ms·
Me too. RAII ftw!
by dicroce 12y ago
Me too. RAII ftw!
- sillysaurus3 12y agoGood luck making your RAII class exception safe. It's extremely difficult, to the point that writing a simple class becomes an hours-long exercise in reasoning about exceptions. And of course, if you admit that exceptions aren't worth worrying about, then you'll start to question the entire "C++ way," which usually ends in disillusionment. Alternatively, instead of disillusionment, you may still kind of enjoy C++. But suddenly you find that it's been three years since you've worked with C++, and on reflection, you haven't really been missing out on much of anything at all by not using C++. It doesn't seem like C++ solves the right problems, even with the new dialects. Programming languages should be convenient to think in. By immersing yourself in the C++ mental model, it becomes difficult to keep a large-scale architecture entirely in your head.
- jonstewart 12y agoHuh? RAII is basically the only way to write exception-safe code in C++.
- eropple 12y ago> Good luck making your RAII class exception safe. It's extremely difficult, to the point that writing a simple class becomes an hours-long exercise in reasoning about exceptions. In my experience, this is literally backwards. RAII is the only way to make exception-safe classes that aren't a gigantic mess. > It doesn't seem like C++ solves the right problems, even with the new dialects. Programming languages should be convenient to think in. No, some progrmaming languages should be convenient to think in. Some need to be uncompromisingly fast. C++ is in the latter set and nothing more convenient to think in is faster.
- halfcat 12y ago>Some need to be uncompromisingly fast. C++ is in the latter set and nothing more convenient to think in is faster. Yes, and C++11/14 make significant strides toward being more convenient to think in as well, without compromising speed. In fact some improvements also improve speed in some cases.
- sillysaurus3 12y agoYes, RAII is the only way to make an exception-safe class. Good luck doing it. It's not easy. I know; I've tried. There are all kinds of corner cases and special considerations that you have to take into account. People have written huge tomes about that exact topic detailing exactly why it's a hard problem and why you're probably not going to solve it correctly by accident. You're far more likely to introduce a memory leak than to make your class exception safe on the first try. That's my point. That's why C++ is overly cumbersome unless you ignore most of its capabilities, like exceptions. And that's why you can simply ignore C++ and not miss out on much. If performance is your only reason for rigorously sticking to a mental model, it's likely that your mental model is a premature optimization.
- jonstewart 12y agoWhat were these corner cases and special considerations you ran into? I wouldn't call Herb Sutter's _Exceptional C++_ a huge tome and it seems to be the standard treatment of exception safety.
- sillysaurus3 12y agoIt's 240 pages of knowledge that you can completely do without, and it's a book called "47 engineering puzzles." The puzzles, of course, are entirely exception-related. I'm not saying it's impossible to write exception safe code. I'm saying that you can write code without exceptions without being worse off for it.
- jonstewart 12y agoWe're talking about the same book, _Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions_, by Herb Sutter. The takeaway I had from reading that book (a long time ago) was just to use RAII religiously, which also tends to make other things easier. So I'm curious about why you think RAII makes writing exception-safe code difficult?
- sillysaurus3 12y ago
- pjmlp 12y ago> But suddenly you find that it's been three years since you've worked with C++, and on reflection, you haven't really been missing out on much of anything at all by not using C++. True on my case, now using the language mainly on hobby projects, although we are very good friends since 1993. Personally I would like safer systems programming languages from Mesa branch of languages had more use in the industry, but OS vendors decided to embrace C instead, and we ended up in the exploits everywhere that we enjoy nowadays. C++ is not perfect, but it surely is way better than C in terms of safety. Until a big OS vendor decides to push another systems programming language, we won't see any major adoption from the alternatives.
- eps 12y agoRAII is hardly a trait of modern C++.
- 72deluxe 12y agoEh? How do you mean that?
- vidarh 12y agoPresumably that it's nothing new. The term RAII dates to the mid to late 80's sometime, and you could hardly find any book about C++ from the 90's onwards that wouldn't be espousing RAII as the One True Way. RAII has been with C++ almost from the beginning.
- deong 12y agoYes, but "modern" doesn't imply that no feature is older than Twitter. "Modern C++" refers to the way that C++ is written today, and some of that is new additions to the language, some has been there for ages.
- leoc 12y agoIs your meaning that it's not a distinctive trait of modern C++?