tags:

views:

115

answers:

3

I just recently learned about Scope Guard C++ idiom. Unfortunately I can't find any good implementation of it.

Can anyone point me to some good and usable Scope Guard implementation in C++?

Thanks, Boda Cydo.

+3  A: 

ScopeGuard has been included in the Loki library (advertised in Modern C++ Design by Andrei Alexandrescu, I'm sure you've heard of this great book), and is mature enough to be used in production code, imo.

Just to be clear: We're talking about writing exception safe code using RAII.

Additional reading (on StackOverflow): Does ScopeGuard use really lead to better code?

Samuel_xL
Yes, I have heard about Modern C++ Design. But I have not read this book because it's very hard to understand! I maybe understand one sentence in ever page. :)
bodacydo
@boda Actually, I think it is rather clearly written. I think the problem is that he doesn't always make totally explicit the motivations for doing what he is talking about, but expects you to know them.
anon
+1  A: 

A "Scope Guard" object is just one instance of the much broader RAII idiom.

And there is no single implementation of that. It is something a C++ programmer has to understand, not just copy/paste. Luckily, it is also pretty trivial to implement.

You create a class which represents some kind of resource. When the class is instantiated (by one of its constructors), it should acquire the resource, and throw an exception if that fails. When the class is destroyed, it should dispose of the resource, performing all the necessary cleanup.

And... that's it. You also have to handle copy constructor and assignment operator (either by cloning the resource or by making these two functions private so they're never called).

You don't need to find "a good implementation", because you're going to write dozens and dozens of different implementations yourself. They're trivial to write, and they can't easily be reused because each one wraps a different type of resource.

jalf
Thanks, jalf, I will try to write my own. Boy, is this going to be a difficult exercise for me. I have never even dreamed of writing such complex code!
bodacydo
The advantage of a good Scope Guard class is that it makes it easy to call arbitrary code at cleanup, without having to write dozens and dozens of different implementations yourself.
Josh Kelley
+1  A: 

The original ScopeGuard class is included in this Dr. Dobb's article by Andrei Alexandrescu and Petru Marginean. A slightly improved version, with some changes from Joshua Lehrer is available here. (Lehrer's version is the one that I'm using in my projects.) It's also included in the Loki library.

Boost now has a ScopeExit library that's more powerful than ScopeGuard (since it can execute arbitrary code, whereas ScopeGuard can only call a single preexisting function).

Edit: With all of that said, a Scope Guard is really just a specific application of RAII, so you really ought to at least understand the concept of how to implement one.

Josh Kelley