Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find a good Scope Guard implementation for my C++ projects?

Tags:

c++

scope

guard

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.

like image 795
bodacydo Avatar asked Aug 12 '10 11:08

bodacydo


1 Answers

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.

like image 97
Josh Kelley Avatar answered Sep 19 '22 15:09

Josh Kelley