The claim has been made that because C++ supports RAII (resource acquisition is initialization), it doesn’t need try…finally. I think this is wrong. There is at least one use case for try…finally that’s very awkward to model with RAII, and one that’s so ugly it ought to be outlawed.
The awkward use case
What if what you want to do during a …finally block has nothing to do with freeing resources? For example, suppose you’re writing the next version of Guitar Hero, and you want to guarantee that when your avatar leaves the stage, the last thing she does is take a bow–even if the player interrupts the performance or an error occurs.
Of course you can ensure this behavior with an RAII pattern, but it’s silly and artificial. Which of the following two snippets is cleaner and better expresses intent? Continue reading
I’m glad newly minted software engineers are exposed to data structures, compilers, concurrency, graph theory, assembly language, and the other goodies that constitute a computer science curriculum. All that stuff is important.
But it’s not enough.
Not all classroom material for CS folks should be technical. Photo credit: uniinnsbruck (Flickr).
Since I’m half way to curmudgeon-hood, I frequently find myself lamenting educational blindspots in the young. I’ve even toyed with the idea of teaching at the nearest university, some day when I Have More Time™. If academia would take me, my lesson plans might cover some of the following topics:
Herb Sutter (champion of VC++ compiler evolution at Microsoft) posted an interesting poll today. He wants to know what the C++ developer community thinks about priorities for the next release.
Editorial comment: I’m not surprised that conformance is winning over performance or fancy features. MS’s compiler has always been easy to use in a vacuum, but a pain when interoperability and cross-platform matter. When Herb went to MS, things improved drastically, but my perception is that gcc leapfrogged VC++ a year or two back, as C++11 began to gel. (VS 2012 probably gets back to parity again; I haven’t poked into it deeply, yet.)